VBScript - 跳过和读取文本文件中的行 [英] VBScript - skip and read lines in text file

查看:64
本文介绍了VBScript - 跳过和读取文本文件中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我弄清楚/完善我的代码吗?我有一个文件需要编辑并将其中的一些保存到下一个文件中.保存在这里不是问题,只是编辑.我需要跳过 2 行并将接下来的 30 行左右读到内存中.直到现在我一直在使用:

Would you be able to help me figure out/make pretty my code? I have a file I need to edit and save some of it to next file. Saving is not an issue here, only editing. I need to skip 2 lines and read next 30 or so to memory. Until now I've been using:

Const ForReading = 1
Const ForWriting = 2

set WshShell = WScript.CreateObject("WScript.Shell")
strMyDocs = WshShell.SpecialFolders("MyDocuments")
strDesktop = WshShell.SpecialFolders("Desktop")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strDesktop & "\folder\blabla.vbs", ForReading)

strText = objFile.SkipLine & objFile.SkipLine
strText = objFile.ReadLine & vbNewLine & objFile.ReadLine & vbNewLine & 
objFile.ReadLine & vbNewLine & objFile.ReadLine & vbNewLine & objFile.ReadLine & 
vbNewLine & objFile.ReadLine & vbNewLine &_
objFile.ReadLine & vbNewLine & objFile.ReadLine & vbNewLine & objFile.ReadLine & 
vbNewLine & objFile.ReadLine & vbNewLine & objFile.ReadLine & vbNewLine & 
objFile.ReadLine & vbNewLine & objFile.ReadLine & vbNewLine &_
objFile.ReadLine & vbNewLine & objFile.ReadLine & vbNewLine & objFile.ReadLine & 
vbNewLine & objFile.ReadLine & vbNewLine & objFile.ReadLine & vbNewLine & 
objFile.ReadLine & vbNewLine & objFile.ReadLine & vbNewLine &_
objFile.ReadLine & vbNewLine & objFile.ReadLine & vbNewLine & objFile.ReadLine & 
vbNewLine & objFile.ReadLine & vbNewLine & objFile.ReadLine & vbNewLine & 
objFile.ReadLine & vbNewLine & objFile.ReadLine & vbNewLine & objFile.ReadLine
objFile.Close
...

正如您所见,它看起来很蹩脚,但它确实可以胜任.我能够找到一些东西来替换技能线部分:

As you can see it looks pretty lame, it does the job though. I was able to find something to replace skilLine part:

For a = 1 to 30
If ((a =< 2) And (ObjFile.AtEndOfStream <> True)) Then
objFile.SkipLine
Do Until a = 30
objFile.ReadLine
Loop
Else    
objFile.Close
End If

但找不到读取下 28 行的方法.我尝试了很多,它总是读取 28 行,但从第 31 行开始,而不是第 3 行.

but cannot find a way to read next 28lines. I tried a lot and it always reads 28 lines but starting with line 31, not 3.

你能帮我吗?

谢谢

推荐答案

这是另一种方式.

' Read all lines into an array...
a = Split(objFSO.OpenTextFile(strDesktop & "\folder\blabla.vbs", ForReading).ReadAll, vbCrLf)

' Start with the 3rd line and read 28 lines (if available)...
For i = 2 To 29
    If UBound(a) >= i Then strText = strText & a(i) & vbCrLf
Next

这篇关于VBScript - 跳过和读取文本文件中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆