how do i see if a file already exists on drive?
Posted on 2002-04-30 18:59:30 by Qages
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:
Posted on 2002-04-30 19:10:58 by AmkG
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).
Posted on 2002-04-30 19:11:25 by f0dder
GetFileAttributes?!?! That sounds MUCH more elegant. Fewer parameters, no need to close the file...
Posted on 2002-04-30 19:18:10 by AmkG
yea thats what im using
Posted on 2002-04-30 19:56:09 by Qages
how can i use it on directories too?
Posted on 2002-04-30 21:05:37 by Qages
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.
Posted on 2002-05-01 05:40:41 by sluggy
I use this for directories or files:



invoke FindFirstFile, addr szOutputDirBuffer, addr wfd
.if eax == INVALID_HANDLE_VALUE ; file / directory does not exist
Posted on 2002-05-01 06:15:36 by bazik
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!
Posted on 2002-05-01 09:49:49 by gfalen
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
Posted on 2002-05-01 09:54:09 by bazik
Posted on 2002-05-01 09:54:25 by gfalen
When speed is limited by some unavoidable bottleneck (in this case disk access) I go for code minimization instead!
Posted on 2002-05-01 10:03:48 by gfalen

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.
Posted on 2002-05-01 13:49:18 by Chalain