Click to See Complete Forum and Search --> : FindFirstFile() Function??


masterx
May 13th, 1999, 09:43 PM
Can anyone help me correct this code or maybe suggest something better?

I am attempting to delete all the files in a directory that have the "xqc" extension. I am getting an Access Violation in the NT.dll dll when it gets to the FindFirstFile() function. What is wrong with this code?

In addition, I am trying to delete only files that I can delete, so I want to avoid deleting read-only files and/or directories that are returned by the FindFirstFile() function.

Please note that some lines are commented out.

Thanks for your help,

Austin.

---------------------------------------------------------------

bool CUpdtDlg::deleteWQRFiles(CString delPath)
{
HANDLE fileHnd;
LPWIN32_FIND_DATA lpFileData = 0;
LPCTSTR pDelFile;
CString delFile;

bool result = false;

delFile = delPath + "*.xqc";
pDelFile = delFile.GetBuffer(delFile.GetLength() + 1);
delFile.ReleaseBuffer(-1);

fileHnd = FindFirstFile(pDelFile, lpFileData);
// fileHnd = FindFirstFile("c:\\data\\cordraw\\ixf\\*.xqc", lpFileData);

if (fileHnd != INVALID_HANDLE_VALUE)
{
// if (lpFindFileData.dwFileAttributes
// FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_READONLY )

DeleteFile(LPCTSTR (delPath + lpFileData->cFileName));

while (FindNextFile(fileHnd, lpFileData))
{
DeleteFile(LPCTSTR (delPath + lpFileData->cFileName));
}
}

FindClose(fileHnd);

return result;
}

ric
May 14th, 1999, 02:38 AM
I don't know if this is the problem, but this is a kind of very big problem. You have declared your File Data as LPWIN32_FIND_DATA, which actually is a pointer to such a WIN32_FIND_DATA structure. So you have a pointer but you do not have a valid OBJECT to which this pointer to point. So try declaring you file data structure like this:

WIN32_FIND_DATA fd;

and then give its address to the FindFirstFile function like this:

fh = FindFirstFile(delpath,&fd);

try it out this way I do not see more problems at a first glance

Jason Teagle
May 14th, 1999, 03:06 AM
I notice that you have used CString::GetBuffer() to get a pointer to a modifiable string area, but then you have called ReleaseBuffer(); this means that the pointer you just got may become invalid. Attempting to write here would probably cause a problem. Although the help says that the buffer would not be reallocated as long as you don't change the string length, I would advise not calling ReleaseBuffer() until you have finished with it. In fact, you do not even need to do this; you can use the pointer from (LPCTSTR)delFile as the filespec pointer for FindFirstFile().

The most important thing, though is that you have defined an LPWIN32_FIND_DATA, and set it to NULL. The FindFirstFile() function expects a pointer to an EXISTING WIN32_FIND_DATA structure so that it can put information in it; passing NULL will result in the access violation.

Replace the line

LPWIN32_FIND_DATA lpFileData = 0;

with

WIN32_FIND_DATA FileData ;

and then pass &FileData as the second parameter to FindFirstFile() and FindNextFile(). That should cure it.

scp
May 14th, 1999, 10:54 AM
Hi!

I don't know what exactly is wrong with your code, but the following will work just as fine:

bool CDeleteApp::deleteWQRFiles(CString delPath)
{
CFileFind ff;
bool result = false;
CString delFile = delPath + "*.xqc";

result = ff.FindFile (delFile);

while (result)
{
result = ff.FindNextFile ();

CString strFilePath = ff.GetFilePath ();
CFileStatus fStatus;

if (!(fStatus.m_attribute == 0x10 ||
fStatus.m_attribute == 0x01))
{
DeleteFile (strFilePath);
}
}

ff.Close ();

return result;
}

You can even check for the return value of DeleteFile() function...

Santhosh

scp
May 14th, 1999, 10:59 AM
One more question:

Do you have a '\' at the path you are passing to the function, right?

Otherwise you may have to change

delFile = delPath + "*.xqc";

to

delFile = delPath + "\\*.xqc";

Regards,
Santhosh