Accessing external text files using VBS some methods and script instance code _vbs

Source: Internet
Author: User
Tags goto readline string to file
The constants used to process files are: ForReading = 1, forwriting=2,forappending=8, for which operations are read/write and appended, and are declared with the VBS const before use. Often used to process text files are: Read (VAR)-reading Var characters, readline-read a line, readall-read the entire file content, skipline-Skip line to the next row, write (Var)-to write the string var file, WriteLine (Var)-writes the string var and newline characters to the file, WriteBlankLines (n)-writes n newline characters.
Here are a few examples of using VBScript to access external files:

1. Create a new text file and report an error if the file already exists:
Copy Code code as follows:

Rem creates "test. txt" and writes a string under the current packing directory
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 found!!! File already exists!!! ")
Goto over
EndIf
VBS Set File=fso. CreateTextFile (Filename,ture)
VBS file. WriteLine ("File first line, this is a test file")
VBS file. Close
Vbscall Call MessageBox ("Test file creation succeeded!!!")
Rem over
Endscript

2. Overwrite the original content by forced writing to the file:
Copy Code code as follows:

Rem creates "test. txt" and writes a string under the current packing directory
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 found!!! File already exists!!! ")
Goto over
EndIf
VBS Set File=fso. CreateTextFile (Filename,ture)
VBS file. WriteLine ("File first line, this is a test file")
VBS file. Close
Vbscall Call MessageBox ("Test file creation succeeded!!!")
Rem over
Endscript

3. Append to File Tail:
Copy Code code as follows:

Rem appends a string to the end of the file "test. txt" in the current packing directory
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 found!!! 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 Success!!!")
Rem over
Endscript

4. Read the specified file line
Copy Code code as follows:

Rem reads a row from the file "test. txt" from the current packing directory
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 found!!! File does not exist!!! ")
Goto over
EndIf
VBS Set File=fso. OpenTextFile (filename,forreading)
VBS Text=file. ReadLine
VBS file. Close
VBS text= "from File &filename&" read row succeeded! The read string is: "&text
Vbscall call MessageBox (text)
Rem over
Endscript

5. Read 2 characters from the specified file line
Copy Code code as follows:

Rem reads 2 characters from the file "test. txt" in the current packing directory
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 found!!! File does not exist!!! ")
Goto over
EndIf
VBS Set File=fso. OpenTextFile (filename,forreading)
VBS Text=file. Read (2)
VBS file. Close
VBS text= "from File &filename&" read 2 characters successfully! Read the character: "&text
Vbscall call MessageBox (text)
Rem over
Endscript

6. ReadAll read the entire file content, large files too much resources, do not recommend the use of

Use method and ReadLine approximation

7.SkipLine jumped out of the way

The use method is file. SkipLine, for files that are opened only for read-only properties

8. test file line End/end of file
A. End of Test line
Judgment statement
If file. Atendofline=-1
...
EndIf
The current pointer is at the end of the line when the expression is true
B. End of test file
Judgment statement
If file. Atendofstream=-1
...
EndIf
The current pointer is at the end of the file when the expression is true

9. Other functions used in the file or called method functions are

A.file.column returns the current column number
B.file.line returns the current line number
C.file.fileexists (filename) Determines whether the specified file exists

Finally, it is important to remember that when you open a file, you should always close 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.