More Windows Scripting Workarounds from Nilpo
(Page 1 of 4 )
Scripting, by nature, is a limited form of programming. As a result of these limitations it is sometimes necessary to employ workarounds in order to produce a desired effect. I showed you some of the workarounds that I commonly use in the first volume of this series. Today, I return with volume two to demonstrate more workarounds that you may find useful in your scripting endeavors.
While I have discovered many of these workarounds on my own, some of them are not my own creation. Some of these can be found on the Rube Goldberg Memorial Scripting Page. I apologize for others in this series that may go uncredited as I'm unaware of their origins. In any case, let's get started.
Display a File Copy Dialog
The most common scripting method for copying files and folders in WSH is by means of the FileSystemObject (Scripting.FileSystemObject). As you may have noticed, this is a silent process that does not display any form of progress dialog. While there are workarounds in existence that can produce a functional command-line version, there is a way to produce the real thing.
A file copy progress dialog box is displayed any time that you copy files or folders using Windows Explorer. This can be demonstrated by dragging and dropping files in My Computer. It stands to reason that this is a feature of the Windows Explorer Shell. In scripting, we do not have access to these specific Explorer APIs; however, we do have access to Explorer's scriptable interface through the use of the Windows Shell object. Since this object employs the native APIs, a progress dialog is displayed whenever the object is used to move or copy files and folders.
strSourceFolder = "C:Folder 1"
strDestFolder = "C:Folder 2"
Set objShell = CreateObject("Shell.Application")
Set objSourceFolder = objShell.NameSpace(strSourceFolder)
Set objDestFolder = objShell.NameSpace(strDestFolder)
result = objDestFolder.CopyHere(objSourceFolder.Items)
This simple example uses the Windows Shell object to perform a file copy procedure. The Shell object's NameSpace method is first used to create Folder objects for the source and destination folders. (The Items property returns a collection of File and Folder objects contained in a Folder). The CopyHere method is then used to initiate the copy process. You may also use the MoveHere method instead if you wish to perform a Move or Rename operation.
Const sFOF_SILENT = 4 'Do not display a progress dialog box.
Const sFOF_RENAMEONCOLLISION = 8 'Rename if target file already exists.
Const sFOF_NOCONFIRMATION = 16 'Respond with "Yes to All" for any dialog box that is displayed.
Const sFOF_ALLOWUNDO = 64 'Preserve undo information, if possible.
Const sFOF_FILESONLY = 128 'Perform the operation on files only (not on folders) if wildcard (*.*) is specified.
Const sFOF_SIMPLEPROGRESS = 256 'Display a progress dialog box without showing file names.
Const sFOF_NOCONFIRMMKDIR = 512 'Create destination directory if required.
Const sFOF_NOERRORUI = 1024 'Do not display a user interface if an error occurs.
Const sFOF_NOCOPYSECURITYATTRIBS = 2048 'Do not copy the security attributes of the file.
Const sFOF_NORECURSION = 4096 'Only operate in the local directory. Don't operate recursively. (Default behavior)
Const sFOF_NO_CONNECTED_ELEMENTS = 9182 'Do not copy connected files as a group. Only copy the specified files. (Win 98SE and higher)
The MoveHere and CopyHere methods also accept a second parameter that specifies shell settings to be used during the operation. These constants must be added to your script if you do not wish to use the default settings.
Next: Implementing Password Masking >>
More Windows Scripting Articles
More By Nilpo