FileSystemObject Processing Documents _FSO topics

Source: Internet
Author: User
Tags readline
There are two main types of file processing:

Create, add, or delete data, and read files
Moving, copying, and deleting files
Create a file
There are three ways to create an empty text file (sometimes called a "text stream").
The first method is to use the CreateTextFile method. The following example demonstrates how to create a text file in VBScript in this way:


Dim FSO, F1
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f1 = fso. CreateTextFile ("C:\testfile.txt", True)

To use this method in JScript, use the following code:

var fso, F1;
FSO = new ActiveXObject ("Scripting.FileSystemObject");
F1 = fso. CreateTextFile ("C:\\testfile.txt", true);

Examine the sample code to see how to use the CreateTextFile method in FileSystemObject.
The second way to create a text file is to use the OpenTextFile method of the FileSystemObject object and set the ForWriting flag. In VBScript, the code is like the following example:

Dim FSO, TS
Const ForWriting = 2
Set fso = CreateObject ("Scripting.") FileSystemObject ")
Set ts = fso. OpenTextFile ("C:\test.txt", ForWriting, True)

To use this method in JScript to create a text file, use the following code:

var fso, TS;
var forwriting= 2;
FSO = new ActiveXObject ("Scripting.FileSystemObject");
TS = fso. OpenTextFile ("C:\\Test.txt", ForWriting, True);

The third way to create a text file is to use the OpenAsTextStream method and set the ForWriting flag. To use this method, use the following code in VBScript:

Dim FSO, F1, TS
Const ForWriting = 2
Set fso = CreateObject ("Scripting.FileSystemObject")
Fso. CreateTextFile ("C:\test1.txt")
Set f1 = fso. GetFile ("C:\test1.txt")
Set ts = f1. OpenAsTextStream (ForWriting, True)

In JScript, use the code in the following example:

Var fso, F1, TS;
var forwriting = 2;
FSO = new ActiveXObject ("Scripting.FileSystemObject");
Fso. CreateTextFile ("C:\\test1.txt");
F1 = fso. GetFile ("C:\\test1.txt");
TS = F1. OpenAsTextStream (ForWriting, true);

Add data to a file
Once you have created a text file, use the following three steps to add data to the file:

Open a text file.
Write Data.
Closes the file.
To open an existing file, use the OpenTextFile method of the FileSystemObject object or the OpenAsTextStream method of the file object.
To write data to an open text file, use the Write, WriteLine, or WriteBlankLines method of the TextStream object according to the tasks described in the following table.

Task methods
Writes data to an open text file without following a new line character. Write
Writes data to an open text file, followed by a new line character. WriteLine
Writes one or more blank lines to an open text file. WriteBlankLines


Examine the sample code to see how to use the Write, WriteLine, and WriteBlankLines methods in the FileSystemObject object.

To turn off an open file, use the Close method of the TextStream object.

Examine the sample code to see how to use the Close method in FileSystemObject.


--------------------------------------------------------------------------------

Note that the new line character contains one or more characters (depending on the operating system) to move the cursor to the beginning of the next line (carriage return/linefeed). Note that some strings may already have this nonprinting character at the end.

--------------------------------------------------------------------------------


The following VBScript example shows how to open a file and use three writing methods to add data to a file, and then close the file:


Sub CreateFile ()
Dim FSO, TF
Set fso = CreateObject ("Scripting.FileSystemObject")
Set tf = fso. CreateTextFile ("C:\testfile.txt", True)
' Writes a line with a new line character.
Tf. WriteLine ("Testing 1, 2, 3.")
' Write three new line characters to the file.
Tf. WriteBlankLines (3)
' Write a line.
Tf. Write ("This is a test.")
Tf. Close
End Sub
This example demonstrates how to use these three methods in JScript:

function CreateFile ()
{
var fso, TF;
FSO = new ActiveXObject ("Scripting.FileSystemObject");
tf = FSO. CreateTextFile ("C:\\testfile.txt", true);
Writes a line with a new line character.
Tf. WriteLine ("Testing 1, 2, 3.");
Writes three new line characters to a file.
Tf. WriteBlankLines (3);
Write a line.
Tf. Write ("This is a test.");
Tf. Close ();
}
Reading files
To read data from a text file, use the read, ReadLine, or ReadAll method of the TextStream object. The following table describes which methods should be used for different tasks.
Task methods
Reads a specified number of characters from a file. Read
Reads an entire line (all the time but does not include new line characters). ReadLine
Reads the entire contents of a text file. ReadAll


Examine the sample code to see how to use the ReadAll and ReadLine methods in FileSystemObject.

If you use the Read or ReadLine method and you want to skip a particular part of the data, use the Skip or SkipLine method. The result text of the Read method exists in a string that can be displayed in a control, or it can be parsed, connected, and so on by string functions such as left, right, and Mid.

The following VBScript example demonstrates how to open a file and how to write data to a file and read data from a file:


Sub Readfiles
Dim FSO, F1, TS, s
Const ForReading = 1
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f1 = fso. CreateTextFile ("C:\testfile.txt", True)
' Write a line.
Response.Write "Writing File <br>"
F1. WriteLine "Hello World"
F1. WriteBlankLines (1)
F1. Close
' Read the contents of the file.
Response.Write "Reading File <br>"
Set ts = fso. OpenTextFile ("C:\testfile.txt", ForReading)
s = ts. ReadLine
Response.Write "File contents = '" & S & "'"
Ts. Close
End Sub

The following code demonstrates doing the same thing in JScript:

function Readfiles ()
{
Var fso, F1, TS, s;
var ForReading = 1;
FSO = new ActiveXObject ("Scripting.FileSystemObject");
F1 = fso. CreateTextFile ("C:\\testfile.txt", true);
Write a line.
Response.Write ("Writing file <br>");
F1. WriteLine ("Hello World");
F1. WriteBlankLines (1);
F1. Close ();
Read the contents of the file.
Response.Write ("Reading file <br>");
TS = fso. OpenTextFile ("C:\\testfile.txt", ForReading);
s = ts. ReadLine ();
Response.Write ("File contents = '" + S + "");
Ts. Close ();
}

Moving, copying, and deleting files
The FSO object mode has two ways to move, copy, and delete files, as described in the following table.
Task methods
Move file File.move or Filesystemobject.movefile
Copy files File.Copy or Filesystemobject.copyfile
Delete file File.delete or Filesystemobject.deletefile


Examine the sample code to understand two ways to delete a file in FileSystemObject.

The following VBScript example creates a text file in the root directory of drive C, writes some information to it, moves it to the \tmp directory, makes a backup in the \temp, and then deletes them from both directories.

To run the following example, you need to first create the \tmp and \temp directories in the root directory of drive C:


Sub Manipfiles
Dim FSO, F1, F2, s
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f1 = fso. CreateTextFile ("C:\testfile.txt", True)
Response.Write "Writing File <br>"
' Write a line.
F1. Write ("This is a test.")
' Close the file.
F1. Close
Response.Write "Moving file to C:\tmp <br>"
' Gets the handle of the file in the root directory (C:\) of C.
Set F2 = fso. GetFile ("C:\testfile.txt")
' Move the file to the \tmp directory.
F2. Move ("C:\tmp\testfile.txt")
Response.Write "Copying file to C:\Temp <br>"
' Copy the file to the \temp directory.
F2. Copy ("C:\temp\testfile.txt")
Response.Write "Deleting Files <br>"
' Gets the handle to the current location of the file.
Set F2 = fso. GetFile ("C:\tmp\testfile.txt")
Set F3 = fso. GetFile ("C:\temp\testfile.txt")
' Delete the file.
F2. Delete
F3. Delete
Response.Write "All done!"
End Sub

The following code demonstrates doing the same thing in JScript:

function Manipfiles ()
{
Var fso, F1, F2, s;
FSO = new ActiveXObject ("Scripting.FileSystemObject");
F1 = fso. CreateTextFile ("C:\\testfile.txt", true);
Response.Write ("Writing file <br>");
Write a line.
F1. Write ("This is a test.");
Closes the file.
F1. Close ();
Response.Write ("Moving file to C:\\tmp <br>");
Gets the handle of the file in the root directory (C:\) of C.
F2 = fso. GetFile ("C:\\testfile.txt");
Move the file to the \tmp directory.
F2. Move ("C:\\tmp\\testfile.txt");
Response.Write ("Copying file to C:\\Temp <br>");
Copy the files to the \temp directory.
F2. Copy ("C:\\temp\\testfile.txt");
Response.Write ("Deleting Files <br>");
Gets the handle to the current location of the file.
F2 = fso. GetFile ("C:\\tmp\\testfile.txt");
F3 = FSO. GetFile ("C:\\temp\\testfile.txt");
Deletes a file.
F2. Delete ();
F3. Delete ();
Response.Write ("All done!");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.