Using Shell file operations
  • Brief Intro
File operations like copying and moving can be done using file I/O. But sometimes your application may need to ask whether user wants to overwrite the existing file or create new directory. Win32Shell provides a finer way for carrying out file operations with all the above mentioned features. Win32Shell provides SHFileOperation( ) function to perform file copy, move, delete and rename operations on any number of different files including wildcard filenames and directories. SHFileOperation( ) takes address of an object of SHFILEOPSTUCT structure. The SHFILEOPSTUCT structure looks like this:
typedef struct _SHFILEOPSTRUCT
{
HWND hwnd;
UINT wFunc;
LPCSTR pFrom;
LPCSTR pTo;
FILEOP_FLAGS fFlags;
BOOL f AnyOperationsAborted;
LPVOID hNameMappings;
LPCSTR lpszProgressTitle;

} SHFILEOPSTRUCT, FAR *LPSHFILEOPSTRUCT;

The wFunc element is the most important element which decides the operation to be performed. This member can be one of the following values:
FO_COPY Copies the files specified in the pFrom member to the location specified in the pTo member.
FO_DELET Deletes the files specified in pFrom. (pTo is ignored.)
FO_MOVE Moves the files specified in pFrom to the location specified in pTo.
FO_RENAME Renames the files specified in pFrom.
The element pFrom contains the address of a buffer to specify one or more source file names. Multiple names must be null-separated. For Ex. "c:\\aaa.txt\0c:\\bbb.txt\0c:\\My Documents\\ccc.txt".
pTo contains the address of a buffer to contain the name of the destination file or directory. The buffer can contain multiple destination file names if the fFlags member specifies FOF_MULTIDESTFILES. Multiple names must be null-separated.
fFlags controls the file operation. This member can be a combination of the following flags:
FOF_ALLOWUNDO Preserve Undo information, if possible.
FOF_CONFIRMMOUSE Not currently implemented.
FOF_FILESONLY Perform the operation on files only if a wildcard file name (*.*) is specified. FOF_MULTIDESTFILES The pTo member specifies multiple destination files (one for each source file) rather than one directory where all source files are to be deposited.
FOF_NOCONFIRMATION Respond with Yes to All for any dialog box that is displayed.
FOF_NOCONFIRMMKDIR Does not confirm the creation of a new directory if the operation requires one to be created.
FOF_NOERRORUI No user interface will be displayed if an error occurs.
FOF_RENAMEONCOLLISION Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.
FOF_SILENT Does not display a progress dialog box.
FOF_SIMPLEPROGRESS Displays a progress dialog box but does not show the file names.
FOF_WANTMAPPINGHANDLE If FOF_RENAMEONCOLLISION is specified, the hNameMappings member will be filled in if any files were renamed. The element fAnyOperationsAborted receives TRUE if the user aborted any file operations before they were completed, or FALSE otherwise. hNameMappings is the handle to a file name mapping object that contains an array of SHNAMEMAPPING structures. Each structure contains the old and new path names for each file that was moved, copied, or renamed. This member is used only if the fFlags member includes the FOF_WANTMAPPINGHANDLE flag. The handle must be freed by using the SHFreeNameMappings( ) function. lpszProgressTitle is the address of a string to use as the title of a progress dialog box. This member is used only if fFlags includes the FOF_SIMPLEPROGRESS flag. On clicking the menu item File Operation, a dialog is displayed. On entering the source and target files if user clicks on Copy button the file given in Source edit box are coipied to the directory specified in the Target edit box. Similarly if Move button is pressed source file is moved to the target directory and is given name which is given in the target. In the same way file given as source is renamed to the file given in target if Rename button is pressed.