1

我在创建脚本方面没有受过教育,但我尝试根据我在互联网上找到的想法制作一个 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
4

1 回答 1

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
于 2015-06-29T12:20:18.113 回答