Microsoft KB Archive/300982

= How To Manipulate Text Files in an ASP Page =

Article ID: 300982

Article Last Modified on 3/29/2007

-

APPLIES TO

 Microsoft Active Server Pages 4.0, when used with:  Microsoft Windows 2000 Standard Edition

 Microsoft Windows NT 4.0 

-



This article was previously published under Q300982





IN THIS TASK
SUMMARY
 * Requirements
 * Create the FileSystemObject
 * How to Create, Open, and Delete Text Files
 * How to Write to and Read from a Text File
 * How to Move and Copy Text Files
 * Troubleshooting

REFERENCES



SUMMARY
This article describes how to create, write, read, move, copy, and delete text files from within an Active Server Pages (ASP) page.

You may want to use a text file for the sake of simplicity (when a database or more complicated file format may be excessive) or when prior data exists in the form of text files. Typical scenarios include:
 * Reading or writing a flat file that a mainframe, third-party application, or trading partner creates or uses.
 * Dynamically generating Windows batch or script files based on parameters that a client application supplies.

The Scripting.FileSystemObject Component Object Model (COM) object exposes a rich set of features to manipulate drives, folders, and files. This FileSystemObject (FSO) object is implemented within an in-process COM server called ScrObj.dll. This article only explores the file manipulation features of the FSO object model.

back to the top

Requirements
Your computer should be running either:


 * Microsoft Internet Information Server (IIS) 4.0 and Active Server Pages (ASP) 2.0.
 * Internet Information Services 5.0 and Active Server Pages 3.0.

You can use the general techniques, if not the implementation details, with any scripting language that supports Microsoft COM technology; they are also not restricted to ASP.

It is helpful if you are familiar with VBScript or JScript.

back to the top

Create the FileSystemObject
Before you manipulate text files, create the FSO as follows:

VBScript Code Dim objFSO Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;) JScript Code var objFSO; objFSO = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;); back to the top

How to Create, Open, and Delete Text Files
The FSO exposes the CreateTextFile method, which enables you to create an empty text file. It also exposes the OpenTextFile and DeleteFile methods, which enable you to open or delete an existing file respectively. Alternatively, you can use the File object to perform each of these three operations. To obtain an instance of the File object, invoke the GetFile method of the FSO. However, this article only demonstrates the direct methods on the FSO. Please refer to the &quot;References&quot; section for more information about the File object techniques.

The OpenTextFile method enables files to be opened for reading, writing, or appending textual information.

VBScript Code Dim objFSO, objCreatedFile, objOpenedFile Const ForReading = 1, ForWriting = 2, ForAppending = 8

'Create the FSO. Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)

Set objCreatedFile = objFSO.CreateTextFile(&quot;c:\HowToDemoFile.txt&quot;, True) Set objOpenedFile = objFSO.OpenTextFile(&quot;c:\HowToDemoFile2.txt&quot;, ForWriting, True)

'Use objCreatedFile and objOpenedFile to manipulate the corresponding files. objCreatedFile.Close objOpenedFile.Close

'Delete the files. objFSO.DeleteFile &quot;c:\HowToDemoFile.txt&quot; objFSO.DeleteFile &quot;c:\HowToDemoFile2.txt&quot; JScript Code var objFSO, objCreatedFile, objOpenedFile; var ForReading = 1, ForWriting = 2, ForAppending = 8;

// Create the FSO. objFSO = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);

objCreatedFile = objFSO.CreateTextFile(&quot;c:\\HowToDemoFile.txt&quot;, true); objOpenedFile = objFSO.OpenTextFile(&quot;c:\\HowToDemoFile2.txt&quot;, ForWriting, true);

// Use objCreatedFile and objOpenedFile to manipulate the corresponding files. objCreatedFile.Close; objOpenedFile.Close;

// Delete the files. objFSO.DeleteFile(&quot;c:\\HowToDemoFile.txt&quot;); objFSO.DeleteFile(&quot;c:\\HowToDemoFile2.txt&quot;); back to the top

How to Write to and Read from a Text File
The CreateTextFile or OpenTextFile methods of the FSO return an instance of the TextStream object, which exposes methods to write or read text files.

Both the Write and WriteLine methods add text to an open file, but WriteLine also appends a trailing newline character. The WriteBlankLines method writes one or more blank lines to an open file based on the parameter that is specified.

The Read method reads the specified number of characters from the current position in the open file. The ReadLine method reads an entire line (up to, but excluding, the newline character), whereas the ReadAll method reads the entire contents of the open file. All three of these methods store the resulting text in a string that you can easily manipulate with various string manipulation operations.

The following code demonstrates these methods:

VBScript Code Dim objFSO, objTextFile Dim sRead, sReadLine, sReadAll Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;) Set objTextFile = objFSO.CreateTextFile(&quot;c:\HowToDemoFile.txt&quot;, True)

' Write a line with a newline character. objTextFile.WriteLine(&quot;This line is written using WriteLine.&quot;)

' Write a line. objTextFile.Write (&quot;This line is written using Write.&quot;)

' Write three newline characters to the file. objTextFile.WriteBlankLines(3)

objTextFile.Close

' Open file for reading. Set objTextFile = objFSO.OpenTextFile(&quot;c:\HowToDemoFile.txt&quot;, ForReading)

' Use different methods to read contents of file. sReadLine = objTextFile.ReadLine sRead = objTextFile.Read(4) sReadAll = objTextFile.ReadAll

objTextFile.Close JScript Code var objFSO, objTextFile; var sRead, sReadLine, sReadAll; var ForReading = 1, ForWriting = 2, ForAppending = 8;

objFSO = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;); objTextFile =objFSO.CreateTextFile(&quot;c:\\HowToDemoFile.txt&quot;, true);

// Write a line with a newline character. objTextFile.WriteLine(&quot;This line is written using WriteLine.&quot;);

// Write three newline characters to the file. objTextFile.WriteBlankLines(3);

// Write a line. objTextFile.Write (&quot;This line is written using Write.&quot;); objTextFile.Close;

// Open file for reading. objTextFile = objFSO.OpenTextFile(&quot;c:\\HowToDemoFile.txt&quot;, ForReading);

// Use different methods to read contents of file. sReadLine = objTextFile.ReadLine; sRead = objTextFile.Read(4); sReadAll = objTextFile.ReadAll;

objTextFile.Close; back to the top

How to Move and Copy Text Files
The FSO exposes the MoveFile and CopyFile methods, which enable you to move and copy files respectively. Alternatively, you can use the File object to perform each of these two operations. To obtain an instance of the File object, invoke the GetFile method of the FSO. However, this article only demonstrates the direct methods on the FSO. Please refer to the &quot;References&quot; section for more information about the File object techniques.

The following code sample demonstrates these methods:

VBScript Code Dim objFSO Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)

objFSO.MoveFile &quot;c:\HowToDemoFile.txt&quot;, &quot;c:\Temp\&quot; objFSO.CopyFile &quot;c:\Temp\HowToDemoFile.txt&quot;, &quot;c:\&quot; JScript Code var objFSO; objFSO = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);

objFSO.MoveFile(&quot;c:\\HowToDemoFile.txt&quot;, &quot;c:\\Temp\\&quot;); objFSO.CopyFile(&quot;c:\\Temp\\HowToDemoFile.txt&quot;, &quot;c:\\&quot;); back to the top

Troubleshooting
 Error messages while creating Scripting.FileSystemObject:

This problem most likely occurs because of the following scenarios:

 ScrObj.dll has not been installed on the deployment computer.</li> ScrObj.dll has been accidentally deleted.</li> ScrObj.dll has been accidentally unregistered from the COM registry.</li></ul>

Because ScrObj.dll resides in the <Windows Installation Directory>\System32 folder, it is part of the operating system dynamic-link libraries (DLLs). Thus, on Windows 2000-based computers, it cannot be deleted, at least not without resorting to extreme measures that seriously hamper the system.</li> Error messages while performing file manipulation operations:

If drives are formatted using the NTFS file system, users in the Administrator group have control over security permissions for entire drives, folders, and files. Especially on Web servers, Administrators should enforce strong security permissions to prevent illegal access to files and information. When you design and write an ASP application that uses the above-mentioned file manipulation techniques, special consideration should be given to the security permissions for the file system on deployment computers to prevent errors that are related to insufficient security permissions.</li></ul>

back to the top

<div class="references_section">