Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops I';我试图制作一个转换脚本,但我';我被套圈缠住了_Loops_Replace_Vbscript_Line By Line - Fatal编程技术网

Loops I';我试图制作一个转换脚本,但我';我被套圈缠住了

Loops I';我试图制作一个转换脚本,但我';我被套圈缠住了,loops,replace,vbscript,line-by-line,Loops,Replace,Vbscript,Line By Line,我没有受过创建脚本的教育,但我尝试根据在互联网上找到的想法制作vbscript。我的目标是 [00:15:63]ki[00:16:09]e[00:16:36]ru 进入 基鲁 文件夹中每个文本文件的每行 我目前正在尝试使用一行一行的循环来获取普通括号中的第一部分。然而,如果我不在循环之前写入数据,我只得到最后一行。但是我不能同时读和写。重复要读取的tFile将重置循环。我该怎么办?这是当前的代码(在第一个循环之后,我缺少变量tFile的新集合): 用于文本值的字符串 strEXT=“txt” 设

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

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

进入

基鲁

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

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

用于文本值的字符串 strEXT=“txt” 设置objFolder=objFilesystem.GetFolder(子文件夹) 设置objFiles=objFolder.Files 设置objRegEx=CreateObject(“VBScript.RegExp”) objRegEx.Global=True Pattern=“(\d{2}):(\d{2}):(\d{2}):(\d{2})” strCount=0 strCount2=0 '解析每个文件并执行替换 对于objFiles中的每个objFile 如果正确(LCase(objFile.Name),3)=strEXT然后 strCount=strCount+1 设置tFile=objFile.OpenAsTextStream(ForReading,TriStateUseDefault) 直到tFile.atEndOfStream strNextLine=tFile.ReadLine 如果Len(strNextLine)>0,则 strLeft=左(strNextLine,10) strRight=Mid(strNextLine,11) 如果结束 strRight=替换(strRight,“]”,“>””]到>
strRight=替换(strRight,“[”,“按照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
用于文本值的字符串 strEXT=“txt” 设置objFolder=objFilesystem.GetFolder(子文件夹) 设置objFiles=objFolder.Files 设置objRegEx=CreateObject(“VBScript.RegExp”) objRegEx.Global=True Pattern=“(\d{2}):(\d{2}):(\d{2}):(\d{2})” strCount=0 strCount2=0 '解析每个文件并执行替换 对于objFiles中的每个objFile 如果正确(LCase(objFile.Name),3)=strEXT然后 strCount=strCount+1 设置tFile=objFile.OpenAsTextStream(ForReading,TriStateUseDefault) strTemp=“c:\log.txt” Set TempFile=objFilesystem.GetFile(strTemp) 设置tFile2=TempFile.OpenAsTextStream(8,-2) 直到tFile.atEndOfStream strNextLine=tFile.ReadLine 如果Len(strNextLine)>0,则 strLeft=左(strNextLine,10) strRight=Mid(strNextLine,11) 如果结束 strRight=替换(strRight,“]”,“>””]到>
strRight=Replace(strRight,“[”,”要将文件处理为“文本”,您需要读取所有行并将它们存储在内存中,进行更改并将这些行写回原始文件。在您的情况下,您可以尝试以二进制形式打开文件,如果您只需要将所有“[”替换为“”,则可以立即进行更改。或者打开“输出”"在循环之前归档,并将所有行写入循环中的输出文件。感谢您的快速回复。我创建了一个临时文件,在循环中连续添加“strLeft”和“strRight”。然后我使用“CopyFile”将覆盖布尔值设置为“true”来覆盖旧文件。之后清除了临时文件。这样就完成了工作!我有了吗把新代码放在什么地方供参考?
'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