ShellFileOperation

Requirements

Version Info

codeRelease #3
documentation2010-09-29, 16:51

History

2007-07-05Original version by SKAN
2010-03-15Updated to work with AutoHotkey_L (Lexikos helped make it truly correct)
2010-07-09Parameters can be passed as strings instead of numbers (much easier)
2010-09-14Now works with any build.  For builds with objects, special return value containing mapping results
2010-09-15Improved the documentation
2010-09-29updated documentation
Summary
ShellFileOperation
Functions
ShellFileOperationWrapper for the SHFileOperation shell function.
Constants
OperationsFor use with the “op” parameter for ShellFileOperation
FlagsFor use with the “flags” parameter for ShellFileOperation

Functions

ShellFileOperation

ShellFileOperation(op,
source,
dest,
flags)

Wrapper for the SHFileOperation shell function.

Parameters

opType of operation, see Operations
sourcelist of source items, with each path separated by a “|”.
desta list of destination items.  If the FOF_MULTIDESTFILES flag is set, the number of source and dest items should be the same.  Otherwise, dest should be a single path into which all source items will go.  Like source, separate multiple paths with a “|”.  For FO_DELETE, you can leave this blank.
flagsa list of flags, again delimited by “|”s.  See the Flags section for a list of available flags + their descriptions.  It is fine to list one flag multiple times (it won’t do anything).

Return Value

The return value depends on what build of AHK you are using.  If you are using the normal “vanilla” build, you will just get a number.  For users with object support, you’ll get an object.

Returned Number

The aborted flag from SHFileOperation (1 = an operation was aborted, 0 = nothing was aborted)

Returned Object (with the following members)

abortedsame as the return value for vanilla AHK
errorthe error code returned by SHFileOperation (0 = no error)
num_mappingsthe number of mappings returned by SHFileOperation
mappingsan object containing all the mappings returned by SHFileOperation, where [key,value] corresponds to [old_path,new_path]

Numbers vs Strings

If you really want to, you can give ShellFileOperation numbers rather than strings.  For multiple flags, you’d have to bitwise-or them together.  In the following snippet of code, both lines are equivalent (though one of them is easier to read...)

ShellFileOperation( "FO_Move","C:\file.txt", "G:\file.txt","FOF_SIMPLEPROGRESS|FOF_WANTMAPPINGHANDLE" )
ShellFileOperation( 0x1,"C:\file.txt", "G:\file.txt",0x100|0x20 )

Mappings

If the FOF_WANTMAPPINGHANDLE flag is given, SHFileOperation will return a set of mappings to describe any renaming that took place.  For example...

1You try to move “C:\file.txt” to “G:\file.txt”, but “G:\file.txt” already exists.
2You choose to rename (“keep both files”, as the error UI will say)
3The returned mapping object will contain the key-value pair (“G:\file.txt”,”G:\file (2).txt”)
ret := ShellFileOperation( "FO_Move","C:\file.txt", "G:\file.txt","FOF_SIMPLEPROGRESS|FOF_WANTMAPPINGHANDLE" )
MsgBox % ret.mappings["C:\file.txt"]  ; will display G:\file (2).txt in this example

Tricking SHFileOperation

Mappings are useful, but what happens if the user cancels a single file copy?  Unfortunately, SHFileOperation was not designed to provide such feedback.  The abort flag will only tell you that some operation was aborted, leaving you to figure it out for yourself.  However, I’ve discovered a way to trick SHFileOperation into reporting everything that was moved...

For Foldersuse a trailing slash.  SHFileOperation will feel compeled to automatically rename the folder so as to remove the trailing slash, thus tricking it into including the operation in mappings.
For Filesuse a trailing space.  Again, SHFileOperation will remove the trailing slash automatically and place it into mappings.  Unfortunately, there is a small problem with this trick.  Any actual renames (due to conflicts) will get butchered.  The ShFileOperation UI will offer to rename the file to, say “file (2).txt”, but will actually end up renaming it to “file.txt (2)”.  However, this is a very easy problem to fix.  In FileContainer, I use a postprocessing routine that picks out the butchered renames and manually fixes them.

I may eventually include this functionallity directly in ShellFileOperation.

Constants

Operations

For use with the “op” parameter for ShellFileOperation

FO_MOVE(0x1) move items
FO_COPY(0x2) copy items
FO_DELETE(0x3) delete items
FO_RENAME(0x4) rename items

Flags

For use with the “flags” parameter for ShellFileOperation

FOF_MULTIDESTFILES(0x1) Indicates that the to member specifies multiple destination files (one for each source file) rather than one directory where all source files are to be deposited.
FOF_CONFIRMMOUSE(0x2) ... don’t have a clue
FOF_SILENT(0x4) Does not display a progress dialog box.
FOF_RENAMEONCOLLISION(0x8) Gives the file being operated on a new name (such as “Copy #1 of...”) in a move, copy, or rename operation if a file of the target name already exists.
FOF_NOCONFIRMATION(0x10) Responds with “yes to all” for any dialog box that is displayed.
FOF_WANTMAPPINGHANDLE(0x20) returns info about the actual result of the operation
FOF_ALLOWUNDO(0x40) Preserves undo information, if possible.  With del, uses recycle bin.
FOF_FILESONLY(0x80) Performs the operation only on files if a wildcard filename (.) is specified.
FOF_SIMPLEPROGRESS(0x100) Displays a progress dialog box, but does not show the filenames.
FOF_NOCONFIRMMKDIR(0x200) Does not confirm the creation of a new directory if the operation requires one to be created.
FOF_NOERRORUI(0x400) don’t put up error UI
FOF_NOCOPYSECURITYATTRIBS(0x800) dont copy file security attributes
FOF_NORECURSION(0x1000) Only operate in the specified directory.  Don’t operate recursively into subdirectories.
FOF_NO_CONNECTED_ELEMENTS(0x2000) Do not move connected files as a group (e.g. html file together with images).  Only move the specified files.
FOF_WANTNUKEWARNING(0x4000) Send a warning if a file is being destroyed during a delete operation rather than recycled.  This flag partially overrides FOF_NOCONFIRMATION.
FOF_NORECURSEREPARSE(0x8000) treat reparse points as objects, not containers (?)
ShellFileOperation(op,
source,
dest,
flags)
Wrapper for the SHFileOperation shell function.
For use with the “op” parameter for ShellFileOperation
(0x1) Indicates that the to member specifies multiple destination files (one for each source file) rather than one directory where all source files are to be deposited.
(0x3) delete items
For use with the “flags” parameter for ShellFileOperation
(0x20) returns info about the actual result of the operation
An extension of Container, specialied to hold file and folder paths.
Close