Guy’s Scripting Ezine 36 – FSO Copy (Part 2)

Contents for Guy’s Scripting Ezine 36 – FSO Copy (Part 2)

Instructions for VBScript file copy operatioon.

This Week’s Secret

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.

Example 1 Create a text file, then copy it to a new path.

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

  1. 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.
  2. Copy and paste the main script into notepad.  Alternatively, use a script editor like OnScript.
  3. Save the file with .vbs extension e.g. CopyFSO.vbs
  4. Double click and observe the file that you have just created and copied.
  5. 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 – Use VBScript to Create a Text File, Then Copy It to a New Path

‘ CopyFSO.vbs
‘ Example VBScript for manipulating files.
‘ Author Guy Thomas https://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.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Orion performance monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Example 2 – Move a file, deleting the old one

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 https://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.

Guy Recommends: SolarWinds Free Wake-On-LAN UtilitySolarwinds Wake-On-LAN

Encouraging computers to sleep when they’re not in use is a great idea – until you are away from your desk and need a file on that remote sleeping machine!

WOL also has business uses for example, rousing machines so that they can have update patches applied.  My real reason for recommending you download this free tool is because it’s so much fun sending those ‘Magic Packets’. Give WOL a try – it’s free.

Download your free copy of SolarWinds Wake-On-LAN

Guy’s Challenge – Try and solve this ‘Out take’

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 File Copy
‘ Author Guy Thomas https://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.

Summary of File System Object

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.

See more about scripting files with VBScript

VBScripts  • Ezines  • WMI  • Create files  • File Open  • Ezine 12 FSO  • Permissions Analyzer

• Ezine 35 FSO  • Ezine 36 FSO II  • Ezine 43 File Create  • PowerShell Create Files

Ezine 78 Files  •Ezine 79 Files  • Ezine 104 FSO  •Ezine 139 Delete Temporary