You can easily call the API functions copyfile, deletefile, and movefile to copy, delete, and move files. These functions are quite easy to use, but they do not display flying folder animations. The following is an example of each API.
// Copy source. txt to DeST. txt. If the false parameter is specified
// Copy fails if it already exists. The returned value of copyfile is of the bool type.
If (copyfile ("C: // source. txt", "C: // DeST. txt", false) = false)
Application-> MessageBox ("copyfile failed", "error", mb_ OK );
// If the operation fails to be moved or deleted, false is returned.
Movefile ("C: // source. txt", "C: // windows // temp/DeST. txt ");
Deletefile ("C: // source. txt ");
These features are easy to use, but unfortunately they do not display flight folder animations. To display the animation, you must use the shfileoperation API function. The following is an example of using shfileoperation to complete the above operations. Note that you must specify the target folder for copying and moving operations.
// Confirm that you have included the header file shellapi. h
// If you are curious, the file is located in the include/Win32 Directory.
# 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 the main form or application
Op. wfunc = fo_copy;
Op. pfrom = from;
Op. PTO =;
Op. fflags = 0;
Shfileoperation (& OP );
// Move source. txt to the temporary directory of Windows
Char * 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 =;
Op. fflags = 0;
Shfileoperation (& OP );
// Delete all temporary files to the recycle bin
Char * 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 directory to the temporary 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 =;
Op. fflags = 0;
Shfileoperation (& OP );
Note:
1: When you specify the fo_delete operation, if fflags contains fof_allowundo, the files will be sent to the recycle bin; otherwise, they will be deleted.
2: The project pfrom and PTO in the op structure are character pointers instead of arrays. They must point to a string because the structure does not contain any storage space and cannot be done like this:
Op. pfrom = "C: // *. txt ";
Op. PTO = "C: // Temp ";
3: note the extra Terminator 0 in the file name string ('/0' in the string '). The msdn document indicates that the PTO and pfrom strings must have a dual zero end.
4: the memory to which pfrom and PTO point can contain multiple strings, which are separated by 0 and ended with double 0. The following is an example:
Char * From = "C: // *. txt/0C: // *. log/0C: // *. tmp/0 ";
Op. pfrom = from;
// You can also separate them with spaces
Char * From = "C: // *. txt/0"
"C: // *. log/0"
"C: // *. tmp/0 ";
5: When copying or moving a file, the fflags parameter containing fof_renameoncollision will prevent the function from rewriting the existing file, shell creates a replica named "original file name of the replica" (this is done by the resource manager ).
6: You can also specify fo_rename as the wfunc parameter. The fflags parameter can contain many other advanced methods. For more information, see the win32.hlp file.