Reading and Writing Text Files with the.NET Framework. With ASP.NET you should no longer use the. 'Open a file for reading Dim FILENAME as String.
Microsoft Windows 2. Scripting Guide. Microsoft® Windows® 2.
Scripting Guide. One of the more powerful tools available to system administrators is the text file. This might seem hard to believe in an age of high- resolution graphics and multi- user databases. Nevertheless, simple text files, such as those created in Notepad, remain a key element in system administration.
Text files are lightweight and low maintenance: They use up very little disk space and require no additional software of any kind to be installed on the computer. Text files are easy to work with and are extremely portable: A text file created by using a script can be copied and viewed on almost any computer in the world, including computers that do not run a Windows operating system. In addition to their convenience, text files provide a quick, easy, and standardized way to get data both into a script and out of a script.
Text files can be used to hold arguments that would otherwise need to be typed at the command line or hard- coded into a script; rather than typing 1. Likewise, text files provide a quick and easy way to store data retrieved from a script. This data could be written directly to a database; however, that requires additional configuration on the server, additional coding in the script, and additional overhead when the script runs. Instead, data can be saved to a text file and then later imported into a database.
- Reading and Writing Text Files. ' strContents = objFile.ReadAll Wscript.Echo strContents Wscript.Echo 'Reading file the second time.
- Reading Text Files in an ASP.NET. to open a file, our ASP.NET code. For more information on reading and writing files in ASP.NET be sure to check out.
- Simple demonstration of reading and writing text files in ASP.NET 2.0 using the StreamReader and StreamWriter classes.
- Read and write to file using ASP.NET (VB.NET) Reading and writing to the file content using ASP.NET 2.0 and C# 2.0 is actually very simple. First, you will need to.
Read and write to file using ASP.NET (C#) Reading and writing to the file content using ASP.NET 2.0 and C# 2.0 is actually very simple. First, you will need to import.
The File. System. Object provides a number of methods for both reading from and writing to text files. Creating Text Files. The File. System. Object allows you to either work with existing text files or create new text files from scratch.
To create a brand- new text file, simply create an instance of the File. System. Object and call the Create. Tex. File method, passing the complete path name as the method parameter.
For example, the script in Listing 4. Script. Log. txt in the C: \FSO folder. Listing 4. 3. 2 Creating a Text File. Set obj. FSO = Create. Object("Scripting.
File. System. Object"). Set obj. File = obj. FSO. Create. Text.
File("C: \FSO\Script. Log. txt"). If the file does not exist, the Create. Text. File method creates it. If the file does exist, the Create. Text. File method will overwrite the existing file and replace it with the new, blank file. If you prefer that the existing file not be overwritten, you can include the optional Overwrite parameter. When this parameter is False, existing files are not overwritten; when this parameter is True (the default value), existing files are overwritten.
For example, the following code sample does not overwrite the file C: \FSO\Script. Log. txt if that file already exists. Set obj. File = obj.
FSO. Create. Text. File("C: \FSO\Script. Log. txt", False). If you set the Overwrite parameter to False and the file already exists, a run- time error will occur. Because of that, you might want to check for the existence of the file and then, if the file exists, take some other action, such as allowing the user to specify an alternative file name for the new file. Creating File Names Within the Script.
One way to avoid the problems that can occur if a file already exists is to allow the script to generate a unique file name. Because the file name generator does not create meaningful file names, this is probably not a good approach for naming log files and other files that you might need to refer to in the future.
However, it does provide a way to ensure unique file names for scripts that require a temporary file. For example, you might have your script save data in HTML or XML format, have that data displayed in a Web browser, and then have this temporary file deleted as soon as the Web browser is closed. In a situation such as that, you can use the Get. Temp. File name method to generate a unique file name. To generate a unique file name, a script must create an instance of the File.
System. Object and then call the Get. Temp. Name method (with no parameters). For example, the script in Listing 4. For Next loop to create 1. Listing 4. 3. 3 Creating a File Name. Set obj. FSO = Create.
Object("Scripting. File. System. Object"). For i = 1 to 1. 0. Temp. File = obj. FSO. Get. Temp. Name.
Wscript. Echo str. Temp. File. When this script is run under Cscript, output similar to the following appears in the command window. Note. The file names generated by Get. Temp. Name are not guaranteed to be unique, partly because of the algorithm used to generate the names and partly because there are only a finite number of possible names; file names are limited to eight characters, and the first three characters are always rad. For example, in a test script that created 1. The remaining 1. 06 were duplicates (5. The demonstration script in Listing 4.
Get. Temp. Name method to create a file. The script must: Create an instance of the File. System. Object. Set a variable named str. Path to the folder where the file will be created (C: \FSO). Use the Get. Temp.
Name method to generate a unique file name. Use the Build. Path method to combine the folder name and file name and create a full path for the temporary file. The full path is stored in the variable str.
Full. Name. Call the Create. Text. File method, using str. Full. Name as the method parameter.
Close the file immediately after creating it. In a production environment, you would most likely write data to the file before closing it. Listing 4. 3. 4 Creating and Naming a Text File. Set obj. FSO = Create. Object("Scripting. File. System. Object"). Path = "C: \FSO".
File. Name = obj. FSO. Get. Temp. Name. Full. Name = obj. FSO. Build. Path(str. Path, str. File. Name). Set obj. File = obj. FSO. Create. Text.
File(str. Full. Name). Opening Text Files. Working with text files is a three- step process. Before you can do anything else, you must open the text file.
This can be done either by opening an existing file or by creating a new text file. When you create a new file, that file is automatically opened and ready for use.) Either approach returns a reference to the Text. Stream object. After you have a reference to the Text.
Stream object, you can either read from or write to the file. However, you cannot simultaneously read from and write to the same file. In other words, you cannot open a file, read the contents, and then write additional data to the file, all in the same operation. Instead, you must read the contents, close the file, and then reopen and write the additional data. When you open an existing text file, the file can be opened either for reading or for writing.
When you create a new text file, the file is open only for writing, if for no other reason than that there is no content to read. Finally, you should always close a text file. Although this is not required (the file will generally be closed as soon as the script terminates), it is good programming practice. To open a text file: Create an instance of the File. System. Object. Use the Open. Text. File method to open the text file.
The Open. Text. File method requires two parameters: the path to the file and one of the following values: For reading (parameter value = 1, constant = For. Reading). Files opened in this mode can only be read from.
To write to the file, you must open it a second time by using either the For. Writing or For. Appending mode. For writing (parameter value 2, constant = For. Writing). Files opened in this mode will have new data replace any existing data. That is, existing data will be deleted and the new data added.) Use this method to replace an existing file with a new set of data.
For appending (parameter value 8, constant = For. Appending). Files opened in this mode will have new data appended to the end of the file. Use this method to add data to an existing file.
You must use the appropriate parameter when opening the file. For example, if you open a file for reading and then attempt to write to the file, you will receive a "Bad file mode" error. You will also receive this error if you attempt to open anything other than a plain- text file.
It is worth noting that both HTML and XML files are plain- text files.)You can use either the parameter value (for example, 1 for reading) or you can create a constant and set the value appropriately. For example, both of these methods will open a file for reading. Const For. Reading = 1. Set obj. FSO = Create. Object("Scripting. File. System. Object"). Set obj. File = obj.
FSO. Open. Text. File("C: \FSO\Script. Log. txt", For. Reading). Set obj. File. 2 = obj. FSO. Open. Text. File("C: \FSO\Script. Log. 2. txt", 1).
However, you cannot use the constants without first defining them. This is due to the fact that VBScript does not have intrinsic access to COM object constants.
The following script sample will fail and return an "Invalid procedure call or argument" error because the For. Reading constant has not been explicitly defined. Because it has not been defined, For. Reading is automatically assigned the value 0, and 0 is not a valid parameter for Open. Text. File. Set obj. FSO = Create. Object("Scripting. File. System. Object").
Set obj. File = obj. FSO. Open. Text. File("C: \FSO\Script. Log. txt", For. Reading). The script in Listing 4.
C: \FSO\Script. Log. For. Reading to represent the value 1.
Listing 4. 3. 5 Opening a Text File for Reading. Const For. Reading = 1. Set obj. FSO = Create. Object("Scripting. File. System. Object"). Set obj. File = obj.
FSO. Open. Text. File("C: \FSO\Script. Log. txt", For. Reading).
Closing Text Files. Any text files opened by a script are automatically closed when the script ends. Because of this, you do not have to explicitly close text files any time you open them. Nevertheless, it is a good idea to always close text files when you are finished with them. Not only is this good programming practice, but problems will occur if you try to do one of the following without first closing the file: Delete the file. As noted previously in this chapter, you might occasionally write scripts that create a temporary file, use that file for some purpose, and then delete the file before the script terminates.