VBScript scripting programming 3 About file reading and writing
BY
SS
SA
2000
7/9 /
2004
Use VBScript to read and write files, very convenient, nonsense, to remove the topic.
1, open the file
Use the OpenTextFile method
Set fs = creteObject ("scripting.filesystemobject")
Set ts = fs.opentextfile ("c: /1.txt", 1, true)
Note that the full path to the file is required, and a parameter behind is an access mode.
1 is forreading
2 is forwriting
8 is A
PP
Ending
The third parameter specifies whether it is created if the specified file does not exist.
2, read the file
There are three ways to read the file.
Read (x) Read X characters
reta
li
NE read a line
RE
ADA
LL all read
E.g:
Set fs = creteObject ("scripting.filesystemobject")
Set ts = fs.opentextfile ("c: /1.txt", 1, true)
Value = TS.READ (20)
li
Ne = ts.read
li
NE
Contents = TS.RE
ADA
LL
Here you still have to introduce several pointer variables:
The ATENDOFSTREAM property of the TextStream object. This attribute returns true when it is in the end of the file. We
can
It has not arrived at the end of the file with cyclic detection. E.g:
Set fs = creteObject ("scripting.filesystemobject")
Set f = fs.getfile ("c: /1.txt", 1, false)
Set ts = f.OpenastextStream (1,0)
Do W
hi
Le Ts.atendofstream <> true
F.Read (1)
loop
There is also an attribute, ATENDOF
li
NE, if you have reached the end of the row, this property returns True.
TextStream objects have two useful properties, column and
li
Ne.
After opening a file, the row and column pointers are set to 1.
Look at an integrated example:
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
* read.vbs
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
Set fs = creteObject ("scripting.filesystemobject")
Set f = fs.opentextfile ("c: /1.txt", 1, true)
Do W
hi
Le F. DendofStream <> TRUE
Data = ""
For a = 1 TO 5
IF
F. Dendofstream <> true kil
Data = DATA F.Read
li
NE
end
IF
NEXT
DataSet = DataSet 1
WScript.
echo
"Data Set" & Dataset & ":" & data
loop
Finally, let's jump in the file.
Skip (x) Skip X characters
Skip
li
NE skipping one line
The usage is also very simple and the front, just don't say it.
3, write files
can
Use Forwriting and FORA
PP
Ending mode to write
Write 3 methods:
Write (x)
Write
li
NE
Writeblank
li
NES (n) is written to NS to see an example:
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
*
Data = "Hello, I
li
Ke Script P
RO
"" ""
Set fs = creteObject ("scripting.filesystemobject")
IF
(fs.fileexists ("c: /2.txt")).
Set f = fs.opentextfile ("c: /2.txt", 8)
F.Write Data
F.Write
li
Ne Data
f.close
Else
Set f = fs.opentextfile ("c: /2.txt", 2, true)
F.WriteBlank
li
Nes 2
F.Write Data
f.close
end
IF
Note Be sure to turn off after writing the file! ! ! ! ! ! ! Also, if you want to read the file and write a file, you must also remember to turn off after reading it, so that you can open it in a written manner.
It's better to finish the documents, and it is possible to involve the operation of the string.
Inside 1 chapter, I intend to write a little driver and registry, the script programming is also very rich, I only talk about hackers. Today, I'm so tired, and I will not be able to fill in the forum. I will delete it every day. This is not good, the forum is everyone, I write some original things every day to make our forum and other forums Some different, I have a limited strength, but also rely on everyone's power, I plan to find a few people in the face of a few people, so that our forum is a bit represented.
Very late, rest.