Thread: SHELL - SHFileOperation

  1. #1
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470

    SHELL - SHFileOperation

    Hello.
    I wanted to copy a single file to another location, using shell functions. So I did this:

    Code:
    // ******** get path ****************
    
    char path[_MAX_PATH];
    SHGetPathFromIDList (
    pidl,
    path 
    );
    // ******** file operation ************
    
    SHFILEOPSTRUCT fopstruct;
    fopstruct.hwnd = NULL; // parent wnd
    fopstruct.wFunc = FO_COPY; // flag
    fopstruct.pFrom = path; // path of the object user selects from browse for folder dialog box
    fopstruct.pTo = "E:\cookies"; // destination
    
    BOOL foCheck = SHFileOperation (
    &fopstruct
    );
    I tried above code so many times but never worked. I don’t get any errors though, the file/folder just doesn’t get copied.
    So I tried hard coding a path into pFrom, member of SHFILEOPSTRUCT:

    Code:
    fopstruct.pFrom = “E:\Debug”;
    Then the file got copied properly into “E:\cookies”. But I think I cannot rely on hard coded paths.
    Also, when I tried to delete a file/folder suing FO_DELETE flag, it didn’t worked either, even when I hard coded the path.
    Why these things are happening? How can I fix them? Please help.
    Thanks a lot for reading.
    -geek@02

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    1. You need to make sure SHGetPathFromIDList() is succeeding.
    2. You must zero out all windows structures before use. This avoids problems when you don't fill in all the members, as you are not in this case.
    3. pFrom and pTo must be double null terminated.
    4. Also make sure that SHBrowseForFolder() is succeeding.
    Code:
    char path[MAX_PATH + 1]; // Space provided for extra null terminator
    
    if (SHGetPathFromIDList (pidl, path)) // Get path
    {
    	// ******** file operation ************
    
    	path[lstrlen[path] + 1] = '\0';    // Double null terminate path
    
    	SHFILEOPSTRUCT fopstruct = { 0 };  // Zero out structure before use
    	fopstruct.hwnd   = NULL;           // parent wnd
    	fopstruct.wFunc  = FO_COPY;        // function
            fopstruct.fFlags = 0;              // flags
    	fopstruct.pFrom  = path;           // path of the object - double null terminated
    	fopstruct.pTo    = "E:\\cookies\0"; // destination - double null terminated
    
    	BOOL foCheck = SHFileOperation (&fopstruct);
    
    	if (!foCheck) MessageBox(NULL, "SHFileOperation failed!", NULL, MB_ICONSTOP);
    }
    else
    {
    	MessageBox(NULL, "SHGetPathFromIDList failed!", NULL, MB_ICONSTOP);
    }
    
    CoTaskMemFree(pidl); // Release pidl returned by SHBrowseForFolder
    Last edited by anonytmouse; 04-26-2004 at 10:28 AM.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Don't forget to double up on backslashes in string literals as well.
    You have "E:\cookies", where it should be as anonytmouse posted.

    gg

  4. #4
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    Yes, now it’s working.
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 05-27-2009, 12:26 PM
  2. What shell is more powerful?
    By xddxogm3 in forum Tech Board
    Replies: 10
    Last Post: 07-24-2004, 10:47 PM
  3. System.ini Shell Problems
    By (TNT) in forum Windows Programming
    Replies: 2
    Last Post: 08-26-2001, 01:05 PM