VBScript – Working with file properties

This tutorial will show how to work with files in VBScript using the FileSystemObject provided with the built-in Windows Script Host.

For the purpose of demonstrating the different methods on the files object, I have created a test folder in the root of the c-drive on my computer.
The content of this folder can be seen in the picture below.

The picture shows the content of the test folder.
The folder contains two subfolders called SubFolder1 and SubFolder2 and three files called Document1.txt, Document2.txt and Document3.txt respectively.

To see the script in action, create the folder ‘C:\Test’ and create the two subfolders and the three files as shown in the picture.
Copy the script code in the box below and paste it into an empty .txt file and save it with the .vbs extension somewhere on your hardisk.
The below code will be explained in more detail later.

[tab:The Script]

Note: Copy all the lines in a unique "text file" renamed with the .vbs extension.

Option Explicit
Dim objFSO, objFile, strFileProperties, colFiles, strFiles
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("C:\Test\Document1.txt") Then
Set objFile = objFSO.GetFile("C:\Test\Document1.txt")

' Display generel file properties

strFileProperties = strFileProperties & "File name: " & objFile.Name & VbCrLf
strFileProperties = strFileProperties & "File path: " & objFile.Path & VbCrLf
strFileProperties = strFileProperties & "Folder placed on drive: " & objFile.Drive & VbCrLf
strFileProperties = strFileProperties & "Date created: " & objFile.DateCreated & VbCrLf
strFileProperties = strFileProperties & "Date last accessed: " & objFile.DateLastAccessed & VbCrLf
strFileProperties = strFileProperties & "Date last modified: " & objFile.DateLastModified & VbCrLf
strFileProperties = strFileProperties & "Parent folder: " & objFile.ParentFolder & VbCrLf
strFileProperties = strFileProperties & "File size: " & objFile.Size & " bytes" & VbCrLf
strFileProperties = strFileProperties & "File type: " & objFile.Type & VbCrLf
MsgBox strFileProperties
Else
MsgBox "Selected file does not exist!"
End If

' Make a copy of the file

Set objFile = objFSO.GetFile("C:\Test\Document1.txt")
objFile.Copy "C:\Test\Document1_Copy.txt", True

' Delete the origional file

Set objFile = objFSO.GetFile("C:\Test\Document1.txt")
objFile.Delete True

' Move another file

Set objFile = objFSO.GetFile("C:\Test\Document2.txt")
objFile.Move "C:\Test\SubFolder1\"

When the script is executed you will see output like the following:

After running the script the content of the “C:\Test” folder should now look like:

And the content of the subfolder called SubFolder1 should now contain the Document2.txt file which was moved from the root folder to the subfolder.

Ok, so what happens in the script?
The first two lines of the script is used to declare the variables used though out the script.

On line 3 we setup a FileSystemObject which is used to setup references to the files and invoke their methods in the remaining of the script.

Option Explicit
Dim objFSO, objFile, strFileProperties, colFiles, strFiles
Set objFSO = CreateObject("Scripting.FileSystemObject")

[tab: File Properties]

Display general file properties using VBScript

Next, we use the FileSystemObject to make a check to see if the specified file actually exists before we do anything with the file.
If the file exists we setup a VBScript file object using the GetFile method on the FileSystemObject.

Then the string variable called strFileProperties is populated with all the file information and finally printed out in a message box.

The file information presented in the message box is generated from the following piece of the script.

[tab:File Copy]

Make a copy of a file with VBScript

Next the script will make a copy of the file called Document1.txt and name the copy Document1_Copy.txt

[tab:File Delete]

Deleting the original file

After copying the file the script will try to delete the original file with the following code.

We set up a reference to the file Document1.txt and then tries to delete the file.
Note that the file will actually try to delete itself using the Delete method on its own object

[tab:FIle Move]

Moving a file with VBScript

Finally the script will move the Document2.txt file to the subfolder called SubFolder1.

Again we are using the files own Move method.

[tab:FileSystemObject]

Working with files using the FileSystemObject

The above mentioned file operations could have been accomplished by using the available methods on the FileSystemObject itself like:

[tab:END]

SOURCE

LINK

LANGUAGE
ENGLISH