
Const ForReading = 1
filespec = "D:\testtxt"
Set fso = CreateObject("ScriptingFileSystemObject")
Set myfile = fsoOpenTextFile(filespec, ForReading)
Do Until myfileAtEndOfStream
strLine = myfileReadLine
MsgBox strLine
# 要取出数字,再对 strLine 进行处理。你期望的输出格式是怎样的?
Loop
myfileClose
你可以把那个
show interface s1/0>ciscotxt
就是导出到一个txt文档内,然后在用VBS来读取csicotxt文档的内容,进而查找需要的数据。
n=inputbox ("请输入你要读取的行:","提示","8")
writeclip n
msgbox "已将读取内容写进贴剪板!",48+4096,"系统提示"
sub writeclip(text)
set fso=createobject("scriptingfilesystemobject")
set file=fsoopentextfile("C:\worktxt")
for i=0 to n-1
fileskipline
next
text=filereadline
fileclose
Set objIE = CreateObject("InternetExplorerApplication")
objIENavigate("about:blank")
objIEdocumentparentwindowclipboardDataSetData "text", text
objIEQuit
end sub
不清楚你的实际文件/情况,仅以问题中的样例/说明为据
复制粘贴到记事本,另存为xxvbs,编码选ANSI,跟要处理的文件放一起运行rem 统计多个指定的字符串/关键字在一个txt文本文件内出现的次数
info="Any question +"& Chr(87) & Chr(88) & "/" & Chr(81) & Chr(81) & CStr(&H53b7e0b4)
Set fso=CreateObject("ScriptingFilesystemobject")
txtfile="\yourfiletxt"
words=Array("word1", "word2", "word3")
If Not fsoFileExists(txtfile) Then
msgbox """" & txtfile & """ not found" & vbCrLf & info
WSHQuit
End If
Set f=fsoOpenTextFile(txtfile, 1)
text=fReadAll
fClose
s=""
For i=0 to Ubound(words)
n=0
m=1
wlen=len(words(i))
t=InStr(1, text, words(i))
If t>=1 Then
n=n+1
m=t
End If
Do While t<>0
t=InStr(m+wlen, text, words(i))
If t>=1 Then
n=n+1
m=t
End If
Loop
s=s & words(i) & ":" & n & vbCrLf
Next
msgbox s & info
新建一个扩展名为wsf的文件,即window脚本宿主文件
输入以下代码
<job id="vbs">
<script language="VBScript">
Const ForReading = 1
Dim objFSO, objFile, i
Set objFSO = CreateObject("ScriptingFileSystemObject")
Set objFile = objFSOOpenTextFile("testtxt", ForReading)
Do While objFileAtEndOfStream <> True
objFileReadLine
i = i + 1
Loop
objFileClose
WscriptEcho i
</script>
</job>
把"testtxt"改成你要的文件就可以统计该文件的行数
改成可以输入的文件名的,可以输入多个文件名,每个文件名用空格隔开
<job id="HowManyLines">
<script language="VBScript">
Const ForReading = 1
Dim strMsg, fileNames, objFile, lineCount(100)
set objFSO = CreateObject("ScriptingFileSystemObject")
set WshShell = WScriptCreateObject("WScriptShell")
strMsg = InputBox("Please enter your file name:")
fileNames = Split(strMsg, " ", -1, 1)
For i = 0 To UBound(fileNames)
Set objFile = objFSOOpenTextFile(fileNames(i) & "txt", ForReading)
Do While objFileAtEndOfStream <> True
objFileReadLine
lineCount(i) = lineCount(i) + 1
Loop
objFileClose
Next
strMsg = ""
For i = 0 To UBound(fileNames)
strMsg = strMsg & lineCount(i)& " "
Next
MsgBox(strMsg)
</script>
</job>
如果你连文件名都不想输,就用下面这个,指定下目录,会统计出该目录下所有txt文件的行数
<job id="HowManyLines">
<script language="VBScript">
Const ForReading = 1
Dim objFSO, fold, f, strMsg, fileNames(100), objFile, lineCount(100),files,i
set objFSO = CreateObject("ScriptingFileSystemObject")
set fold = objFSOGetFolder("C:\Documents and Settings\")
set files = foldFiles
For Each f in files
If objFSOGetExtensionName(f) = "txt" Then
Set objFile = objFSOOpenTextFile(f, ForReading)
Do While objFileAtEndOfStream <> True
objFileReadLine
lineCount(i) = lineCount(i) + 1
Loop
objFileClose
fileNames(i) = fName
i = i + 1
End If
Next
strMsg = ""
For i = 0 To UBound(fileNames)
If fileNames(i)<>"" Then
strMsg = strMsg & fileNames(i) & ":" & lineCount(i)
strMsg = strMsg & Chr(13)
End If
Next
MsgBox(strMsg)
</script>
</job>
Just make a try!
好面熟的问题~
'str是要查找要字符串'1txt是包含str的txt文件
'newfile即“另外”的txt,请自己修改路径和文件名
str="cccccccccccccccc"
src="1txt"'源txt文件
newfile="newfiletxt"'新生成文件
Set fso = WScriptCreateObject("ScriptingFilesystemobject")
Set fs=fsoOpenTextFile(src,1)'以只读方式打开txt
Set fn=fsoCreateTextFile(newfile,True)'如果文件已存在,将覆盖现有文件
Do until fsAtEndOfStream
rcon=fsReadLine
If InStr(1,rcon,str,1)>0 Then
fnWriteline(rcon)
End If
Loop
fsClose
fnClose
分类: 电脑/网络 >> 程序设计 >> 其他编程语言
问题描述:
提示出来的时只能显示第一行
解析:
没人答?那还是我来吧
objectRead(characters)
Read 方法的语法有以下部分:
object 必选。应为 TextStream 对象的名称。
characters 必选。要从文件读的字符数目。
也可以重复调用readline
第一次读第一行,第二次读第二行。。。
还有个readall方法,把所有的字符都读出来
改的头大(查了不少资料,自己又调试了好久 老实说我对instr并不是很了解)
~ 记得要加分哦~
代码开始:
set fso=createobject("scriptingfilesystemobject")
set file=fsoopentextfile("testtxt",1)
i=1
do while fileatendofstream<>true
n=filereadline
if fileatendofstream=-1 then
l=instr(n,"""")
s=s&mid(n,l+1)
p=instr(s,"""")
d=left(s,p-1)
msgbox(d)
i=i+1
end if
loop
fileclose
代码结束
程序已经通过调试 成功啦~
以上就是关于VBS脚本读取如下TXT文本中数据存入变量全部的内容,包括:VBS脚本读取如下TXT文本中数据存入变量、如何运用VBS脚本读写DOS窗口固定字符所在的行数内容、VBS怎么后台读取文本信息要怎么写等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)