How to create a text file using VBScript – CreateTextFile

If you have to create a text file using VBScript, it provides a very efficient tool FileSystemObject. Further, there are many methods provided by FileSystemObject (also known as FSO) to perform file and folder-related operations. One such method is CreatTextFile. Let’s see how to use the method CreateTextFile to create a text file using VBScript.

CreateTextFile – is the method provided by FileSystemObject to create a text file using VBScript. This page has all the basic functions for file handling using fso in VBScript. VBScript function for files using FSO is very important when working on any automation project with tools that support VBScript such as UFT or QTP

How to create a text file using VBSCript

'--------------------------------------------------------------
'Method 1: CreateTextFile
'--------------------------------------------------------------

'Create a filesystemObject
Set fso=createobject("Scripting.FileSystemObject")
'Create a non existing file “qtptest.txt ”  with overwrite option as True
Set qfile1=fso.CreateTextFile("C:qtptest.txt",True,False)
'Output –> New File “qtptest.txt ” is created
'Close the files
qfile1.Close
'Release the allocated objects
Set qfile1=nothing 
'--------------------------------------------------------------
'Method 2: CreateTextFile
'--------------------------------------------------------------
'Create a filesystemObject
Set fso=createobject("Scripting.FileSystemObject")
'Create a   file "qtptest.txt "  in C Drive .
'Then  run the below statement with overwrite option as False
 'Output –> Error message “Fie already exists” is displayed
Set qfile2=fso.CreateTextFile("C:\qtpexist.txt",False,False)
Set fso=nothing

How to Delete a file using fso in VBScript

Set fso=createobject("Scripting.FileSystemObject")
'File to be  deleted.  Sourcefile=”C:copy.txt”  ‘Delete the file
fso.DeleteFile Sourcefile
Set fso=nothing

How to Check if the file exists

Set fso=createobject("Scripting.FileSystemObject")
'The file to check the existence
filepath= "D:\qtptest.txt"
If fso.FileExists(filepath) then
 msgbox  "File Exists"
Else
 Msgbox "File doesnot Exist"
End If
Set fso=nothing

You may also read


Discover more from Automation Script

Subscribe to get the latest posts sent to your email.

Related Posts