VBScript FileSystem Objects

As the name suggests, FSO Objects help the developers to work with drives, folders and files. In this section, we will discuss −
Drive
Drive is an Object. Contains methods and properties that allow you to gather information about a drive attached to the system
Drives
Drives is a Collection. It Provides a list of the drives attached to the system, either physically or logically.
File
File is an Object. It Contains methods and properties that allow developers to create, delete or move a file.
Files
Files is a Collection. It Provides a list of all files contained within a folder.
Folder
Folder is an Object. It Provides methods and properties that allow developers to create, delete or move folders.
Folders
Folders is a Collection. It Provides a list of all the folders within a Folder.
TextStream
TextStream is an Object. It enables developers to read and write text files.
Drive is an object, which provides access to the properties of a particular disk drive or network share. The Following properties are supported by Drive object −
AvailableSpace
DriveLetter
DriveType
FileSystem
FreeSpace
IsReady
Path
RootFolder
SerialNumber
ShareName
TotalSize
VolumeName
<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim oFS, drive, space
         Set oFS = CreateObject("Scripting.FileSystemObject")
         Set drive = oFS.GetDrive(oFS.GetDriveName("C:\"))
         space = "Drive " & UCase(drvPath) & " - " 
         space = space & drive.VolumeName   & "  "
         space = space & "Free Space: " & FormatNumber(drive.FreeSpace/1024, 0) 
         space = space & " Kbytes"
         Document.write space

      </script>
   </body>
</html>
If the above script is saved as HTML and executed in IE, we would get the following output in the console.
Drive - Win 7 Free Space:20,154,059 Kbytes

Drives is a collection, which provides details of all the drives attached to the system, either physically or logically. It carries two properties −
Count Property
Item Property
<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim oFS, d, dc, s, n
         Set oFS = CreateObject("Scripting.FileSystemObject")
         Set dc = oFS.Drives
         
         For Each d in dc
            n = ""
            s = s & d.DriveLetter & " - " 
            If d.DriveType = 3 Then
               n = d.ShareName
            ElseIf d.IsReady Then
               n = d.VolumeName
            Else
               n = "Drive not available"
            End If
            s = s & n & "::"
         Next
         
         document.write s
         document.write dc.count
         
      </script>
   </body>
</html>
If the above script is saved as HTML and executed in IE, we would get the following output in the console.
C- Win 7::D-Personal ::E-Common::F-Songs::
4

File is an Object, which contains both properties and methods that allow the developers to create, delete or move a file.
Copy
Delete
Move
openasTextStream
Attributes
DateCreated
DateLastAccessed
DateLastModified
Drive
Name
ParentFolder
Path
ShortName
ShortPath
Size
Type
<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim fso, f
         Set fso = CreateObject("Scripting.FileSystemObject")
         Set f = fso.GetFile("C:\user.js")
         document.write "Line 1: "& f.DateCreated & "<br />"
         document.write "Line 2: "& f.Attributes & "<br />"
         
         document.write "Line 3: "& f.DateLastAccessed & "<br />"
         document.write "Line 4: "& f.DateLastModified & "<br />"
         
         document.write "Line 5: "& f.Drive  & "<br />"
         document.write "Line 6: "& f.Name  & "<br />"
         
         document.write "Line 7: "& f.ParentFolder & "<br />"
         document.write "Line 8: "& f.Path  & "<br />"
         
         document.write "Line 9: "& f.ShortName  & "<br />"
         document.write "Line 10: "& f.ShortPath & "<br />"
         
         document.write "Line 11: "& f.Size  & "<br />"
         document.write "Line 12: "& f.Type & "<br />"
         
         f.copy ("D:\") & "<br />"    'copying to file to another location'
         f.Move ("E:\") & "<br />"   'Move the file to another location'
         f.Delete ("D:\") & "<br />"  'Delete to file from one location'

      </script>
   </body>
</html>
If the above script is saved as HTML and executed in IE, we would get the following output in the console.
Line 1: 08/02/13 06:57:34
Line 2: 32
Line 3: 08/02/13 06:57:34
Line 4: 04/18/12 22:23:37
Line 5: C:
Line 6: user.js
Line 7: C:\
Line 8: C:\user.js
Line 9: user.js
Line 10: C:\user.js
Line 11: 474
Line 12: JScript Script File

Files is a collection, which provides a list of all files contained within a folder.
Count
Item
<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim fso, f, f1, fc, s
         Set oFS = CreateObject("Scripting.FileSystemObject")
         
         'get the folder by giving its path
         Set f = oFS.GetFolder("D:\PROJECT\")
         Set fc = f.Files
         
         'Get Item
         Set s = fc.Item("sendmail.vbs")
         
         'Get Count
         x = fc.Count
         
         Document.write s
         Document.write x

      </script>
   </body>
</html>
If the above script is saved as HTML and executed in IE, we would get the following output in the console.
D:\PROJECT\sendmail.vbs
6

Folder is an Object, which contains both properties and methods that allow the developers to create, delete or move a folder.
Copy
Delete
Move
CreateTextFile
Attributes
DateCreated
DateLastAccessed
DateLastModified
Drive
Files
IsRootFolder
Name
ParentFolder
Path
ShortName
ShortPath
Size
SubFolders
Type
<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim fso, f
         Set fso = CreateObject("Scripting.FileSystemObject")
         
         ' Enter a Folder Name that exists on your System'
         Set f = fso.GetFolder("D:\PROJECT\")
         
         ShowFileInfo = "Created: " & f.DateCreated & "<br / >"
         document.write ShowFileInfo
         
         ShowFileInfo = "attributes " & f.attributes & "<br / >"
         document.write ShowFileInfo
         
         ShowFileInfo = "Last Accessed : " &  f.DateLastAccessed & "<br / >"
         document.write ShowFileInfo

         ShowFileInfo = "DateLastModified : " & f.DateLastModified & "<br / >"
         document.write ShowFileInfo

         ShowFileInfo =  "Drive : " &  f.Drive & "<br / >"
         document.write ShowFileInfo

         ShowFileInfo =   "count : " &  f.Files.count & "<br / >"
         document.write ShowFileInfo

         ShowFileInfo = "IsRoot folder : "  &f.IsRootFolder   & "<br / >" 
         document.write ShowFileInfo

         ShowFileInfo =  "Name : " & f.Name    & "<br / >"
         document.write ShowFileInfo

         ShowFileInfo =  "parent folder : " & f.ParentFolder    & "<br / >"
         document.write ShowFileInfo

         ShowFileInfo =  "Path : " & f.Path    & "<br / >"
         document.write ShowFileInfo

         ShowFileInfo =  "shortname : " & f.ShortName    & "<br / >"
         document.write ShowFileInfo

         ShowFileInfo =  "ShortPath : "  & f.ShortPath    & "<br / >"
         document.write ShowFileInfo

         ShowFileInfo =  "File Size : " & f.Size & "<br / >"   
         document.write ShowFileInfo

         ShowFileInfo = "Type : " &  f.Type    & "<br / >"
         document.write ShowFileInfo
         
      </script>
   </body>
</html>
If the above script is saved as HTML and executed in IE, we would get the following output in the console.
Created: 22/02/2012 8:24:57 PM
attributes 16
Last Accessed : 1/08/2013 12:48:36 PM
DateLastModified : 1/08/2013 12:48:36 PM
Drive : D:
count : 6
IsRoot folder : False
Name : PROJECT
parent folder : D:\
Path : D:\PROJECT
shortname : PROJECT
ShortPath : D:\PROJECT
File Size : 8655239975
Type : File folder

Folders is an collection of all Folder Objects within a Folder object.
Add
Count
Item
If the above script is saved as HTML and executed in IE, we would create a folder with name "Test_Folder".
<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim fso, f, fc, nf
         Set fso = CreateObject("Scripting.FileSystemObject")
         Set f = fso.GetFolder("D:\PROJECT")
         Set fc = f.SubFolders
         folderName = "Test_Folder"
         
         If folderName <> "" Then
            Set nf = fc.Add(folderName)
         Else
            Set nf = fc.Add("New Folder")
         End If
         
      </script>
   </body>
</html>
TextStream object helps the developers to work with text files seamlessly. Developers can read, write or append the contents to the text file using the text stream object.
TextStream.{property  | method( )}

If the above script is saved as HTML and executed in IE, we would create a folder with name "Test_Folder".
<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim objFSO
         Set objFSO = CreateObject("Scripting.FileSystemObject") 
         
         Dim objTextFile 
         Set objTextFile = objFSO.CreateTextFile("D:\Testfile.txt") 
         
         objTextFile.Close
         Const ForAppending = 8
         Set objTextFile = objFSO.OpenTextFile("D:\Testfile.txt",ForAppending,True)
         
         objTextFile.WriteLine "Welcome to VBScript Programming"
         objTextFile.Close
         Set objTextFile = Nothing 
         Set objFSO = Nothing 
         
      </script>
   </body>
</html>
If the above script is saved as HTML and executed in IE, it will create a text file in D:\ Drive and append the string specified in the WriteLine Method.
Welcome to VBScript Programming


    63 Lectures 
     4 hours 

 Frahaan Hussain
 Print
 Add Notes
 Bookmark this page