我正在尝试制作转换脚本,但我遇到了循环

时间:2015-06-29 01:18:10

标签: loops replace vbscript line-by-line

我没有创建脚本的教育,但我尝试根据我在互联网上找到的想法制作一个vbscript。我的目标是转向

  

[00:15:63] ki [00:16:09] e [00:16:36] ru

  

[00:15.63] ki< 00:16.09> e< 00:16.36> ru

文件夹中每个文本文件的每行。

我目前正在尝试使用逐行循环来获取正常括号中的第一部分。但是,如果我在循环之前没有写入数据,那么我只得到最后一行。但我不能同时读写。重复tFile读取重置循环。我该怎么办?这是它的当前代码(我在第一个循环后缺少一个新的变量tFile集):

'Strings for text values
strEXT = "txt"
Set objFolder = objFilesystem.GetFolder(SubFolder)
Set objFiles = objFolder.Files

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.Pattern = "(\d{2}):(\d{2}):(\d{2})"

strCount = 0
strCount2 = 0

'Parse each file and perform replacements
For Each objFile In objFiles
   If Right(LCase(objFile.Name), 3) = strEXT Then
      strCount = strCount + 1
     Set tFile = objFile.OpenAsTextStream(ForReading, TriStateUseDefault) 
     do until tFile.atEndOfStream
            strNextLine = tFile.ReadLine
        If Len(strNextLine) > 0 Then
            strLeft = Left(strNextLine, 10)
            strRight = Mid(strNextLine, 11)
        End If
            strRight = Replace(strRight, "]", ">")      ' ] to >
            strRight = Replace(strRight, "[", "<")      ' [ to < 
            strLeft = objRegEx.Replace _
            (strLeft, "$1:$2.$3")                   ':xx: to :xx.
            strRight = objRegEx.Replace _
            (strRight, "$1:$2.$3")                  ':xx: to :xx.
    tFile.Close
    Set tFile = objFile.OpenAsTextStream(ForWriting, TriStateUseDefault)
         tFile.Write strLeft 
         tFile.Write strRight
         tFile.Close
     loop
         tFile.Close
  end if
Next

1 个答案:

答案 0 :(得分:1)

正如Tim3880所建议的,我将数据写入循环中的单独临时文件,然后将临时文件移回原始文件,从而覆盖它。这样我就可以避免读取和写入同一个文件,或者一遍又一遍地重新启动循环。以下是此特定问题的最终代码:

'Strings for text values
strEXT = "txt"
Set objFolder = objFilesystem.GetFolder(SubFolder)
Set objFiles = objFolder.Files

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.Pattern = "(\d{2}):(\d{2}):(\d{2})"

strCount = 0
strCount2 = 0

'Parse each file and perform replacements
For Each objFile In objFiles
  If Right(LCase(objFile.Name), 3) = strEXT Then
    strCount = strCount + 1
    Set tFile = objFile.OpenAsTextStream(ForReading, TriStateUseDefault) 
    strTemp = "c:\log.txt"
    Set TempFile = objFilesystem.GetFile(strTemp)
    Set tFile2 = TempFile.OpenAsTextStream(8,-2)    

    do until tFile.atEndOfStream
          strNextLine = tFile.ReadLine
      If Len(strNextLine) > 0 Then
          strLeft = Left(strNextLine, 10)
          strRight = Mid(strNextLine, 11)
      End If
          strRight = Replace(strRight, "]", ">")        ' ] to >
          strRight = Replace(strRight, "[", "<")        ' [ to < 
          strLeft = objRegEx.Replace _
          (strLeft, "$1:$2.$3")                 ':xx: to :xx.
          strRight = objRegEx.Replace _
          (strRight, "$1:$2.$3")                    ':xx: to :xx.
      tFile2.Write strLeft & strRight & vbCrlf    'write per line
    loop
      tFile.Close
      tFile2.Close
      objFilesystem.CopyFile strTemp,objFile,true 'overwrite old file
      Set tFile2 = TempFile.OpenAsTextStream(2,-2)
      tFile2.Write ""
      tFile2.Close
  End If
Next