CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 1999
    Posts
    12

    FindFirstFile() Function??

    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:eleteWQRFiles(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;
    }



  2. #2
    Join Date
    Apr 1999
    Posts
    306

    Re: FindFirstFile() Function??

    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


  3. #3
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: FindFirstFile() Function??

    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.




    --
    Jason Teagle
    [email protected]

  4. #4
    Join Date
    May 1999
    Posts
    26

    Re: FindFirstFile() Function??

    Hi!

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

    bool CDeleteApp:eleteWQRFiles(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




  5. #5
    Join Date
    May 1999
    Posts
    26

    Re: FindFirstFile() Function??


    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



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured