In the following script, you copy a folder called fso that resides on the C drive to a folder called fso1 on the C drive. It is important to note that the folder does not need to exist in order for the copy process to succeed.
Set objFSO = CreateObject ("scripting.fileSystemObject")
objFSO.CopyFolder "c:\fso","C:\Myfso"
You can make the script a little easier to use by creating variables to hold both the source and the destination folders. In the next script, CopyFolderExtended.vbs, you do exactly that. In addition, you create a constant called overwriteFiles that you set to True. Note that in this next script, the destination folder, called dFolder, is located on a network share. The CopyFolderExtended.vbs script could be used by a network administrator to copy user data from the local machine to a network drive for consolidated backup. One negative aspect of the CopyFolder command is that it does not indicate that it is working or that it is done. To give yourself a little bit more information, you use the Now command and WScript.Echo to indicate when the command begins. In addition, after the copy operation is complete, you display another message that the copy ended and the time.
Const OverWriteFiles = True
startTime = Timer
WScript.Echo " beginning copy ..."
strSource = "c:\Documents and Settings"
strDestination = "\\London\fileBU"
Set objFSO = CreateObject ("scripting.fileSystemObject")
objFSO.CopyFolder strSource, strDestination , OverWriteFiles
endTime = Timer
WScript.Echo "ending copy. It took: " & _
Round(endtime-startTime) & " seconds to copy"
1. | Open \My Documents\Microsoft Press\VBScriptSBS\Templates\FSOTemplate.vbs in Notepad or some other script editor. Save your file as YourNameListFolderSizes.vbs.
|
2. | Delete two Dim statements that will not be used: strFile and colFiles.
|
3. | Add two new variables: colFolders and strHeader. The completed Header section of the script looks like the following:
Option Explicit
'On Error Resume Next
Dim objFSO 'the fileSystemObject
Dim objFolder 'folder object
Dim strFolder 'individual folder form collection
Dim colFolders 'collection of subFolders
Dim strHeader 'header used for reporting
|
4. | In the Reference section of the script, add a constant called noDecimal and set it to 0. It will be used in the formatNumber function. This is seen below:
|
5. | Assign a folder location to the strFolder variable. Use the line that is commented out in the template to save typing. Mine looks like the following:
|
6. | Delete the Const forReading = 1, forWriting = 2, forAppending = 8 line.
|
7. | Delete the three lines in the template that are for use with files. These are listed below:
'strFile = "c:\fso\fso.txt"
'Set objFile = objFSO.OpentextFile(strFile)
'Set colFiles = objFolder.files
|
8. | Remove the comment from the Set objFolder = objFSO.GetFolder(strFolder) line.
|
9. | Assign the path property and the size property of the folder object to the strHeader variable. Use vbTab to provide spacing between the two properties. Use the formatNumber function to add commas to the number and to remove all trailing decimal positions. My code to do this looks like the following:
strHeader = objFolder.Path & vbTab & formatNumber(objFolder.size,noDecimal)
|
10. | Open the FunLine2.vbs script from the \My Documents\Microsoft Press\VBScriptSBS \Utilities folder and copy the funLine function from that file. Paste it at the bottom of your script. The funLine function looks like the following:
Function funLine(strIn)
funLine = Len(strIN)+1
funLine = strIN & VbCrLf & String(funLine,"=")
End Function
|
11. | Use the funLine function to underline strHeader when you print it out using WScript.Echo. The code to do this looks like the following:
WScript.Echo funLine(strHeader)
|
12. | Create a collection of subfolders and assign it to the colFolders variable. This is seen here:
Set colFolders = objFolder.SubFolders
|
13. | Use For...Each...Next to walk through the collection of subfolders. Use strFolder as the counter variable. Print out the path and the formatted size of each folder. The code to do this looks like the following:
For Each strFolder In colFolders
WScript.Echo strFolder.path, formatNumber(strFolder.size,noDecimal)
Next
|
14. | Save and run your script. You should see a printout of a folder and subfolders. If your code does not perform as expected, compare your script with \My Documents\Microsoft Press\VBScriptSBS\ch07\ListFolderSizes.vbs.
|