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

    Need to delete the files in a folder

    I'm going insane. I cannot figure out what function to call to delete the files in a folder.

    Code:
    const wchar_t g_wsz_DriverBackupFolder[] = L"C:\\Sample\\*\0\0\0\0";
    
    
    SHFILEOPSTRUCT info;
    memset(&info, 0, sizeof(info));
    info.wFunc = FO_DELETE;
    info.fFlags = FOF_NOCONFIRMATION | FOF_SILENT;
    info.pFrom = g_wsz_DriverBackupFolder;
    if (0 != SHFileOperation(&info))
    {
    	DWORD dwError = GetLastError();
    	DWORD dwError1 = GetLastError();
    	DWORD dwError2 = GetLastError();
    	DWORD dwError3 = GetLastError();
    }
    All I get is an error: GetLastError = 6: "Invalid Handle". Oh thank yo that's useful.

    I don't want any nag screens or anything asking me how I feel. I just want the contents to be gone. I also have no patience at this point whatsoever so please don't answer with a riddle or broken code fragment. I will just blow my top and since I'm not videoing this I won't even be able to post to YouTube whatever funny destructive thing I end up doing.
    Why are the "tolerant" so easy to offend?

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Need to delete the files in a folder

    Why don't you use _unlink to delete a file ?

  3. #3
    Join Date
    Aug 1999
    Posts
    492

    Re: Need to delete the files in a folder

    Quote Originally Posted by Skizmo
    Why don't you use _unlink to delete a file ?
    Because I'm trying to delete all files from the folder.
    Why are the "tolerant" so easy to offend?

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Need to delete the files in a folder

    You can use CFileFind.

    Code:
    CFileFind clFiles;
    clFiles.FindFile (_T("c:\\*.*"));
    
    while (clFiles.FindNextFile ())
    {
    	_wunlink (clFiles.GetFilePath ());
    }

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Need to delete the files in a folder

    Don't dare to try this myself but maybe you should provide a valid hwnd even though you have set FOF_SILENT.

  6. #6
    Join Date
    Aug 1999
    Posts
    492

    Re: Need to delete the files in a folder

    Quote Originally Posted by Skizmo
    You can use CFileFind.

    Code:
    CFileFind clFiles;
    clFiles.FindFile (_T("c:\\*.*"));
    
    while (clFiles.FindNextFile ())
    {
    	_wunlink (clFiles.GetFilePath ());
    }

    Can't use it. This is a plain and simple console app that does not use MFC.
    Why are the "tolerant" so easy to offend?

  7. #7
    Join Date
    Aug 1999
    Posts
    492

    Re: Need to delete the files in a folder

    Quote Originally Posted by S_M_A
    Don't dare to try this myself but maybe you should provide a valid hwnd even though you have set FOF_SILENT.

    I have no window because this is just a plain and simple console app.
    Why are the "tolerant" so easy to offend?

  8. #8
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Need to delete the files in a folder

    You seem to have not read the document. If you did , you wouldn't have relied on GetLastError

  9. #9
    Join Date
    Aug 1999
    Posts
    492

    Re: Need to delete the files in a folder

    Well, here's what the problem was.

    First, the path must be only the path (with the double NULL termination). No wildcards:

    const wchar_t g_wsz_DriverBackupFolder[] = L"C:\\Sample\0\0";

    Next, the FOF_FILESONLY flag must be specified.


    So basically even though I already told it plenty of information for performing these tasks it required a tweak to a string and a special flag that the documentation certainly doesn't imply is required ("Files only" means that without it I'd risk killing directories. It should still DELETE!)

    Naturally the Microsoft gurus behind the MSDN documentation didn't bother to document any of this and I had to get the answer online from people like you all.


    Oh, and the function still claims to fail even though it succeeds. Good ol' Microsoft.
    Why are the "tolerant" so easy to offend?

  10. #10
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Re: Need to delete the files in a folder

    Just a guess - but the error you're seeing might be because the file was a system file or something like that. I routinely 'normalize' the attributes of a file before attempting to delete it. Something like:-

    Code:
    ::SetFileAttributes("path_to_file",  FILE_ATTRIBUTE_NORMAL);
    ::remove("path_to_file");
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Need to delete the files in a folder

    Quote Originally Posted by SteveS
    Can't use it. This is a plain and simple console app that does not use MFC.
    There's a Win32 equivalent of those functions which doesn't use MFC, FYI.

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