Your source for ASP-related frequently asked questions and answers! : ASP FAQS : FileSystemObject

Question: How can I create a text file on the Web server's file system?

Answer: The FileSystemObject contains a handy method for creating text files on the Web server's file system. This method was aptly named: CreateTextFile, and has the following calling syntax:

TextStreamObject = fsoObjectInstance.CreateTextFile(filename[, overwrite[, unicode]])

filename should be the complete physical path of the file you wish to create. For instance, if you wish to create a text file in the directory C:\Inetpub\wwwroot\MyWeb named Hello.txt, filename should be: C:\Inetpub\wwwroot\MyWeb\Hello.txt. (If you don't know the full physical path of your Web site you can always find out using Server.MapPath. For more information see: Using Server.MapPath!)

overwrite and unicode are both optional Boolean values, both of which default to False. overwrite indicates what should happen when you try to create a file but a file with the same name in the same folder already exists. If overwrite is specified True, the file will be overwritten; if overwrite is specified as False (or omitted) the file will not be overwritten and you will receive an error. If unicode is specified as True, the file is treated as a Unicode file; if unicode is specified False (or omitted), the file is treated with the ASCII encoding.

Update from Bill Wilkinson
You say: "if unicode is specified False (or omitted), the file is treated with the ASCII encoding."

Not true. The file is treated as multibyte character encoding. That is, the current Session.CodePage is used in converting the Unicode characters that are ALWAYS used inside VBS and JS into the given multibyte encoding. In US English, and in most Western European languages, the multibyte encoding is actually single byte and is ASCII (well...not really...because ASCII only goes to character number 127...ISO would be more accurate). But in all the Eastern Asia languages and some of the other non-European languages, each unicode character translates to 2 or 3 or even 4 bytes in the text file stream when unicode is not specified.
Thanks Bill!

The CreateTextFile method returns a TextStream object that represents the newly created file. With this TextStream object you can write the contents to the file. (Check out Using the FileSystemObject to Write to Text Files for information on using the TextStream object to write to a file.)

The following shows some very simple code that will create a text file named C:\MyDirectory\foo.txt with the contents Hello, World!.

  'Create our FSO and TextStream objects
  Dim objFSO, objTS
  Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

  'Create the text file
  Set objTS = objFSO.CreateTextFile("C:\MyDirectory\foo.txt")

  'Write "Hello, World" to the file
  objTS.WriteLine("Hello, World!")

  'Clean up!
  Set objTS = Nothing
  Set objFSO = Nothing

A closing note: when dealing with IIS, the permissions granted to IUSR_machinename are important. For example, if you do not have write permission to the C:\MyDirectory folder for IUSR_machinename, an error will occur in the above script. (The machinename part of the IUSR_machinename bit above is the name of the Web server. If the Web server's machine name is Bob, then the IUSR_Bob account must have adequate permissions.)

Happy Programming!

-- View the technical docs for CreateTextFile
-- View the technical docs for the TextStream object

FAQ posted by Scott Mitchell at 3/26/2001 1:04:57 AM to the FileSystemObject category. This FAQ has been viewed 71,033 times.

Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers