Using VBS to access external text files some methods and script instance code

Source: Internet
Author: User
Tags processing text string to file

Constants used to process files include: ForReading = 1, ForWriting = 2, and ForAppending = 8. The operations used are read/write and append, before use, use VBS Const. Commonly used methods for processing text files include: Read (var)-Read var characters, ReadLine-Read a row, ReadAll-Read the entire file content, SkipLine-Skip the line pointing to the next line, write (var)-Write the string var into the file, WriteLine (var)-Write the string var and linefeed into the file, WriteBlankLines (n)-Write n linefeeds.
Below are several examples of using VBScript to access external files:

1. Create a new text file. If the file already exists, report an error:Copy codeThe Code is as follows: Rem creates "test .txt" in the root directory of the current disk and writes a string
VBS dim fso, file, filename
VBS filename = "\ test .txt"
VBS Const ForWriting = 2
VBS Set fso = CreateObject ("Scripting. FileSystemObject ")
If fso. FileExists (filename) =-1
VBSCall Call MessageBox ("error detected !!! The file already exists !!! ")
Goto over
EndIf
VBS Set file = fso. CreateTextFile (filename, Ture)
VBS file. WriteLine ("the first line of the file, this is a test file ")
VBS file. Close
VBSCall Call MessageBox ("Test File Created successfully !!! ")
Rem over
EndScript

2. Forcibly write the file to overwrite the original content:Copy codeThe Code is as follows: Rem creates "test .txt" in the root directory of the current disk and writes a string
VBS dim fso, file, filename
VBS filename = "\ test .txt"
VBS Const ForWriting = 2
VBS Set fso = CreateObject ("Scripting. FileSystemObject ")
If fso. FileExists (filename) =-1
VBSCall Call MessageBox ("error detected !!! The file already exists !!! ")
Goto over
EndIf
VBS Set file = fso. CreateTextFile (filename, Ture)
VBS file. WriteLine ("the first line of the file, this is a test file ")
VBS file. Close
VBSCall Call MessageBox ("Test File Created successfully !!! ")
Rem over
EndScript

3. Append content to the end of the file:Copy codeThe Code is as follows: Rem appends a string to the end of the file "test .txt" in the root directory of the current disk.
VBS dim fso, file, filename
VBS filename = "\ test .txt"
VBS Const ForAppending = 8
VBS Set fso = CreateObject ("Scripting. FileSystemObject ")
If fso. FileExists (filename) = 0
VBSCall Call MessageBox ("error detected !!! The file does not exist !!! ")
Goto over
EndIf
VBS Set file = fso. OpenTextFile (filename, ForAppending)
VBS file. WriteLine ("test append string to end of file ")
VBS file. Close
VBSCall Call MessageBox ("APPEND string to file successful !!! ")
Rem over
EndScript

4. Read a row of a specified fileCopy codeThe Code is as follows: Rem reads a row from the file "test .txt" in the root directory of the current disk.
VBS dim fso, file, filename, text
VBS filename = "\ test .txt"
VBS Const ForReading = 1
VBS Set fso = CreateObject ("Scripting. FileSystemObject ")
If fso. FileExists (filename) = 0
VBSCall Call MessageBox ("error detected !!! The file does not exist !!! ")
Goto over
EndIf
VBS Set file = fso. OpenTextFile (filename, ForReading)
VBS text = file. ReadLine
VBS file. Close
VBS text = "read from file" & filename! The read string is: "& text
VBSCall Call MessageBox (text)
Rem over
EndScript

5. Read 2 characters from the specified file lineCopy codeThe Code is as follows: Rem reads 2 characters from the file "test .txt" in the root directory of the current disk.
VBS dim fso, file, filename, text
VBS filename = "\ test .txt"
VBS Const ForReading = 1
VBS Set fso = CreateObject ("Scripting. FileSystemObject ")
If fso. FileExists (filename) = 0
VBSCall Call MessageBox ("error detected !!! The file does not exist !!! ")
Goto over
EndIf
VBS Set file = fso. OpenTextFile (filename, ForReading)
VBS text = file. Read (2)
VBS file. Close
VBS text = "read from file" & filename & "2 Characters! The read character is: "& text
VBSCall Call MessageBox (text)
Rem over
EndScript

6. ReadAll reads the entire file content. Large files occupy too much resources and are not recommended.

// The usage is similar to that of ReadLine.

7. Skip the current row using SkipLine

// The usage is file. SkipLine, which is only used for files opened by the read-only attribute.

8. Test the end of a file line/end of a file
A. the test row ends.
Judgment statement
If file. AtEndOfLine =-1
...
EndIf
// When the expression is true, the current pointer is at the end of the row
B. End of Test File
Judgment statement
If file. AtEndOfStream =-1
...
EndIf
// When the expression is true, the current pointer is at the end of the file

9. Other functions or method functions used for files

A. file. Column: returns the current Column number.
B. file. Line: returns the current row number.
C. file. FileExists (filename) to determine whether the specified file exists

Finally, remember to close the file after opening it to avoid data errors and data loss.

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.