Open In App

ASP CreateTextFile() Method

Last Updated : 24 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The ASP CreateTextFile Method is used to create a new text file in the current folder or directory. It returns a TextStream object that can be used to perform operations on the file.

Syntax:

  • For Folder Object:

    FolderObject.CreateTextFile(filename, overwrite, unicode)
  • For FileSystem Object:

    FileSystemObject.CreateTextFile(filename, overwrite, unicode)

Parameters: This property has three parameters as mentioned above and described below:

  • filename: It specifies the name of the text file to be newly created.
  • overwrite: It contains a boolean value that indicates whether an existing file can be overwritten. The true value indicates that the file can be overwritten and false indicates that no overwriting is allowed. The default value is true. It is an optional parameter.
  • unicode: It contains a boolean value that indicates whether the file is created as a Unicode or ASCII file. The true value indicates that the file is created as a Unicode file and a false value indicates that it is created as an ASCII file. The default value is false. It is an optional parameter.

The below examples demonstrate the ASP CreateTextFile Method.

Example 1: The below example demonstrates the ASP Folder.CreateTextFile Method.

ASP




<%
dim fs,fo,tfile
Set fs=Server.CreateObject("Scripting.FileSystemObject")
  
'Getting the folder where the file has to be created
Set fo=fs.GetFolder("d:\GFG")
  
'Creating a new text file
Set tfile=fo.CreateTextFile("GFG.txt",false)
  
'Writing a line to the file
tfile.WriteLine("Hello Geeks!")
  
'Closing the file
tfile.Close
  
Response.write("A new textfile has been created!")
  
'Reading the new text file
Set tfile=fs.OpenTextFile("d:\GFG\GFG.txt", 1)
Response.Write("<br>The File Contents are: " & tfile.ReadAll)
tfile.Close
  
set tfile=nothing
set fo=nothing
set fs=nothing
%>


Output:

A new textfile has been created!
The File Contents are: Hello Geeks! 

Example 2: The below example demonstrates the ASP FileSystemObject.CreateTextFile Method.

ASP




<%
dim fs,txtfile
set fs=Server.CreateObject("Scripting.FileSystemObject")
  
'Creating the text file at the given path
set txtfile=fs.CreateTextFile("D:\gfg.txt")
  
'Writing a line to the file
txtfile.WriteLine("Hello World!")
  
'Closing the file
txtfile.Close
  
Response.Write("The new text file has been created!")
  
'Reading the new text file
Set txtfile=fs.OpenTextFile("d:\GFG.txt", 1)
Response.Write("<br>The File Contents are: " & txtfile.ReadAll)
txtfile.Close
  
set txtfile=nothing
set fs=nothing
%>


Output:

The new text file has been created!
The File Contents are: Hello World!


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads