【发布时间】:2015-09-15 08:57:46
【问题描述】:
我在创建脚本方面没有受过教育,但我尝试根据我在互联网上找到的想法制作一个 vbscript。我的目标是转
[00:15:63]ki[00:16:09]e[00:16:36]ru
进入
[00:15.63]kieru
文件夹中每个文本文件的每行。
我目前正在尝试使用逐行循环来获取普通括号中的第一部分。但是,如果我在循环之前不写数据,我只会得到最后一行。但我不能同时阅读和写作。重复 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
【问题讨论】:
-
要将文件作为“文本”处理,您需要读取所有行并将它们存储在内存中,进行更改并将这些行写回原始文件。在您的情况下,您可以尝试将文件作为二进制文件打开并即时进行更改,如果您只需将所有“[”替换为“”。或者在循环之前打开“输出”文件并将所有行写入循环中的输出文件。
-
感谢您的快速回复。我制作了一个临时文件,它在循环连续中附加了“strLeft”和“strRight”。然后我使用'CopyFile'和覆盖布尔值'true'来覆盖旧文件。之后清除了临时文件。这完成了工作!我必须将新代码放在某处以供参考吗?
标签: loops replace vbscript line-by-line