Tutorials: Using the File System Object

229,789pages on
this wiki
Add New Page
Discuss this page0 Share


The File System Object gives access to various file management and information functions.

The following will hopefully give you some clues to using the FSO with DesktopX.

Getting the FSO 'into' a DesktopX Object

To start using the FSO you need to give your script access to it by loading it in using the VBScript Set function.

When I am finished with an object which is loaded using the Set command, I make a habit of setting it back to Nothing when I'm done with it.

Dim fso

Sub your_fso_subroutine
 Set fso = CreateObject("Scripting.FileSystemObject")

 'Your fso commands here

 Set fso = Nothing
End Sub

Playing with a single file object

Dim fso

Sub your_fso_subroutine
 Set fso = CreateObject("Scripting.FileSystemObject")

 'Use the fso.CreateTextFile method to create a file for our script purposes  

 'Create a file object that points to this file.
 Set o_file = fso.GetFile("c:\testfile.txt")
 'o_file now contains an object reference to the file specified in the quotes.
 'you can call on the properties of the file using 'o_file' similar to how you would with a DesktopX object.  
 'Some properties
 name = o_file.Name                  'returns file name as string
 date_created = o_file.DateCreated   'returns date created as string
 'A method
 o_file.Copy("c:\windows\temp\")     'makes a copy of the file linked to o_file to the quoted directory
 Set o_file = Nothing
 Set fso = Nothing
End Sub

There are many other properties and methods you can access through an FSO file object. You can do the same with individual folders using the GetFolder method.

Using the fso. object on it's own

You don't need to create a file or folder object to carry out queries/commands. The fso object has a number of methods and properties of it's own you can invoke to carry out file management tasks.

The following creates a text file, checks if it exists and if it does, copies it to the windows directory, then deletes both copies.

Dim fso

Sub your_fso_subroutine
 Set fso = CreateObject("Scripting.FileSystemObject")
 fso.CreateTextFile("c:\testfile.txt", True)
 If fso.FileExists("c:\testfile.txt") = True then 
  fso.CopyFile "c:\testfile.txt","c:\windows\"
 End If

 Set o_file = Nothing
 Set fso = Nothing
End Sub

FSO MSDN library

Although it can be a little overwhelming, all the FSO methods and properties, as well as the subset objects (like a file or folder object) that can be created with an FSO object are documented on the MSDN library

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.