how do i see if a file already exists on drive?
AFAIK you can use CreateFile to see if a file already exists.
Invoke CreateFile, addr FileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
addr FileName - pointer to filename
GENERIC_READ - means we want to read from the file
0 - this prevents someone else from accessing the file... you can change this to FILE_SHARE_READ + FILE_SHARE_WRITE since we're just checking if it's there.
(first) NULL - this is the security parameter, we're not interested in security
OPEN_EXISTING - means the CreateFile will open an existing file without destroying it, and will fail if it does not exist.
FILE_ATTRIBUTE_NORMAL - tells the OS not to bother changing the file attributes.
(second) NULL - template file, I'm not sure what the template file is for but you should generally NULL it.
If this call fails, more likely than not the file does NOT exist. You might want to check GetLastError() to be sure that it does not exist, or if someone else locked the file from access.
If it succeeds the file most definitely exists, you must close the file soon after.
Other solutions may exist, however, which are most likely much more elegant.:grin: :grin:
Invoke CreateFile, addr FileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
addr FileName - pointer to filename
GENERIC_READ - means we want to read from the file
0 - this prevents someone else from accessing the file... you can change this to FILE_SHARE_READ + FILE_SHARE_WRITE since we're just checking if it's there.
(first) NULL - this is the security parameter, we're not interested in security
OPEN_EXISTING - means the CreateFile will open an existing file without destroying it, and will fail if it does not exist.
FILE_ATTRIBUTE_NORMAL - tells the OS not to bother changing the file attributes.
(second) NULL - template file, I'm not sure what the template file is for but you should generally NULL it.
If this call fails, more likely than not the file does NOT exist. You might want to check GetLastError() to be sure that it does not exist, or if someone else locked the file from access.
If it succeeds the file most definitely exists, you must close the file soon after.
Other solutions may exist, however, which are most likely much more elegant.:grin: :grin:
There's a couple of ways to do it. The obvious is to CreateFile with
OPEN_EXISTING (*AND* checking GetLastError if CreateFile fails).
Or you could use FindFirstFile... or you could GetFileAttributes (and
again, GetLastError on failure).
OPEN_EXISTING (*AND* checking GetLastError if CreateFile fails).
Or you could use FindFirstFile... or you could GetFileAttributes (and
again, GetLastError on failure).
GetFileAttributes?!?! That sounds MUCH more elegant. Fewer parameters, no need to close the file...
yea thats what im using
how can i use it on directories too?
I also recommend GetFileAttributes, it is fast. To use it on a directory, just specify the path (as you do with a file). I am not sure if a backslash is required or not.
I use this for directories or files:
invoke FindFirstFile, addr szOutputDirBuffer, addr wfd
.if eax == INVALID_HANDLE_VALUE ; file / directory does not exist
You guys apparently don't know about Shlwapi. It has has MANY usefull functions incuding:
BOOL PathFileExists(LPCTSTR pszPath);
BOOL PathIsDirectory(LPCTSTR pszPath);
BOOL PathIsDirectoryEmpty(LPCTSTR pszPath);
Enjoy!
BOOL PathFileExists(LPCTSTR pszPath);
BOOL PathIsDirectory(LPCTSTR pszPath);
BOOL PathIsDirectoryEmpty(LPCTSTR pszPath);
Enjoy!
Why should I include a additional library to use that slow functions when I can write my own wich is faster in just 2 lines? I add kernel32.lib anyway because of ExitProcess. :)
regards,
bAZiK
regards,
bAZiK
Here's a link to MSDN onlline for those who don't have the docs!
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/shlwapi/shlwapi.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/shlwapi/shlwapi.asp
When speed is limited by some unavoidable bottleneck (in this case disk access) I go for code minimization instead!
Why should I include a additional library to use that slow functions when I can write my own wich is faster in just 2 lines? I add kernel32.lib anyway because of ExitProcess. :)
That's a good reason not to use PathFileExists. However, I'm glad gfalen posted the ShlWapi references: the only bad reason to not use PathFileExists is that you didn't know it was there. :)
Know where your system calls are, then avoid them if you choose.
Cheers,
-Chalain
...who once spent half a day writing a FileExists function because he didn't know PathFileExists existed.