【问题标题】:VBScript - skip and read lines in text fileVBScript - 跳过和读取文本文件中的行
【发布时间】:2014-04-17 12:35:39
【问题描述】:

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

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
...

您可以看到它看起来很蹩脚,但它确实可以完成工作。 我能够找到一些东西来替换技能部分:

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。

你能帮我吗?

谢谢

【问题讨论】:

    标签: vbscript readline


    【解决方案1】:

    这是另一种方式。

    ' 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
    

    【讨论】:

    • 开箱即用。谢谢。
    • 现在我正在尝试编辑 strText 中的文本并替换其中的一些内容。 strNewText = Replace (strText,"INC",input ) strNewText = Replace (strText,"'","") 它确实将 ' 替换为“空白”,但将 INC 交换为输入值不会。有什么线索吗?
    • Replace() 默认区分大小写。 "INC" 在你的文本字符串中是大写的吗?您可以使用vbTextCompare 参数进行不区分大小写的替换。
    • 这很奇怪。使用我以前的蹩脚方法,一切都很好,INC 变成了输入,'变成了“”。现在它没有。我将订单从 INC 和 ' 更改为 ' 和 INC,现在我只看到第二个 Replace() 启动。为什么?
    • 可能与你提出的方法有关?
    【解决方案2】:

    这行得通吗?注意添加了Do Until 循环。

    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】:

      如果rowcount 大于 2 或小于 30,这将读取一行,否则它将跳过该行,然后您可以在 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多