Is your server running slowly? Check with SolarWinds ipMonitor
Get a free evaluation copy of ipMonitor
Contents for Guy's Scripting Ezine 36
- FSO Copy (Part 2)
This week's secrets are the FSO 'dot' commands. What I am referring to are the methods, which you employ to manipulate VBScript objects. For example, once you create an object called objFSO by
using the command: Set objFSO = CreateObject("Scripting.FileSystemObject"), then you can .Copy, or .Move, and thus manipulate that file object. These .dot commands open up a whole new area of scripting
and not just for FSO. How do you find out which .dot commands are available for which command? The best answer is by getting a free download of the OnScript editor.
One task that is ripe for scripting is automating file copying, for example,
housekeeping log files or
daily accounts records. From a scripting point of view, this weeks
example extends our previous lessons with FSO (File Scripting Object)
in Ezine 36 and Ezine 12. From a learning progression point of view, this script builds on Ezine 35, which explains how to create a text file.
Instructions
- For strFilePath and strDestination I could have used "C:\". Instead, I
deliberately left in a path that exists on my machine because I wanted
you to think about these string values and then either adjust the script
(best) or create the corresponding folders on your machine.
- Copy and paste the main script into notepad. Alternatively, use a script editor like OnScript.
- Save the file with .vbs extension e.g. CopyFSO.vbs
- Double click and observe the file that you have just created and copied.
- If you get errors, check that you have amended strFilePath and
strDestination. If you still get Code 800xxxxx message boxes, check out
the
error codes
Example 1 - Create a text file, then copy it to a new
path
' CopyFSO.vbs ' Example VBScript for manipulating files.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 3.4 - 4th July 2004
' ----------------------------------------------------------
Option Explicit
Dim objFSO, objFileCopy, objGuyFile
Dim strFilePath, strDestination
Dim strFileText, strFileText2, strFileText3
strFilePath = "e:\ezine\scripts\ezine36\newsletter36.txt"
strDestination ="e:\ezine\scripts\ezine36\4thJuly.txt"
strFileText = "Today is American Independence day "
strFileText2 = "Next Ezine 11 July"
strFileText3 = " Bye from Guy"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objGuyFile = objFSO.CreateTextFile(strFilePath, True)
objGuyFile.WriteLine(strFileText)
objGuyFile.Write (strFileText2)
objGuyFile.WriteLine (strFileText3)
objGuyFile.Close
WSCript.Echo "Written " & strFileText & " to " & strFilePath
Set objFileCopy = objFSO.GetFile(strFilePath)
' Copy the file to its destination
objFileCopy.Copy (strDestination)
WSCript.Echo "Copied to " & strDestination
Wscript.Quit
' End of example VBScript
Learning Points
Note 1:
Set objFSO = CreateObject("Scripting.FileSystemObject")
builds our file object. Once we have created objFSO, then we can put
it to work and copy or move files. Creating the file is covered in Ezine 35.
Note 2: This is script of two halves, the first half creates the file
called newsletter36.txt and adds text. The second half, (below the
line: WSCript.Echo "Written "), copies the file to a new destination.
Note 3: In addition to the .Copy, spot the .GetFile method which is
essential to 'grasp' the file before it can be copied or deleted.
Challenge 1.
It would more spectacular and realistic to copy the file to another drive
or at least to another folder. So my challenge is to amend
your strDestination.
Challenge 2.
My idea here is to move the newsletter36.txt file rather than copy it.
Change .Copy to .Move; this is what I mean, alter objFileCopy.Copy (strDestination)
to objFileCopy.Move (strDestination).
N.B. You are likely to get an error message,
unless you add the .delete command.
' MoveFSO.vbs ' Example script to move files.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 4.2 - 4th July 2004
' -------------------------------------------------------------'
Option Explicit
Dim objFSO, objFileCopy, objGuyFile, objFileDelete
Dim strFilePath, strDestination
Dim strFileText, strFileText2, strFileText3
strFilePath = "e:\ezine\scripts\ezine36\newsletter36.txt"
strDestination ="e:\ezine\scripts\ezine36\4thJuly.txt"
strFileText = "Today is American Independence day "
strFileText2 = "Next Ezine 11 July"
strFileText3 = " Bye from Guy"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objGuyFile = objFSO.CreateTextFile(strFilePath, True)
objGuyFile.WriteLine(strFileText)
objGuyFile.Write (strFileText2)
objGuyFile.WriteLine (strFileText3)
objGuyFile.Close
WSCript.Echo "Written " & strFileText & " to " & strFilePath
' Delete file if it alread exists in the destination.
Set objFileCopy = objFSO.GetFile(strDestination)
objFileCopy.Delete ()
Set objFileCopy = objFSO.GetFile(strFilePath)
' Move the file destination
objFileCopy.Move (strDestination)
WSCript.Echo "Copied to " & strDestination
Wscript.Quit
' End of example Script
Learning Points
Note 1: If the file already exists then the script would generate an error -
unless you add the .delete command.
Note 2: The .delete command takes a blank () argument. The
syntax is objFileCopy.Delete() not objFileCopy.Delete(strDestination)
Note 3: I suspect that there are other ways of moving and
overriding the existing file. However adding 'True' to the command for
example, : (strDestination,True) did
not work.
This script is likely to generate Windows Script Host error Code: 800A005
Can you spot the sequencing problem and correct the order of instructions and so
cure the problem?
' CopyFSO.vbs ' Example VBScript
' Author Guy Thomas http://computerperformance.co.uk/
' Version 3.3 - 4th July 2004
' -------------------------------------------------------------'
Option Explicit
Dim objFSO, objFileCopy, objGuyFile
Dim strFilePath, strDestination
Dim strFileText, strFileText2, strFileText3
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objGuyFile = objFSO.CreateTextFile(strFilePath, True)
strFilePath = "e:\ezine\scripts\ezine36\newsletter36.txt"
strDestination ="e:\ezine\scripts\ezine36\4thJuly.txt"
strFileText = "Today is American Independence day "
strFileText2 = "Next Ezine 11 July"
strFileText3 = " Bye from Guy"
objGuyFile.WriteLine(strFileText)
objGuyFile.Write (strFileText2)
objGuyFile.WriteLine (strFileText3)
objGuyFile.Close
WSCript.Echo "Written " & strFileText & " to " & strFilePath
Set objFileCopy = objFSO.GetFile(strFilePath)
' Move the file destination
objFileCopy.Copy (strDestination)
WSCript.Echo "Copied to " & strDestination
Answer:
This statement appears BEFORE the strFilePath is initialized.
Set objGuyFile = objFSO.CreateTextFile(strFilePath, True)
Check Example 1 to see the correct line sequencing.
As a general principle, the .dot commands or methods extend what you can
do with a VBScript object. In this particular instance .Copy, .Move
and .Delete enable us to manipulate files.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
|