Learn VBA : FSO ( File System Object )

Recently I have been asked several times about the File System Object or FSO as it can be called. In this article we will look at the basic things that you may need to do when using this object in your scripts.

First off lets create some variables

<

>Dim oFSO,oFile
Dim sFileNameOnly,sFileName,sPath>

next we will give them some values. When using files it is always a great idea to set up these variables in the first place as it just makes your

 allot cleaner and easier to handle.

<

>sPath = ""C:\MyOutputFiles""
sFileName = sPath + "\MyFile.txt">

So to get started we must make the FSO object. We do this using the CreateObject command as shown,

<

>Set oFSO = CreateObject("Scripting.FileSystemObject")>

Once we have created the object correctly when we type oFSO. we will see that the script assist pops up and we can see all the available procedures and properties that the object allows us access to.

Ok so what do we normally do first with the FSO. For me its check to see if a folder exists and to help us discover this we can use the "FolderExists" function. This function takes 1 parameter , the path and the result is a true or a false. False means the folder specified is not there.

<

>If oFSO.FolderExists(sPath) = False Then
oFSO.CreateFolder(sPath)
End If>

You will see that in the

 above if the folder does not exist we use the "CreateFolder" function to make the folder in the desired location. Once we have checked and possibly created the folder that we want to work with we then might want to create a file in that folder. To do this we can use the "CreateTextFile" function that takes two parameters the first is the name of the file and the second is a Boolean value that if it is set to true then the file will be created.

<

>Set oFile = oFSO.CreateTextFile(sFileName, True)>

Once the File object is created we can then write a line of text into the file using the "WriteLine" function that takes one parameter which is the text you want to place in the file.

<

>sFileNameOnly = oFSO.GetBaseName(sFileName)
oFile.WriteLine("This is the file that will be copied.")
oFile.WriteLine("This is the filename only value : " + sFileNameOnly)
oFile.Close()>

Set oFile = Null

You will see from the

 I have used that I have also used the "GetBaseName" function. This is a really useful function to pull out the filename from a path variable. You should also see that after I have written all I need to my file I have closed it using the "close" function. This then allows other users to use the file as required. Once we have closed the file we need to destroy the file object and release the memory that it has used. To do this we set it to Null.

Another great function is the "CopyFolder" function. This takes three parameters, the first is the location of the original folder, the second is the destination folder and the third parameter is a Boolean that if it is set to true then it will overwrite any destination files.

<

>oFSO.CopyFolder(sPath,"D:\",true)>

The last thing that I use frequently is the "FileExists" function this takes one parameter and like the "FolderExists" function takes one parameter which is the location and filename of the file that you want to check. If the file does exist then the function will return a true value.

<

>If oFSO.FileExists(sFileName) = True Then
oFSO.DeleteFile(sFileName)
End If>

Set oFSO = Null

In the above example you can also see that I have used the "DeleteFile" function to remove the file if it does exist. Also note that the last thing we do is to close the FSO object by setting it to null.

The last thing that I would like to add to all of this is that in Author Professional. If you open up Professional and click File / New / From template you will see the following screen.

New File from template

Leave a Comment