VBScript - skip and read lines in text file

2019-03-04 23:43发布

问题:

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

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.

Could you help me?

Thanks

回答1:

Here's another way.

' 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


回答2:

Does this work? Note the addition of the Do Until loop.

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)
objfile.SkipLine
objfile.SkipLine
Dim strText

Do Until objFile.AtEndOfStream
    If(Len(strText) > 0) Then strText = strText & vbNewLine
    strText = strText & objFile.ReadLine
Loop

objFile.Close


回答3:

This will read a line if the rowcount is greater than 2 or less than 30, else it will skip the line, you can then do something with that line in strText

rowcount = 1

Do While NOT objFile.AtEndOfStream

    if((rowcount > 2) And (rowcount < 30))Then

        strText = objTextFile.Readline
        'or strText = strText & vbCrLf & objTextFile.Readline

    Else

        strText = objTextFile.SkipLine 


    End If
rowcount = rowcount + 1
Loop