There are two major Categories of file manipulation-
1. Creating, Adding, Removing and Reading files
2. Moving, Copying and Deleting Files.
Creating Files :
There are three ways to create an empty text file, sometime referred to as a "text Stream"
The first way is to use the CreateTextFile method.
Set fso=CreateObject("Scripting.FileSystemObject")
Set f1=fso.CreateTextFile("C:\testfile.txt",true)
The second way to create a text file is to use the OpenTextFile method of the FileSystemObject object with the ForWriting flag set.
Const ForWriting=2
Set fso=CreateObject("Scripting.FileSystemObject")
Set ts=fso.OpenTextFile("C:\test.txt",ForWriting, True)
A third way to create a text file is to use the OpenAsTextStream method with the ForWriting flag set.
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)
Adding Data to the File-
Once the text file is created, add data to the file using the following three steps-
- Open the text file.
- Write the data.
- Close the file.
To Open an existing file, use either the OpenTextFile method of the FileSystemObject object or the OpenAsTextStream method of the File object.
To write data to the open text file, we can use the Write, WriteLine or WriteBlankLines methods of the TextStream object, according to the tasks.
Method Task
Write - Write data to an Open text file without a trailing newline character.
WriteLine - Write data to an open text file with a trailing newline character.
WriteBlankLine- Write one or more blank lines to an open text file.
Note-
The newline character contains a character or characters (depending on the operating system) to advance the cursor to the beginning of the next line(carriage return/linefeed).Be aware that the end of some strings may already have such nonprinting characters.
Example - write a script to open a file, use all three write methods to add data to the file and then close the file.
Dim fso, tf
Set fso=CreateObject("Scripting.FileSystemObject")
Set tf=fso.CreateTextFile("C:\testfile.txt",true)
'writing a line with a newline character
tf.WriteLine("Testing 1, 2, 3.")
'Write three newline characters to the file
tf.WriteBlankLines(3)
'Write a line
tf.Write("This is a test")
tf.Close
End Sub
Reading Files -
To read data from a text file, use the Read, ReadLine and ReadAll method of the TextStream object. The following are describes which method to use for various tasks.
Read - Read a specified number of characters from a file.
ReadLine - Read and entire line (up to, but not including the newline character).
ReadAll - Read the entire contents of a text file
If we use the Read or ReadLine method and want to skip to a particular portion of data, use the Skip or SkipLine method. The resulting text of the read methods is stored in a string which can be displayed in a control, parsed by string functions (such as Left, Right and Mid) concatenated and so forth.
Example- Write a script to open file, write to it and then read from it.
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"
f1.WriteLine "Hello World"
f1.WriteBlankLines(1)
f1.Close
'Read the contents of the file
Response.Write "Reading file"
Set ts=fso.OpenTextFile("c:\testfile.txt", ForReading)
s=ts.ReadLine
Response.Write "File contents = "&s&"'"
ts.close
End Sub
Moving, Copying and Deleting Files-
The FSO Object model has two methods each for moving, copying and deleting files.
File.Move or FileSystemObject.MoveFile - Move a file
File.Copy or FileSystemObject.CopyFile - Copy a file
File.Delete or FileSystemObject.DeleteFile - Delete a file
Example - Create a text file in the root directory of drive C, writes some information to it, moves it to a directory called \tmp, makes a copy of it in a directory called \temp, then deletes the copies from both directories.
Note- before that create directories named \tmp and \temp in the root directory of drive C.
Sub ManipFiles
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1=fso.CreateTextFile("C:\testfile.txt", true)
Response.Write "Writing file"
'Write a line
f1.Write("This is a test")
'Close the file to writing
f1.close
Response.Write "Moving file to C:\tmp"
'Get a handle to the file in root of c:\
Set f2=fso.GetFile("c:\testfile.txt")
'Move the file to \tmp directory
f2.Move ("C:\tmp\testfile.txt")
Response.Write "Copying file to c:\temp"
'Copy the file to \tmp
f2.Copy ("C:\tmp\testfile.txt")
Response.Write "Deleting files"
'Get handles to files current location
set f2=fso.GetFile("C:\tmp\testfile.txt")
Set f3=fso.GetFile("C:\tmp\testfile.txt")
'Delete the files
f2.Delete
f3.Delete
Response.Write "All Done"
End Sub