1

I have no education in creating scripts but I tried making a vbscript based on ideas that I found on the internet. My objective is to turn

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

into

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

per line for each text file in a folder.

I'm currently trying to use a line by line loop to get the first part in normal brackets. However, if I don't write the data before it loops I only get the last line. But I can't read and write at the same time. Repeating the tFile to read resets the loop. What should I do? This is the current code for it right now (where I'm lacking a new set for variable tFile after the first loop):

'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
  • to process the files as "text", you need read all lines and store them in memory, do the changes and write the lines back to the original file. In your case, you can try open the file as binary and do the changes on-fly if all you need is to replace all "[" to "<" and all "]" to ">". Or open the "output" file before the loop and write all lines to the output file in your loop. – Tim3880 Jun 29 '15 at 01:31
  • Thank you for your fast reply. I made a temporary file which appends the 'strLeft' and 'strRight' in a looping succession. I then used 'CopyFile' with the overwrite boolean to 'true' to overwrite the old file. Cleared the temporary file afterwards. This did the job! Do I have to put the new code somewhere for reference? – Stephan Jonkers Jun 29 '15 at 03:06

1 Answers1

1

As advised by Tim3880, I wrote down the data into a separate temporary file in the loop, and moved the temporary file back to the original file afterwards, thus overwriting it. This way I avoiding reading and writing the same file, or restarting the loop over and over again. Here's the finalized code for this specific problem:

'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