Use vbs to access external text files and script instances

Source: Internet
Author: User
Tags processing text string to file
Use vbs to access external text files and script instances
15:37:49
Use vbs to access external text files and script instances
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:
Rem creates "test .txt" in the current root directory and writes a string
Vbs dim FSO, file, filename
Vbs filename = "\ test .txt"
Vbs const forwriting = 2
Vbs set FSO = Createobject ("scripting. FileSystemObject ")
Ifexpression FSO. fileexists (filename) =-1
Vbscall call MessageBox ("error detected !!! The file already exists !!! ") @ <Stdlib. vbs>
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 !!! ") @ <Stdlib. vbs>
Rem over
Endscript
2. Forcibly write the file to overwrite the original content:
Rem writes a string to the file "test .txt" in the root directory of the current disk to overwrite the original content.
Vbs dim FSO, file, filename
Vbs filename = "\ test .txt"
Vbs const forwriting = 2
Vbs set FSO = Createobject ("scripting. FileSystemObject ")
Ifexpression FSO. fileexists (filename) = 0
Vbscall call MessageBox ("error detected !!! The file does not exist !!! ") @ <Stdlib. vbs>
Goto over
Endif
Vbs set file = FSO. opentextfile (filename, forwriting, ture)
Vbs file. writeline ("test writing a file row ")
Vbs file. Close
Vbscall call MessageBox ("file written successfully !!! ") @ <Stdlib. vbs>
Rem over
Endscript
3. Append content to the end of the file:
Rem appends a string to the "test .txt" file 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 ")
Ifexpression FSO. fileexists (filename) = 0
Vbscall call MessageBox ("error detected !!! The file does not exist !!! ") @ <Stdlib. vbs>
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 !!! ") @ <Stdlib. vbs>
Rem over
Endscript
4. Read a row of a specified file
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 ")
Ifexpression FSO. fileexists (filename) = 0
Vbscall call MessageBox ("error detected !!! The file does not exist !!! ") @ <Stdlib. vbs>
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) @ <stdlib. vbs>
Rem over
Endscript
5. Read 2 characters from the specified file line
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 ")
Ifexpression FSO. fileexists (filename) = 0
Vbscall call MessageBox ("error detected !!! The file does not exist !!! ") @ <Stdlib. vbs>
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) @ <stdlib. vbs>
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
Ifexpression 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
Ifexpression 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
If you are interested, you can take a closer look at it. Now we will talk so much about it and make it easy to sort it out. If you have any questions, I hope you can correct them.
Finally, remember to close the file after opening it to avoid data errors and data loss. If you have any questions, you can follow the post and study it together.

It seems like it was written by the wine, but it cannot be found now, dizzy

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.