Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


How do I know if a folder exists?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Wed Feb 17, 2021 3:26 pm    Post subject: How do I know if a folder exists? Reply with quote

I did some research, but the results were ineffective.

I don't want to delete or recreate the folder.
How can I test if the folder exists without opening it?

Code:
local scriptFile = os.getenv("TEMP")
local a1=(scriptFile..'\\New1') --or nil

if a1==nil then
print("none: "..a1)
else
print("yes: "..a1)
end


Any ideas?
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4300

PostPosted: Wed Feb 17, 2021 3:34 pm    Post subject: Reply with quote

Can't with stock Lua.

Maybe the CE function getDirectoryList would be of use to you.
celua.txt wrote:
getFileList(Path:string, searchMask:string OPTIONAL, SearchSubDirs: boolean OPTIONAL, DirAttrib: integer OPTIONAL): Returns an indexed table with filenames
getDirectoryList(Path:string, SearchSubDirs: boolean OPTIONAL): Returns an indexed table with directory names
extractFileName(filepath): returns the filename of the path
extractFilePath(filepath): removes the filename from the path

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25296
Location: The netherlands

PostPosted: Wed Feb 17, 2021 3:40 pm    Post subject: This post has 1 review(s) Reply with quote

Code:

attr=lfs.attributes(folderpath)
if attr then
  --file/folder exists
  if attr.mode=='directory' then
    print("folder exists")
  else
    print("It exists, but is no folder")
  end
else
  --file/folder does not exist
  print("Does not exist")
end

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Wed Feb 17, 2021 4:21 pm    Post subject: Reply with quote

Dark Byte wrote:
Code:

attr=lfs.attributes(folderpath)
if attr then
  --file/folder exists
  if attr.mode=='directory' then
    print("folder exists")
  else
    print("It exists, but is no folder")
  end
else
  --file/folder does not exist
  print("Does not exist")
end


I saw "LuaFileSystem" in my research.
But finding his example in CE was comforting to me.
Thanks DB. And again I owe you +1.


EDIT: In CELua, there is no reference to "LuaFileSystem".
From what I see this is an added lua encoding.
Maybe you can sample this coding from DB and add it to CELua later.
Explanation;

Quote:
Introduction
LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution.

LuaFileSystem offers a portable way to access the underlying directory structure and file attributes.
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Wed Feb 17, 2021 11:05 pm    Post subject: This post has 1 review(s) Reply with quote

Example (on your case):

Code:
function pathExist(fl)
 local fd = os.getenv("TEMP")
 --print(fd)
 local check = os.execute([[cd "]]..fd.."\\"..fl)
 if not check then print("Folder doesnt exist") else
 print("Folder exist")
 return fl
 end
end


pathExist("New1")


If not use os.getenv() then just change the script according the desire purpose.

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Thu Feb 18, 2021 8:14 am    Post subject: Reply with quote

Yes @Corroder this works fine.
Thanks for adding a new approach

There are some terms that I normally use;
([[mkdir "]]) = Create folder.
([[rmdir "]]) = Delete folder.

([[cd "]]) = I'm just seeing this.
Please give the reference or explanation of "([[cd"]]) "?

And also @DarkByte, I have a question;
Does the "lfs" example you gave above include the following commands?
Lua does not define "lfs", you gave an example in CE.
All or which of the following codes are defined in CE?

Reference
LuaFileSystem offers the following functions:

lfs.attributes (filepath [, request_name | result_table])
Returns a table with the file attributes corresponding to filepath (or nil followed by an error message and a system-dependent error code in case of error). If the second optional argument is given and is a string, then only the value of the named attribute is returned (this use is equivalent to lfs.attributes(filepath)[request_name], but the table is not created and only one attribute is retrieved from the O.S.). if a table is passed as the second argument, it (result_table) is filled with attributes and returned instead of a new table. The attributes are described as follows; attribute mode is a string, all the others are numbers, and the time related attributes use the same time reference of os.time:
dev
on Unix systems, this represents the device that the inode resides on. On Windows systems, represents the drive number of the disk containing the file
ino
on Unix systems, this represents the inode number. On Windows systems this has no meaning
mode
string representing the associated protection mode (the values could be file, directory, link, socket, named pipe, char device, block device or other)
nlink
number of hard links to the file
uid
user-id of owner (Unix only, always 0 on Windows)
gid
group-id of owner (Unix only, always 0 on Windows)
rdev
on Unix systems, represents the device type, for special file inodes. On Windows systems represents the same as dev
access
time of last access
modification
time of last data modification
change
time of last file status change
size
file size, in bytes
permissions
file permissions string
blocks
block allocated for file; (Unix only)
blksize
optimal file system I/O blocksize; (Unix only)
This function uses stat internally thus if the given filepath is a symbolic link, it is followed (if it points to another link the chain is followed recursively) and the information is about the file it refers to. To obtain information about the link itself, see function lfs.symlinkattributes.
lfs.chdir (path)
Changes the current working directory to the given path.
Returns true in case of success or nil plus an error string.
lfs.lock_dir(path, [seconds_stale])
Creates a lockfile (called lockfile.lfs) in path if it does not exist and returns the lock. If the lock already exists checks if it's stale, using the second parameter (default for the second parameter is INT_MAX, which in practice means the lock will never be stale. To free the the lock call lock:free().
In case of any errors it returns nil and the error message. In particular, if the lock exists and is not stale it returns the "File exists" message.
lfs.currentdir ()
Returns a string with the current working directory or nil plus an error string.
iter, dir_obj = lfs.dir (path)
Lua iterator over the entries of a given directory. Each time the iterator is called with dir_obj it returns a directory entry's name as a string, or nil if there are no more entries. You can also iterate by calling dir_obj:next(), and explicitly close the directory before the iteration finished with dir_obj:close(). Raises an error if path is not a directory.
lfs.lock (filehandle, mode[, start[, length]])
Locks a file or a part of it. This function works on open files; the file handle should be specified as the first argument. The string mode could be either r (for a read/shared lock) or w (for a write/exclusive lock). The optional arguments start and length can be used to specify a starting point and its length; both should be numbers.
Returns true if the operation was successful; in case of error, it returns nil plus an error string.
lfs.link (old, new[, symlink])
Creates a link. The first argument is the object to link to and the second is the name of the link. If the optional third argument is true, the link will by a symbolic link (by default, a hard link is created).
lfs.mkdir (dirname)
Creates a new directory. The argument is the name of the new directory.
Returns true in case of success or nil, an error message and a system-dependent error code in case of error.
lfs.rmdir (dirname)
Removes an existing directory. The argument is the name of the directory.
Returns true in case of success or nil, an error message and a system-dependent error code in case of error.
lfs.setmode (file, mode)
Sets the writing mode for a file. The mode string can be either "binary" or "text". Returns true followed the previous mode string for the file, or nil followed by an error string in case of errors. On non-Windows platforms, where the two modes are identical, setting the mode has no effect, and the mode is always returned as binary.
lfs.symlinkattributes (filepath [, request_name])
Identical to lfs.attributes except that it obtains information about the link itself (not the file it refers to). It also adds a target field, containing the file name that the symlink points to. On Windows this function does not yet support links, and is identical to lfs.attributes.
lfs.touch (filepath [, atime [, mtime]])
Set access and modification times of a file. This function is a bind to utime function. The first argument is the filename, the second argument (atime) is the access time, and the third argument (mtime) is the modification time. Both times are provided in seconds (which should be generated with Lua standard function os.time). If the modification time is omitted, the access time provided is used; if both times are omitted, the current time is used.
Returns true in case of success or nil, an error message and a system-dependent error code in case of error.
lfs.unlock (filehandle[, start[, length]])
Unlocks a file or a part of it. This function works on open files; the file handle should be specified as the first argument. The optional arguments start and length can be used to specify a starting point and its length; both should be numbers.
Returns true if the operation was successful; in case of error, it returns nil plus an error string.

Thanks in advance for the answers.

EDIT:
I'm trying to delete a folder.
Operations applied and their results;
1) The folder can be deleted manually.
2) Changing the folder name manually and with "os.rename".
3) Folder size ranges from 110 MB to 1 GB.
4) The folder cannot be deleted with "os.execute ([[rmdir"]]) ". (Lower size folders can be deleted)
5) The folder is not deleted with "os.remove". (Smaller sized folders can be deleted)
6) I tried changing the folder name and deleting it, the name changes but is not deleted.

What can I do?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25296
Location: The netherlands

PostPosted: Thu Feb 18, 2021 10:37 am    Post subject: Reply with quote

before you can delete a folder (rmdir) you need to delete all files and subfolders in that folder (excluding the folders named "." and ".." because it's a bad idea to recursively delete everything in "..")
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Thu Feb 18, 2021 12:35 pm    Post subject: Reply with quote

@DB, subfolders and folders are 1300 and as you said it's pretty hard to do this.
Also, the files will have variable names and versions as they are updated by the game.
This will not produce the same result for every user.

But I solved this Smile
It will delete the file and not throw it in the recycle bin.
In addition, there will not even be a confirmation notification.
Here is the code;

Code:
local scriptFile = os.getenv("TEMP")
local a1=(scriptFile..'\\New1')

os.execute([[rd /s /q ]]..a1)

"rmdir" failed to do this, or if there were no side commands.
"rd" and the side commands "/ s / q" did this.

1350 files or folders, 140 MB, cleared in one second. No notification, no traces in the recycle bin. Clean.
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Thu Feb 18, 2021 8:20 pm    Post subject: Reply with quote

ByTransient wrote:


([[cd "]]) = I'm just seeing this.
Please give the reference or explanation of "([[cd"]]) "?


CD mean 'Change Directory' or = CHDIR, will return nil if the directory not found/exists

https://ss64.com/nt/cd.html
https://www.geeksforgeeks.org/cd-cmd-command/

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Thu Feb 18, 2021 9:12 pm    Post subject: Reply with quote

Corroder wrote:

CD mean 'Change Directory' or = CHDIR, will return nil if the directory not found/exists

https://ss64.com/nt/cd.html
https://www.geeksforgeeks.org/cd-cmd-command/


Thanks. I will add this to my notes.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites