The use of SHFileOperation

Source: Internet
Author: User

The use of SHFileOperation

Delete a file or folder
BOOL DeleteFile (char * lpszpath)
{
Shfileopstruct fileop={0};
Fileop.fflags = Fof_allowundo | Allow the Recycle Bin to be put back
Fof_noconfirmation; No confirmation dialog box appears
Fileop.pfrom = Lpszpath;
Fileop.pto = NULL; Be sure if it's null
Fileop.wfunc = Fo_delete; Delete operation
Return SHFileOperation (&fileop) = = 0;
}

Copy a file or folder
BOOL CopyFile (char *pto,char *pfrom)
{
Shfileopstruct fileop={0};
Fileop.fflags = fof_noconfirmation| No confirmation dialog box appears
Fof_noconfirmmkdir; Create a folder directly when needed, without user identification
Fileop.pfrom = Pfrom;
Fileop.pto = PTo;
Fileop.wfunc = fo_copy;
Return SHFileOperation (&fileop) = = 0;
}

Move a file or folder
BOOL MoveFile (char *pto,char *pfrom)
{
Shfileopstruct fileop={0};
Fileop.fflags = fof_noconfirmation| No confirmation dialog box appears
Fof_noconfirmmkdir; Create a folder directly when needed, without user identification
Fileop.pfrom = Pfrom;
Fileop.pto = PTo;
Fileop.wfunc = Fo_move;
Return SHFileOperation (&fileop) = = 0;
}


From naming a file or folder
BOOL RenameFile (char *pto,char *pfrom)
{
Shfileopstruct fileop={0};
Fileop.fflags = fof_noconfirmation; No confirmation dialog box appears
Fileop.pfrom = Pfrom;
Fileop.pto = PTo;
Fileop.wfunc = Fo_rename;
Return SHFileOperation (&fileop) = = 0;
}

Application Example:
DeleteFile ("d:\\pid\0\0");   //Delete a folder
DeleteFile ("D:\\pid.dsp\ 0d:\\pid.dsw\0\0 "); Delete multiple files
CopyFile ("D:\0\0", "D:\\myprojects\\ Temporary program \0\0");   //Place the "Temporary programs" folder under the D-Disk
CopyFile ("D : \0\0 "," D:\\myprojects\\ provisional Procedure \\PID.dsw\0 "); Myprojects\\ Temporary Program \0\0 "folder is copied to the D disk, and from the name NewName, it should be
CopyFile (" D:\\newname\0\0 "," d:\\myprojects\\ Temp program \\*.*\0\0 "); Copy the "Temporary programs" folder to the D disk and name it "NewName"   

The following class allows you to copy multiple files or folders for reference only
Classes that connect multiple paths
Class Joinfilepath
{
Private
int POS;
char* Multiplefilepath;
Public
Joinfilepath ()
{
pos=0;
Multiplefilepath=new CHAR[MAX_PATH*10];
memset (MULTIPLEFILEPATH,0,MAX_PATH*10);
}
~joinfilepath () {delete []multiplefilepath;}
void Join (char *filepath)
{
while (*filepath!= ')
multiplefilepath[pos++]=*filepath++;
pos++;
}
char * Getmultiplefilepath () {return multiplefilepath;}
};

Application Examples:
Joinfilepath FilePath;
Filepath.join ("D:\\myprojects\\ Provisional Procedure \\PID\\PID.DSP");
Filepath.join ("D:\\myprojects\\ Provisional Procedure \\PID\\PID.DSW");
CopyFile ("D:\0\0", Filepath.getmultiplefilepath ());

1 Pfrom and PTO It is best to end with \0\0 (the string that holds the path is initialized to 0), otherwise there may be an error, and each path in the middle is separated by
2 pfrom points to the file or folder (can be multiple) will be copied or moved to the folder under the PTO point (if the folder does not exist will ask whether to create, of course, you can also choose to create directly)

Detailed parameters:

Typedef struct _SHFILEOPSTRUCT
{
HWND hwnd; The window handle of the message being sent;
UINT Wfunc; Type of operation
LPCSTR Pfrom; source Files and Paths
LPCSTR PTo; Destination Files and Paths
Fileop_flags fflags; Operation and Confirmation flag
BOOL fanyoperationsaborted; Action Select bit
LPVOID hnamemappings; File mapping
LPCSTR Lpszprogresstitle; File Operation Progress window title
}shfileopstruct, FAR * lpshfileopstruct;

In this structure, the HWND is a handle to the window that sends the message, PFROM and PTO are the source and destination filenames for the file operation, which contains the path of the file, the path string corresponding to a single file, or, for multiple files, must be null as the end of the string or between the file path name. Otherwise, an error occurs when the program is running. In addition, both PFROM and PTO support wildcards * and? , which greatly facilitates the use of developers. For example, if the source file or directory has two, it should be: Char pfrom[]= "D:\\test1\0d:\\text.txt\0", which represents the operation of all files in the D: Disk test directory and the Text.txt file on the D: disk. The "\ \" In the string is the escape character of ' \ ' in the C language, and '/' is null. Wfunc is a very important member of the structure, it represents the type of operation that the function will take, and its value is as follows:

Fo_copy: Copies the file pfrom to the specified position of the PTO.

Fo_rename: Rename the filename of pfrom to the file name of PTO.

Fo_move: Move the Pfrom file to the PTO location.

Fo_delete: Deletes the file specified by Pfrom.

When you use this function for file copy, move, or delete, if it takes a long time, the program automatically displays a modeless dialog box during the process (the File Operation dialog box provided by the Windows operating system), which shows the progress and execution time of the execution, and the name of the file being copied, moved, or deleted. The members in the structure Lpszprogresstitle display the caption for this dialog box. Fflags is the process and status control identification during file operation. It mainly has some of the following identities, or it can be a combination:

Fof_filesonly: Execute wildcard character, execute file only;

Fof_allowundo: Save undo information in order to recover files in the Recycle Bin;

Fof_noconfirmation: If the target file already exists, if you do not set this, it will appear to confirm whether to overwrite the dialog box, set this to automatically confirm, overwrite, does not appear dialog box.

Fof_noerrorui: When you set this key, you do not receive an error when an error occurs during file processing, or you will be prompted with an error.

Fof_renameoncollision: When a file name already exists, replace it with a text prompt.

Fof_silent: The progress dialog box is not displayed.

Fof_wantmappinghandle: the SHFileOperation () function is required to return the list of actual files that are in operation State, and the file list name handles are saved in the Hnamemappings member.

The SHFILEOPSTRUCT structure also contains an array of shnamemapping structures that hold the old and new paths of each file in action that is computed by the shell.

When you use this function to delete a file, you must set the mysterious Fof_allowundo flag in the SHFILEOPSTRUCT structure so that the file to be deleted is copied to the Recycle Bin, which allows the user to undo the delete operation. Note that if Pfrom is set to a filename, deleting the file with the FO_DELETE flag will not move it to recycle Bin, or even set the Fof_allowundo flag, where you must use the full path name, This will shfileoperation the deleted files to the Recycle Bin.

Shell's file manipulation functions
SHFileOperation
Function:
1. Copying one or more files
2. Delete one or more
3. Renaming files
4. Move one or more files

There are the same Win32API function functions:
CopyFile (), DeleteFile (), MoveFile ()
MoveFile can rename the file!
Win32 API level is lower than shfileoperation

SHFileOperation
The important parameters
1.wFunc//PFROM PTO action to perform
2.fFlags//affect operation on Wfunx
3.hNameMappings//There are system fills, and you can also populate
4.lpszProgressTitle

Pfrom PTo at the end is two ' \0\0 '
Usually with a ' "", this will fail!!
When Fof_multidestfiles
Szpfrom[lstrlen (Szpfrom) +1]=0

Szpfrom: You must first make sure that the file he specified exists!
Can be a single file name, *. *, or a file name that contains a wildcard character
Note must be a filename, not the folder name where the file resides
Szsource: Can be a directory, if not a directory, but there is
Multiple files, you must correspond to each of the Szpfrom files, and also specify
Fof_multidetfiles logo


Source and Target
Multiple Files---> a folder
Many separate files----> a folder
Separate files---> individual files
Many separate files----> many separate files

Separate file: A file that knows the name
Multiple files: Files with wildcard characters
Notice that there is no action on the folder in Source!!


!!!!
SHFileOperation can manipulate files on the network
If you want to copy Local files to 192.168.1.99
So just share the 123 directory on the 192.168.1.99.
Then set the PTO to \\192.168.1.99\123
You can do it.
But do not set it to \\192.168.1.99


for hnamemappings operations is undocumented!!
If Hnamemappings
is not specified, then hnamemappings is always null
Hnamemappings is not null unless a file name conflict is caused by an operation (COPY,MOVE,RENAME)!!!
Hnamemappings must be null when you copy some files to an empty directory for the first time
so hnamemappings is just an area of memory that lets Explorer.exe save the renamed file to avoid file name collisions!
It knows how to make hnamemappings effective
now how to use hnamemappings, and the size of the structure it refers to? and get the contents of this memory block?
Hnamemappings is simple lpvoid you cannot use loop
to use hnamemappings, you must define a struct
struct handletomappings {
    uint              uNumberOfMappings; Number of mappings in array
    lpshnamemapping   lpshnamemapping;   // Pointer to array of mappings
};
But can write a enumerate function to enumerate lpshnamemapping point to the memory block, and is let window itself call me, not I actively invoke, like loop

Related joins:
Q154123:file Name Mapping with Windows NT 4.0 Shell
Q133326:shfileopstruct pfrom and PTo fields incorrect
Q142066:PRB:SHGetNameMappingPtr () and Shgetnamemappingcount ()
Manipulating Files with the SHFileOperation Function in Visual Basic 4.0

Fof_silent//Does not produce a dialog box that is being copied
fof_noconfirmmkdir//If the destination directory does not exist, it is created by default
Fof_noconfirmation//Does not appear confirmation file Replace dialog (confirmation Dialog) (default replaces original article I pieces)
fof_noerrorui//error dialog box does not appear
Best not to use Fof_noerrorui,fof_noconfirmmkdir at the same time
Because Fof_noconfirmmkdir blocked the missing directory Error
But Fof_noerror also blocked missing directory Error, so use Fof_noerrorui,fof_noconfirmmkdir at the same time
Also prevents the creation of a new directory with quiet (no user confirmation to generate a new Directory dialog box)!!
So how do you use Fof_noerrorui,fof_noconfirmmkdir at the same time?
You must first confirm that the directory specified by the PTO is present.
BOOL makesurediretorypathexists (LPCSTR dirpath);

Use it to include Imagehlp.h and Imagehlp.lib
How to judge simultaneous existence of Fof_noerrorui,fof_noconfirmmkdir


fof_renameoncollision//automatic renaming when duplicate files are available


A flag that can generate a dialog box:
Fof_silent//progress Dialog
Fof_renameoncollision//replace Dialog
Fof_noconfirmation//confirmation Dialog
Fof_noconfirmmkdir//asks for your permission to create a new folder
Fof_noerrorui//error Message


Fof_allowundo//Put files into the Recycle Bin, otherwise directly deleted, generally this is best to do the default

The use of SHFileOperation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.