VBS和bat批处理逐行读取文件实例
首先是批处理的,很简单,每隔两秒钟读取一行。
@echooff for/f"tokens=*"%%iin(lrbf.ini)do(echo%%i&ping-n2127.1>nul) pause
更直观的:
FOR/F"delims="%iIN(file.txt)DOecho%i
当然如果你想做更多其他的事do后面是你发挥的地方
VBS的两个版本
第一种方式,逐行读取,依次显示:
ConstForReading=1
dim objFSO,objFile,strline
SetobjFSO=CreateObject("Scripting.FileSystemObject")
SetobjFile=objFSO.OpenTextFile("lrbf.ini",ForReading)
do until objFile.atendofstream
strline=objFile.readline
wscript.echo strline '这里是显示一行内容而已,可以换成别的内容
loop
objFile.close
set fso=nothing第二种方式,全部读取,依次显示:
ConstForReading=1
dim objFSO,objFile,strline
SetobjFSO=CreateObject("Scripting.FileSystemObject")
SetobjFile=objFSO.OpenTextFile("lrbf.ini",ForReading)
str=objFile.readall
objFile.close
if str="" then
wscript.echo "Nothing"
wscript.quit
end if
strarry=split(str,vbcrlf)
for each linestr in strarry
wscript.echo linestr '这里是用echo显示每一行的内容,可以换成别的内容
next
set fso=nothing
VBS读取文本最后一行:
ConstForReading=1
SetobjFSO=CreateObject("Scripting.FileSystemObject")
SetobjFile=objFSO.OpenTextFile("lrbf.ini",ForReading)
DoUntilobjFile.AtEndOfStream
strNextLine=objFile.ReadLine
IfLen(strNextLine)>0Then
strLine=strNextLine
EndIf
Loop
objFile.Close
Wscript.EchostrLine