Multiple+file+operations

//by Richard Russell, July 2007//

The Windows API function **SHFileOperation** is very flexible, allowing you to perform common file-system operations (delete, copy, move/rename) on one or more files. It can optionally move 'deleted' files to the Recycle Bin, display a handy progress dialogue for lengthy operations and prompt for confirmation.

Initialisation
Before using the **SHFileOperation** API incorporate the following declarations in your program:

code format="bb4w" FO_MOVE = 1 FO_COPY = 2 FO_DELETE = 3 FOF_MULTIDESTFILES = 1 FOF_CONFIRMMOUSE = 2 FOF_SILENT = 4 FOF_RENAMEONCOLLISION = 8 FOF_NOCONFIRMATION = 16 FOF_WANTMAPPINGHANDLE = 32 FOF_ALLOWUNDO = 64 FOF_FILESONLY = 128 FOF_SIMPLEPROGRESS = 256 : REM shfo.fFlags.h& = 1 FOF_NOCONFIRMMKDIR = 512 : REM shfo.fFlags.h& = 2 code

Deleting files
To delete one or more files use code similar to the following:

code format="bb4w" DEF PROCdeletefiles(afsp$) LOCAL shfo{} DIM shfo{hWnd%, wFunc%, pFrom%, pTo%, fFlags{l&,h&}, \ \       fAborted%, hNameMaps%, sProgress%} afsp$ += CHR$0 + CHR$0 shfo.hWnd% = @hwnd% shfo.wFunc% = FO_DELETE shfo.pFrom% = !^afsp$ SYS "SHFileOperation", shfo{} ENDPROC code As shown the operation will display a progress dialogue and prompt the user for confirmation. If you prefer it to happen 'silently' initialise the **fFlags** member as follows:

code format="bb4w" shfo.fFlags.l& = FOF_SILENT OR FOF_NOCONFIRMATION shfo.fFlags.h& = 0 code You can specify that the files are moved to the Recycle Bin rather than permanently deleted as follows:

code format="bb4w" shfo.fFlags.l& = FOF_ALLOWUNDO OR FOF_NOCONFIRMATION shfo.fFlags.h& = 0 code

Copying files
To copy one or more files use code similar to the following:

code format="bb4w" DEF PROCcopyfiles(from$,dest$) LOCAL shfo{} DIM shfo{hWnd%, wFunc%, pFrom%, pTo%, fFlags{l&,h&}, \ \       fAborted%, hNameMaps%, sProgress%} from$ += CHR$0 + CHR$0 dest$ += CHR$0 + CHR$0 shfo.hWnd% = @hwnd% shfo.wFunc% = FO_COPY shfo.pFrom% = !^from$ shfo.pTo% = !^dest$ SYS "SHFileOperation", shfo{} ENDPROC code The **from$** parameter should contain one or more path/filenames separated by NULs (CHR$0); the filenames may include wildcard characters (? or *). The **dest$** parameter should contain the path to the destination folder to which you want the files to be copied; this folder will be created if necessary.

As shown the operation will display a progress dialogue and prompt the user for confirmation. If you prefer it to happen 'silently' initialise the **fFlags** member as follows:

code format="bb4w" shfo.fFlags.l& = FOF_SILENT OR FOF_NOCONFIRMATION shfo.fFlags.h& = FOF_NOCONFIRMMKDIR DIV 256 code If you want to copy only files, rather than any subdirectories included in the **from$** specification, add the flag.

Moving files
To move (or rename) one or more files use code similar to the following:

code format="bb4w" DEF PROCmovefiles(from$,dest$) LOCAL shfo{} DIM shfo{hWnd%, wFunc%, pFrom%, pTo%, fFlags{l&,h&}, \ \       fAborted%, hNameMaps%, sProgress%} from$ += CHR$0 + CHR$0 dest$ += CHR$0 + CHR$0 shfo.hWnd% = @hwnd% shfo.wFunc% = FO_MOVE shfo.pFrom% = !^from$ shfo.pTo% = !^dest$ SYS "SHFileOperation", shfo{} ENDPROC code The **from$** parameter should contain one or more path/filenames separated by NULs (CHR$0); the filenames may include wildcard characters (? or *). The **dest$** parameter should contain the path to the destination folder to which you want the files to be moved; this folder will be created if necessary.

As shown the operation will display a progress dialogue and prompt the user for confirmation. If you prefer it to happen 'silently' initialise the **fFlags** member as follows:

code format="bb4w" shfo.fFlags.l& = FOF_SILENT OR FOF_NOCONFIRMATION shfo.fFlags.h& = FOF_NOCONFIRMMKDIR DIV 256 code If you want to move only files, rather than any subdirectories included in the **from$** specification, add the flag.

If, rather than moving all the files to the same destination folder, you want to move or rename them individually, initialise the **fFlags** member as follows:

code format="bb4w" shfo.fFlags.l& = FOF_MULTIDESTFILES shfo.fFlags.h& = 0 code Now the **dest$** parameter must contain the same number of path/filename specifications as there are in **from$**, separated by NULs (CHR$0).