When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback

The 4 Guys Present: ASPFAQs.com

Jump to a FAQ
Enter FAQ #:
..or see our 10 Most Viewed FAQs.

4GuysFromRolla.com : ASP FAQS : FileSystemObject


Question:

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


[Print this FAQ]

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
BEGIN QUOTE
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.
END QUOTE
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!
  objTS.Close
  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.

Do you have a FAQ you'd like to suggest? Suggestions? Comments? If so, send it in! Also, if you'd like to be a FAQ Admin (creating/editing FAQs), let me know! If you are looking for other FAQs, be sure to check out the 4Guys FAQ and Commonly Asked Messageboard Questions!

Most Viewed FAQs:

1.) How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. (761643 views)
2.) I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem? (207777 views)
3.) How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset? (202549 views)
4.) How can I quickly sort a VBScript array? (196039 views)
5.) How can I find out if a record already exists in a database? If it doesn't, I want to add it. (156019 views)
6.) How do I display data on a web page using arrays instead of Do...While...MoveNext...???... (152331 views)
7.) When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... (140381 views)
8.) For session variables to work, must the Web visitor have cookies enabled? (110162 views)
9.) Can I send emails without using CDONTS? (107083 views)
10.) How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3? (106308 views)
Last computed at 9/17/2007 3:22:00 AM


ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article