CIS Logo SVC Logo

   Computing & Information Systems
   Department

 

Schoology Facebook        Search CIS Site      Tutorials

Using the WSH FileSystem Object


The following example script shows how the FileSystemObject can be used to manipulate files and folders. The FolderExists method is used to check if the backup folder already exists. If not, the CreateFolder method is used to create it. The FileExists method is used to see if a particular file exists. If the desired file exists, it is copied with the CopyFile method. Similarly, the CopyFolder method is used to copy an entire folder. Note that the file names are built up by concatenating various strings with the & operator.


' Filename:  backup.vbs
'
' Author:  Br. David Carlson
'
' Date:  August 4, 2001
'
' This WSH script backs up two files and one folder to M:\Backup.

Dim fso
Dim source1
Dim source2
Dim fileA
Dim fileB
Dim dir
Dim bak

set fso = Wscript.CreateObject("Scripting.FileSystemObject")
source1 = "C:\Courses\Dept\"
source2 = "C:\Courses\"
fileA = "budget2000.xls"
fileB = "agreement.doc"
dir = "Grant"
bak = "M:\Backup"

if Not fso.FolderExists(bak) then
   fso.CreateFolder bak
end if

if fso.FileExists(source1 & fileA) then
   fso.CopyFile source1 & fileA, bak & "\" & fileA
end if

if fso.FileExists(source1 & fileB) then
   fso.CopyFile source1 & fileB, bak & "\" & fileB
end if

if fso.FolderExists(source2 & dir) then
   fso.CopyFolder source2 & dir, bak & "\" & dir
end if

Further Details on FileSystemObject


There is much more that can be done with the FileSystemObject. It gives access to drives, folders, and files. You can move, copy, and delete files and folders. You can get information such as the amount of free space left on a given drive and the time stamp on a file, you can read and write lines of a text file, etc. For further information see the MSDN FileSystemObject page.

Back to main Windows Script Host page



Author: Br. David Carlson
Last updated: February 11, 2007
Disclaimer