Checking if a folder exists...
-
I need to check if a folder exists. I have a specific path and will perform a certain action only if that path exists. How can I check to see if a specific folder (ie. "c:\Program Files\Debug") exists. I have already tried using the WIN32 API call GetFileAttributes. I was checking to see if there was an error or not to see if the folder exists. The call returns an error regardless. The path is a BSTR, casted to char*, casted to LPCTSTR, if this is a problem.
-
I need to check if a folder exists. I have a specific path and will perform a certain action only if that path exists. How can I check to see if a specific folder (ie. "c:\Program Files\Debug") exists. I have already tried using the WIN32 API call GetFileAttributes. I was checking to see if there was an error or not to see if the folder exists. The call returns an error regardless. The path is a BSTR, casted to char*, casted to LPCTSTR, if this is a problem.
-
Thanks for the links. But actually, I tried pretty much everything there. Thing is, the _access ends up calling the GetFileAttributes API call and the GetFileAttributes call was what I was having problems with. I actually found out what the problem was that I was having with GetFileAttributes. I guess the GetFileAttributes has it's own parser to parse the string you feed into it. I was feeding in a path that I had read straight from the registry. It was "C:\Program Files\Program". In order to turn off the parser (it tells me this in the help section), you need to append "\\?\" to the front of the path. Since I had my path in a variable, I needed to do this in order to get the correct path for stuffing into the GetFileAttributes function... Path = "\\\\?\\" + Path; It works now.
-
I need to check if a folder exists. I have a specific path and will perform a certain action only if that path exists. How can I check to see if a specific folder (ie. "c:\Program Files\Debug") exists. I have already tried using the WIN32 API call GetFileAttributes. I was checking to see if there was an error or not to see if the folder exists. The call returns an error regardless. The path is a BSTR, casted to char*, casted to LPCTSTR, if this is a problem.
ChemmieBro wrote: The path is a BSTR, casted to char*, casted to LPCTSTR, if this is a problem. It is. A BSTR is a unicode string; you can't cast it to a char*. You'll have to call the unicode version of the function (
GetFileAttributesW()
), or use theW2T()
ATL conversion macro to convert from unicode to LPCTSTR.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Thanks for the links. But actually, I tried pretty much everything there. Thing is, the _access ends up calling the GetFileAttributes API call and the GetFileAttributes call was what I was having problems with. I actually found out what the problem was that I was having with GetFileAttributes. I guess the GetFileAttributes has it's own parser to parse the string you feed into it. I was feeding in a path that I had read straight from the registry. It was "C:\Program Files\Program". In order to turn off the parser (it tells me this in the help section), you need to append "\\?\" to the front of the path. Since I had my path in a variable, I needed to do this in order to get the correct path for stuffing into the GetFileAttributes function... Path = "\\\\?\\" + Path; It works now.
ChemmieBro wrote: Thing is, the _access ends up calling the GetFileAttributes API... Where is this documented at?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
ChemmieBro wrote: Thing is, the _access ends up calling the GetFileAttributes API... Where is this documented at?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
I don't believe it is documented. But if you step into the _access() call while debugging, it's right there.
-
I don't believe it is documented. But if you step into the _access() call while debugging, it's right there.
Thanks for the tip.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
I need to check if a folder exists. I have a specific path and will perform a certain action only if that path exists. How can I check to see if a specific folder (ie. "c:\Program Files\Debug") exists. I have already tried using the WIN32 API call GetFileAttributes. I was checking to see if there was an error or not to see if the folder exists. The call returns an error regardless. The path is a BSTR, casted to char*, casted to LPCTSTR, if this is a problem.
I implemented a function for something I worked on that checks to see if a file exists, but the documentation indicates that it works for directories as well as files. So take a look at this. It's worked well for me and seems very reliable. It seems different than the other responses, so if you are still looking for an alternative; here you go. I simply created a CString as input. The CString may contain wildcard characters such as * or ?. FindFirstFile is a global method and can be found in the MSDN library so go ahead and look it up and feel free to cut and paste this function and play with it to see how it works. It's pretty simple. BOOL CDXControlDlg::IsNewFile(CString& sPath) { // Check if files exists already WIN32_FIND_DATA fileInfo; HANDLE hFile = ::FindFirstFile(sPath, &fileInfo); if (hFile == INVALID_HANDLE_VALUE) { // File not found return TRUE; } else { // File exists. ::FindClose(hFile); return FALSE; } } Best Regards, Shawn
-
Thanks for the tip.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
I think you might be surprised if you take a 'generic' C app and build it in windows and then trace through the 'standard C function' calls. You will find many of them devolving into regular WIN32 API calls. People were trying to convince me one time that the File* functions (fopen, close, fprintf, etc.) were faster than CreateFile WriteFile, ReadFile, etc. I traced into the C runtime and find that after a lot of variable hashing and flags manipulation, they end up calling the Win32 API anyway, so I just started calling the Win32 API directly, as there is no effort at being 'port ready' to other platforms for our products. I am not surprised that _access calls GetFileAttributes.