Create ,Read and write a text file

In this post, we are going to learn how to Create ,Read and write a text file .

Creating a text File:

Create a text File
‘Syntax : object.CreateTextFile(filename[, overwrite[, unicode]])
filename Required. Provide the full path of the file with the file name which you want to create.
overwrite Optional (If true, it will overwrite the file and if false, file will not be overwritten.If omitted, existing files are not overwritten.
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.CreateTextFile("D:\Sample.txt",True)
oFile.Close
Set oFile = Nothing

Write into a text File:

Please read the comments above the code to understand what code is actually doing.

'Write into a file
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.CreateTextFile("D:\Sample.txt",True)
' Writes a specified string to the file
oFile.Write("I am writing to a sample file.")
'Writes no of new line in the file
oFile.WriteBlankLines(2)
'Writes a specified string and newline character into the file.
oFile.WriteLine("This is a second line.")
oFile.Close

Read a text file:

'Reading a file
Set FSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1, ForWriting = 2, ForAppending = 8
'We can also use 'OpenTextFile' to write the data into existing file
'We are opening an existing file 'Sample1.txt' for writing
Set oFile1 = FSO.OpenTextFile("D:\Sample1.txt", ForWriting, True)
oFile1.Write("Hello All!!")
oFile1.Write("Here, first we will write it into the file.")
oFile1.WriteLine
oFile1.Write("Then we will read the data.")
oFile1.Close
'Now open file for reading
Set oFile2 = FSO.OpenTextFile("D:\Sample1.txt", ForReading, True)
'AtEndOfStream - Returns true if the file pointer is at the end of a TextStream file; false if it is not
Do Until oFile2.AtEndOfStream = True
	print oFile2.ReadLine
Loop 
oFile2.Close

Difference between AtEndOfStream and AtEndOfLine:

AtEndOfStream Returns true if the file pointer is at the end of a TextStream file; false if it is not
AtEndOfLine Returns true if the file pointer is at the end of a line in a file; false if it is not

Can we read all content at a time,if not line by line?
Yes,We can do this by using the method ReadAll.
oFile.ReadAll

Can we read character by character?
Yes,We can do this by using the method Read.
oFile.Read(no of character to read)

Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

2 Responses

  1. Monika says:

    After writing code for creating and adding I am not getting results either pass or fail.I have applied breakpoint also but it is not stopping there.Could you help
    Set obj = CreateObject(“Scripting.FileSystemObject”)
    Set fo = obj.CreateTextFile(“C:\write.txt”,2,True)
    fo.write(“ABC”)
    fo.Close

    • In this case, You might not be able to create a file directly in C drive. As a solution, you can either create a folder in C driver first and then provide its name in the path OR provide a different path in CreatetextFile.\
      Example – Create a folder name in C drive named ‘tmp’ and use the command as below.
      Set fo = obj.CreateTextFile(“C:\tmp\write.txt”,True)