Q: Delete or move files while displaying the flying folders

Answer: You can easily copy, delete, or move files by calling the API functions CopyFile, DeleteFile, and MoveFile. These functions are pretty easy to use, but they do not display the flying folders. Here are examples of each.

// copy SOURCE.TXT to DEST.TXT, the FALSE argument tells windows// to fail if DEST.TXT already exists. CopyFile returns BOOLif( CopyFile("C:\\SOURCE.TXT","C:\\DEST.TXT", FALSE) == FALSE) Application->MessageBox("CopyFile failed", "Error", MB_OK); // MoveFile and DeleteFile also return FALSE if failure.MoveFile("C:\\SOURCE.TXT","C:\\WINDOWS\\TEMP\\DEST.TXT");DeleteFile("C:\\SOURCE.TXT");

These functions are easy to use, but unfortunately they don't display the flying folders. In order to get the folders, you must use the SHFileOperation API function. The same examples that are shown above have been duplicated below to use SHFileOperation. Notice that the move and copy commands can specify a destination directory.

// make sure to include the file SHELLAPI.H. Its located in the// INCLUDE\WIN32 file if you're curious.#include <shellapi.h> // copy SOURCE.TXT to DEST.TXT;char *From = "C:\\SOURCE.TXT\0";char *To = "C:\\DEST.TXT\0";SHFILEOPSTRUCT op;ZeroMemory(&op, sizeof(op));op.hwnd = Handle; // Handle of main form or the applicationop.wFunc = FO_COPY;op.pFrom = From;op.pTo = To;op.fFlags= 0;SHFileOperation( &op); // move SOURCE.TXT the windows temp directorychar *From = "C:\\SOURCE.TXT\0";char *To = "C:\\WINDOWS\\TEMP\0";SHFILEOPSTRUCT op;ZeroMemory(&op, sizeof(op));op.hwnd = Handle;op.wFunc = FO_MOVE;op.pFrom = From;op.pTo = To;op.fFlags= 0;SHFileOperation( &op); // send all temp files to the recycle binchar *File = "C:\\windows\\temp\\*.TMP\0";SHFILEOPSTRUCT op;ZeroMemory(&op, sizeof(op));op.hwnd = Handle;op.wFunc = FO_DELETE;op.pFrom = File;op.fFlags= FOF_ALLOWUNDO;SHFileOperation( &op); // copy all text files in the root drive to// the temp directory.char *From = "C:\\*.TXT\0";char *To = "C:\\WINDOWS\\TEMP\0";SHFILEOPSTRUCT op;ZeroMemory(&op, sizeof(op));op.hwnd = Handle;op.wFunc = FO_COPY;op.pFrom = From;op.pTo = To;op.fFlags= 0;SHFileOperation( &op);

Notes:

1: When you specify FO_DELETE, the files will be sent to the recycle bin if fFlags contains the FOF_ALLOWUNDO style. Otherwise they will be deleted.

2: The pFrom and pTo structure items are char pointers. They are not arrays. They must point to a string, but the structure doesn't contain any memory space to actually contain the strings. Don't do this:

op.pFrom = "C:\\*.TXT"; op.pTo = "C:\\TEMP";

3: Notice the extra null terminator (added with the trailing '\0') in the file strings . The MSDN documentation states that the pTo and pFrom strings must be double null terminated.

4: The strings that pFrom and pTo point to can contain more than one string. Each string should be separated with one null terminator. The double null terminator mentioned above is used to terminate the entire list. Here is an example:

char *From = "C:\\*.txt\0C:\\*.log\0C:\\*.tmp\0"; op.pFrom = From; // the From declaration would be a little // cleaner by utilizing white space char *From = "C:\\*.txt\0" "C:\\*.log\0" "C:\\*.tmp\0";

5: When copying or moving files, the FOF_RENAMEONCOLLISION style in the fFlags parameter will prevent the function from overwriting existing files. The shell will create copies that say "Copy of readme.txt" (this is what explorer does).

6: You can also specify FO_RENAME as the wFunc parameter. The fFlags parameter could contain many other advanced styles. Check the WIN32.HLP file for more info.

Наши рекомендации