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.

Write into a text File:

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

Read a text file:

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.

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

Send mail from outlook with an attachment
Use of FileSystemObject
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of 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)

    • 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)

Leave a Reply

Your email address will not be published. Required fields are marked *