Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File processing in VBscript

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi, I got assigned a little task, and I'm hoping that someone are able to aid me:

I need a VBScript that reads input from a file, alters some of the text, and save the file.

Optionaly, I could pre-make the wanted output as its only 5 lines, and just have the program insert the variable and save the file.

BUT: how do I manage filecontroll in VB ? open (file) for input as #1 certainly dont work, and I have never done any VBscripting before.

Any help apreciated.
 
Take a look at the Scripting.FileSystemObject

goto

and click on the
Microsoft Windows Script 5.6 Documentation
link. This will take you to the download for Microsoft Windows Script 5.6 Documentation which includes vbscript documentation.
The following is an excerpt from the documentation for fileSystemObject


Scripting Runtime Library

Working with Files
Language


VBScript

Show All
There are two major categories of file manipulation:

Creating, adding, or removing data, and reading files
Moving, copying, and deleting files
Creating Files
There are three ways to create an empty text file (sometimes referred to as a "text stream").

The first way is to use the CreateTextFile method. The following example demonstrates how to create a text file using the CreateTextFileMethod method.

[VBScript]
Dim fso, f1
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.

[VBScript]
Dim fso, ts
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.

[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)


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, use the Write, WriteLine, or WriteBlankLines methods of the TextStream object, according to the tasks outlined in the following table.

Task Method
Write data to an open text file without a trailing newline character. Write
Write data to an open text file with a trailing newline character. WriteLine
Write one or more blank lines to an open text file. WriteBlankLines

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

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/line feed). Be aware that the end of some strings may already have such nonprinting characters.
The following example demonstrates how to open a file, use all three write methods to add data to the file, and then close the file:

[VBScript]
Sub CreateFile()
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile("c:\testfile.txt", True)
' Write 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, or ReadAll method of the TextStream object. The following table describes which method to use for various tasks.

Task Method
Read a specified number of characters from a file. Read
Read an entire line (up to, but not including, the newline character). ReadLine
Read the entire contents of a text file. ReadAll

If you 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.

The following example demonstrates how to open a file, write to it, and then read from it:

[VBScript]
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 &quot;Writing file <br>&quot;
f1.WriteLine &quot;Hello World&quot;
f1.WriteBlankLines(1)
f1.Close
' Read the contents of the file.
Response.Write &quot;Reading file <br>&quot;
Set ts = fso_OpenTextFile(&quot;c:\testfile.txt&quot;, ForReading)
s = ts.ReadLine
Response.Write &quot;File contents = '&quot; & s & &quot;'&quot;
ts.Close
End Sub


Moving, Copying, and Deleting Files
The FSO object model has two methods each for moving, copying, and deleting files, as described in the following table.

Task Method
Move a file File.Move or FileSystemObject.MoveFile
Copy a file File.Copy or FileSystemObject.CopyFile
Delete a file File.Delete or FileSystemObject.DeleteFile

The following example creates 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.

To run the following example, create directories named \tmp and \temp in the root directory of drive C:

[VBScript]
Sub ManipFiles
Dim fso, f1, f2, s
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set f1 = fso.CreateTextFile(&quot;c:\testfile.txt&quot;, True)
Response.Write &quot;Writing file <br>&quot;
' Write a line.
f1.Write (&quot;This is a test.&quot;)
' Close the file to writing.
f1.Close
Response.Write &quot;Moving file to c:\tmp <br>&quot;
' Get a handle to the file in root of C:\.
Set f2 = fso.GetFile(&quot;c:\testfile.txt&quot;)
' Move the file to \tmp directory.
f2.Move (&quot;c:\tmp\testfile.txt&quot;)
Response.Write &quot;Copying file to c:\temp <br>&quot;
' Copy the file to \temp.
f2.Copy (&quot;c:\temp\testfile.txt&quot;)
Response.Write &quot;Deleting files <br>&quot;
' Get handles to files' current location.
Set f2 = fso.GetFile(&quot;c:\tmp\testfile.txt&quot;)
Set f3 = fso.GetFile(&quot;c:\temp\testfile.txt&quot;)
' Delete the files.
f2.Delete
f3.Delete
Response.Write &quot;All done!&quot;
End Sub

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

© 2001 Microsoft Corporation. All rights reserved.

Build: Topic Version 5.6.9309.1546
 
MS even recommends that you start using FSO in VB6, not just in VBScript.

Definately worth learning about - FSO is a PRIMARY VBScript tool.

Maybe they'll add binary file handling someday, but I doubt it, but for your purposes FSO should be pefect.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top