CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2004
    Location
    Redlands, CA
    Posts
    74

    Exclamation SHFileOperation question

    I try to delete files using Shell Structure. Instead of deleting file by file using :: DeleteFile(), I want to delete all files with a wild character (.*).

    //CComBSTR with the path to files (i.e. "C:\Files\files.*")
    // filePath and fileName is passed to this function as BSTR types
    CComBSTR pathWild(filePath);
    pathWild.Append("\\");
    pathWild.Append(fileName);
    pathWild.Append(".*");

    // Delete files
    SHFILEOPSTRUCT shDelete;
    HRESULT hr;

    shDelete.wFunc = FO_DELETE;
    shDelete.fFlags = FOF_SILENT | FOF_NOCONFIRMATION;
    shDelete.pFrom = pathWild;
    shDelete.pTo = NULL;

    hr = SHFileOperation(&shDelete);

    I got an error 1026, and nothing was deleted. shDelete.pFrom accepts an "Address of a buffer to specify one or more source file names", I guess it doesn't like CComBSTR. How could I solve this problem?!

    Any help is highly appreciated (Second day I am trying to solve it). Maybe there is another way to delete files with wild character?!

  2. #2
    Join Date
    Nov 2001
    Posts
    323

    Re: SHFileOperation question

    Best Regards,

    --Zim
    If you find this post useful, please rate it.
    _________________________________
    "Have you the brain worms?!?!?"

  3. #3
    Join Date
    Sep 2004
    Location
    Redlands, CA
    Posts
    74

    Re: SHFileOperation question

    Quote Originally Posted by Zim327
    Yes, I've already looked at it before. It is a file by file deletion. I would liek to use the shell structure to delete many files at the same time. I have problems passing the CComBSTR to it.

  4. #4
    Join Date
    Nov 2001
    Posts
    323

    Re: SHFileOperation question

    Unicode must be enabled

    shDelete.pFrom = (LPCTSTR)pathWild.operator BSTR( );

    or you could load the bstr into a TCHAR buf[255];

    I got these suggestions here:
    http://www.csupomona.edu/~dlbell/cpp...sfa99/174.html

    the msdn states: Multiple names must be null-separated. The list of names must be double null-terminated.
    so I'm not sure you can use wild cards (.*)

    HTH,
    Last edited by Zim327; January 11th, 2005 at 02:28 PM. Reason: additional info
    Best Regards,

    --Zim
    If you find this post useful, please rate it.
    _________________________________
    "Have you the brain worms?!?!?"

  5. #5
    Join Date
    Nov 2001
    Posts
    323

    Re: SHFileOperation question

    wait there's more!
    to use wild cards *.* you must use this flag FOF_FILESONLY

    And you should be able to load the bstr into a CString and then use operator LPCTSTR. again unicode must be enabled.
    Best Regards,

    --Zim
    If you find this post useful, please rate it.
    _________________________________
    "Have you the brain worms?!?!?"

  6. #6
    Join Date
    Sep 2004
    Location
    Redlands, CA
    Posts
    74

    Re: SHFileOperation question

    Quote Originally Posted by Zim327
    Unicode must be enabled

    shDelete.pFrom = (LPCTSTR)pathWild.operator BSTR( );

    or you could load the bstr into a TCHAR buf[255];

    I got these suggestions here:
    http://www.csupomona.edu/~dlbell/cpp...sfa99/174.html

    the msdn states: Multiple names must be null-separated. The list of names must be double null-terminated.
    so I'm not sure you can use wild cards (.*)

    HTH,
    In MSDN it says also:
    Standard Microsoft MS-DOS wild cards, such as "*", are permitted in the file-name position.

    I've basically tried all string conversions from CComBSTR to pass to a shell structure, but I guess the integer length of the string that is stored preceding the address of a string in CComBSTR is my problem in trying to convert it properly to hold the address of a string. Passed BSTR to the function hold an address of "0x004240a4 'string' ", when I convert CComBSTR to BSTR or LPCTSTR it holds only "0x004240a4".

  7. #7
    Join Date
    Sep 2004
    Location
    Redlands, CA
    Posts
    74

    Re: SHFileOperation question

    Quote Originally Posted by Zim327
    wait there's more!
    to use wild cards *.* you must use this flag FOF_FILESONLY

    And you should be able to load the bstr into a CString and then use operator LPCTSTR. again unicode must be enabled.
    No, luck either. But thanks for the remark.

  8. #8
    Join Date
    Nov 2001
    Posts
    323

    Re: SHFileOperation question

    If Unicode is enabled then you won't see the contents of any of the strings in the debugger.
    you must add the string to the watch window and then add ,su after it and then
    you can see the contents.
    have you tried hard coding the string just to see if it will work? just to make sure it's definitely a string problem?
    Best Regards,

    --Zim
    If you find this post useful, please rate it.
    _________________________________
    "Have you the brain worms?!?!?"

  9. #9
    Join Date
    Sep 2004
    Location
    Redlands, CA
    Posts
    74

    Re: SHFileOperation question

    Quote Originally Posted by Zim327
    If Unicode is enabled then you won't see the contents of any of the strings in the debugger.
    you must add the string to the watch window and then add ,su after it and then
    you can see the contents.
    have you tried hard coding the string just to see if it will work? just to make sure it's definitely a string problem?
    Yes, I've hard coded LPCTSTR like
    LPCTSTR test = L"C:\\Something\\files.*";

    and it works fine with Shell Structure.
    The same with BSTR:
    BSTR test2 = L"C:\\Something\\files.*"

    So, it is definitely some problem with string address.

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: SHFileOperation question

    Quote Originally Posted by tigrushke
    It is a file by file deletion.
    Well...what do you think happens internally while using 'SHFileOperation()'?

  11. #11
    Join Date
    Sep 2004
    Location
    Redlands, CA
    Posts
    74

    Cool Re: SHFileOperation question

    I've solved my task with file by file deletion (checking for ReadOnly property and if it is true setting it to normal state, and then :: DeleteFile(file)), but it is not code sufficient, and I would like to simplify my code with separate Shell structure method.
    So, I am still looking for solution on this problem.

  12. #12
    Join Date
    Sep 2004
    Location
    Redlands, CA
    Posts
    74

    Re: SHFileOperation question

    Quote Originally Posted by Andreas Masur
    Well...what do you think happens internally while using 'SHFileOperation()'?
    I bet it is definitely file by file deletion I would be happy to debug Shell structure code to understand in full where it fails, but unfortunatelly, I cannot do that.

  13. #13
    Join Date
    Oct 2007
    Posts
    9

    Re: SHFileOperation question

    The folowing code deletes all files inside D:\tempDir\folder1
    Code:
    CString tempDir = L"D:\\tempDir\\folder1\\*";
    
    ASSERT(tempDir.GetLength()+2 > MAX_PATH);
        TCHAR path[MAX_PATH];
        wcscpy_s(path, MAX_PATH, tempDir.GetBuffer());
        path[tempDir.GetLength()] = '\0';
        path[tempDir.GetLength()+1] = '\0';
    
        SHFILEOPSTRUCT fileOP;
        ZeroMemory(&fileOP, sizeof(SHFILEOPSTRUCT));
        fileOP.hwnd = NULL;
        fileOP.wFunc = FO_DELETE;
        fileOP.pFrom = path;
        fileOP.pTo = NULL;
        fileOP.fFlags = FOF_NO_UI;
        BOOL bRes = SHFileOperation(&fileOP);
    ASSERT(!bRes);
    what I have tried to stress upon is how important it really is to put two NULLs at the end of string. I was getting the 1026 error for a long time till I really made sure it is double NULL terminated.

    Happy debugging! (and sorry for writing in a very old thread. blame google for that. i m innocent, my lord.)

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