Introduction to VBS File Objects: Tutorial #13
My previous tutorial briefed about ‘Connection Objects’ in the VBScript. In this tutorial, I will explain to you about VBS File Objects like VBScript CopyFile, DeleteFile, OpenTextFile, Read Text File, and Write to Text File.
=> Check the list of complete VBScipting tutorials under this series.
VBScript supports different types of objects and File Objects are among them. The objects which provide support to the Coders to work and deal with the files are known as file objects.
This tutorial gives you a complete overview of working with File Objects in the VBScript along with easy examples for your better understanding.
I decided to pick this as one of the topics in the series of the VBScript tutorial just because of its importance. File Object forms the basis of working with files
I will try to make you understand all the different codes, properties, and methods that are required to be written to work with the files in an easy manner so that you can easily write a piece of code by yourself.
Let’s start with the Properties and Methods which will be used while working with the files by providing access to handle operations like create, delete, move, etc., on a file. Hence, it is too important to gain an understanding of these before moving into the coding part.
Table of Contents:
- Properties and Methods of a File Object
- Copying a File Using File Object
- Deleting a File Using File Object
- Moving a File Using File Object
- Creating a File Using File Object
- Opening a Text File and Writing the Text Using File Object
- Reading from a Text File Using File Object
- Conclusion
- Was this helpful?
- Recommended Reading
Properties and Methods of a File Object
There are different properties and methods that support performing operations on a file.
The list of properties is as follows:
- Attributes: This is used to know the number of attributes supported by a particular file.
- DateCreated: This returns the date along with the time of the creation of a particular file.
- DateLastAccessed: This returns the date along with the time when a particular file was last accessed.
- DateLastModified: This returns the date along with the time when a particular file was last modified.
- Drive: This returns the drive in which the particular file is located.
- Name: This returns the name of a particular file.
- ParentFolder: This returns the parent folder of a particular file as if the file is stored in a C drive so it will return C:\.
- Path: This returns the path of a particular file as if the file is stored in a C drive and the name of the file is a test so it will return C:\test.txt.
- Size: This returns the size of a particular file in bytes.
- Type: This returns the type of a particular file i.e. file type description like a file which ends with .vbs, for that “VBScript” will be returned.
These are the properties of a File Object. There is a Files Object also (this is a collection of a file objects) and let’s see its properties as follows:
- Item: This property is used to know the value of an item that is passed as a parameter. When a particular filename is passed as an item then this will return the full name of the file including the location of the file.
- Count: This is used to know the count of the File objects that are present in the collection.
Now, let’s move to Methods.
A list of some of the important File related methods is as follows:
- CopyFile/CopyFolder: This is used to copy the mentioned file/folder to a specific destination.
- DeleteFile/DeleteFolder: This is used to delete a particular specified file/folder.
- MoveFile/MoveFolder: This is used to move the particular file/folder to the new destination as specified.
- OpenTextFile: This is used to open the file that is specified as a parameter and it returns as an instance of a text stream so that it can behave like a text file and operations of reading, writing & appending can be performed on that. If you want to open a text file for reading only then you can pass the constant values 1, 2 in case of writing, and 8 for appending purposes.
- CreateTextFile: This is used to create a text file that is specified as a parameter and it returns as an instance of a text stream so that it can behave like a text file and the operations of reading, writing, etc., can be performed.
- FileExists/FolderExists/DriveExists: This is used to check if the mentioned file/folder/drive exists or not. This returns True if it exists else False.
- GetFile/GetFolder/GetDrive: This is used to get the file/folder/drive object of the mentioned file/folder/drive which is specified as a parameter.
The above-mentioned are the different properties and methods that you will use while dealing with File Objects.
Now, let’s move on to the practical implementation and see the working of these objects.
Copying a File Using File Object
Following is the Code for copying a file:
Set obj = createobject(“Scripting.FileSystemObject”) ‘Creating a File Object
Dim loc,loc1 ‘Declaring variables
src=”C:\app\pictures\img1.jpg” ‘Mentioning source location of the file to be copied
dest=”C:\app1” ‘Mentioning the destination
obj.CopyFile src,dest ‘CopyFile Method is used for copying the file
Set obj=Nothing ‘Releasing File object
Let’s see how it works:
- Firstly, a File Object with the name ‘obj’ is created using ‘createobject’ keyword and the File System Object in the parameter is defined.
- Then, variables are declared for mentioning the destination and source location of the file to be copied.
- A CopyFile method is then used to copy the source file to the destination mentioned above.
- Finally, the object – obj is released by using a ‘Nothing’ keyword.
Deleting a File Using File Object
Following is the Code for deleting a file:
Set obj = createobject(“Scripting.FileSystemObject”) ‘Creating a File Object
Dim filename1 ‘Declaring variables
filename1=”C:\app\pictures\img1.jpg” ‘Mentioning name and location of the file to be deleted
obj.DeleteFile filename1 ‘DeleteFile Method is used for deleting the file
Set obj=Nothing ‘Releasing File object
Let’s see how it works:
- Firstly, a File Object with the name ‘obj’ is created using ‘createobject’ keyword and the File System Object in the parameter is defined.
- Then, the variable is declared for mentioning the location of the file which has to be deleted.
- A DeleteFile method is then used to delete the file.
- Finally, the object – obj is released by using a ‘Nothing’ keyword.
Moving a File Using File Object
Following is the Code for moving a file:
Set obj = createobject(“Scripting.FileSystemObject”) ‘Creating a File Object
Dim filename1,filename2 ‘Declaring variables
filename1=”C:\app\pictures\img1.jpg” ‘Mentioning the name and source location of the file to be moved
filename2=”C:\Users\img1.jpg” ‘Mentioning the name and destination location of the file to be moved
obj.MoveFile filename1,filename1 ‘MoveFile Method is used for moving the file to the destination
Set obj=Nothing ‘Releasing File object
Let’s see how it works:
- Firstly, a File System Object with the name ‘obj’ is created using ‘createobject’ keyword and File System Object in the parameter is defined.
- Then, the variables are declared by mentioning both the source location and the destination location of the file to be moved.
- A MoveFileFile method is then used to move the file.
- Finally, the object – obj is released by using a ‘Nothing’ keyword.
Creating a File Using File Object
Following is the Code for creating a text file:
Set obj = createobject(“Scripting.FileSystemObject”) ‘Creating a File Object
src=”C:\Users\Riya\file1.txt” ‘Mentioning name and location of the file to be created
obj CreateTextFile src ‘CreateTextFile Method is used for creating the file
Set obj=Nothing ‘Releasing File object
Let’s see how it works:
- Firstly, a File Object with the name ‘obj’ is created using ‘createobject’ keyword and File System Object in the parameter is defined.
- Then, the variable is declared for mentioning the name and location of the file which has to be created.
- A CreateTextFile method is then used to create the file that is mentioned above.
- Finally, the object – obj is released by using a ‘Nothing’ keyword.
Note: In the same way, a Folder can be created, deleted, and copied using CreateFolder, DeleteFolder, and CopyFolder methods respectively.
Opening a Text File and Writing the Text Using File Object
Following is the Code for writing text inside a file:
Set obj = CreateObject(“Scripting.FileSystemObject”) ‘Creating a File Object
Const ForWriting = 2 ‘Defining Constant Value to write in a file
Set obj1 = obj.OpenTextFile(“C:\app.txt”, ForWriting) ‘Opening a text file and writing text inside it
obj1.WriteLine(“This text is written in a file”) ‘Text is written using WriteLine method
obj1.Close ‘Closing a File
Set obj=Nothing ‘Releasing File object
Let’s see how it works:
- Firstly, a File Object with the name ‘obj’ is created using ‘createobject’ keyword and the File System Object in the parameter is defined.
- A constant value is then defined for writing purposes as VBScript cannot automatically access the COM objects and so it is required to define a constant value to pass a writing parameter value in the OpenTextFile method.
- Then, a text file is opened using ‘OpenTextFile’ method and the writing operation is performed.
- A text is then written inside a file.
- Then, the File is closed.
- Finally, the objects – obj and obj1 are released by using a ‘Nothing’ keyword.
Note: In the same way, Appending operation can also be performed on a file by defining the constant value as 8.
Reading from a Text File Using File Object
Following is the Code for reading text from a file:
Set obj = CreateObject(“Scripting.FileSystemObject”) ‘Creating a File Object
Const ForReading = 1 ‘Defining Constant Value to read from a file
Set obj1 = obj.OpenTextFile(“C:\app.txt”, ForReading) ‘Opening a text file and reading text from it
Dim str,str1
str=obj1.ReadAll ‘All text from the file is read using ReadAll
Msgbox str ‘Contents of a file will be displayed through the message box
Do while obj1.AtEndofStream ‘Reading text line wise using Do Loop and ReadLine
str1=obj1.ReadLine
Msgbox str1
Loop
obj1.Close ‘Closing a File
Set obj=Nothing ‘Releasing File object
Let’s see how it works:
- Firstly, a File Object with the name ‘obj’ is created using ‘createobject’ keyword and the File System Object in the parameter is defined.
- A constant value is then defined for the reading purpose as VBScript cannot automatically access the COM objects. Hence it is required to define a constant value to pass a reading parameter value in the OpenTextFile method.
- Then, a text file is opened using ‘OpenTextFile’ method and the reading operations are performed.
- A whole text is then read from a file using ‘ReadAll’.
- Another way of reading from a file is line-wise. Do loop is used to read a text from a file line-by-line using ‘ReadLine’.
- Then, the File is closed.
- Finally, the objects – obj and obj1 are released by using a ‘Nothing’ keyword.
These are some of the prime scenarios which should be understood properly. They form the foundation to work and deal with the codes for handling different types of scenarios while dealing with File Objects in the script.
Given below are the different types of Examples by taking reference to the above scenarios and topics.
Example1:
Making use of ‘Count’ and ‘Item’ properties of ‘Files’ object along with ‘GetFolder’ method
<html> <head> <title>Let’s see implementation of Files Object with properties</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim obj, obj1, obj2, itm, cnt Set obj= CreateObject(“Scripting.FileSystemObject”) Set obj1=obj.GetFolder(“C:\Users\Riya”) Set obj2=obj1.Files itm=obj2.Item(“riya.vbs”) cnt=obj2.Count Msgbox(itm) Msgbox(cnt) </script> </body> </html>
Output is: C:\Users\Riya\riya.vbs
6 (assuming a total of 6 files available in a folder)
Example2:
Making use of different properties and methods of a ‘File’ object
<html> <head> <title>Let’s see implementation of a File Object</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim obj, obj1 Set obj= CreateObject(“Scripting.FileSystemObject”) Set obj1=obj.GetFile(“C:\Users\Riya.vbs”) Msgbox “DateCreated of File is “& obj1.DateCreated & “<br>” Msgbox “Attributes of File is “& obj1.Attributes & “<br>” Msgbox “DateLastAccessed of File is “& obj1.DateLastAccessed & “<br>” Msgbox “DateLastModified of File is “& obj1.DateLastModified & “<br>” Msgbox “Name of File is “& obj1.Name & “<br>” Msgbox “Drive of File is “& obj1.Drive & “<br>” Msgbox “ParentFolder of File is “& obj1. ParentFolder & “<br>” Msgbox “Path of File is “& obj1. Path & “<br>” Msgbox “Size of File is “& obj1. Size & “<br>” Msgbox “Type of File is “& obj1. Type & “<br>” obj1.Copy “C:\Users\Riya.vbs”,”C:\Users” obj1.Move “C:\Users\Riya.vbs”,”D:\” obj1.Delete </script> </body> </html>
Note: I am using Copy, Move and Delete methods instead of CopyFile, MoveFile and DeleteFile because direct reference of a particular file is used and is saved in ‘obj1’.
Output is: DateCreated of File is 30/12/2017 04:04:28
Attributes of File is 20
DateLastAccessed of File is 30/11/2017 02:04:38
DateLastModified of File is 11/10/2017 06:06:48
Name of File is Riya.vbs
The drive of File is C:
ParentFolder of File is C:\
Path of File is C:\Users\Riya.vbs
Size of File is 600
Type of File is VBScript Script File
Conclusion
We explained the importance and effectiveness of using VBScript File Objects which in turn would help you in working with the file-related scenarios in an easy manner.
Next Tutorial #14: I will cover the ‘VBScript Error Handling’ concept in my next tutorial.