Text 替换大型txt文件上的文本函数值输入无效
expiration.txt文件包含197.015行Text 替换大型txt文件上的文本函数值输入无效,text,vbscript,fso,Text,Vbscript,Fso,expiration.txt文件包含197.015行 foo1; 2020-03-01 13:33; foo2; 2020-02-01 08:45; foo3; 2020-01-01 11:30; ... ... ... 在这个大型txt文件中,我需要替换以下文件中的所有日期值: 2020-03-01 13:33至2020-03-01 2020-02-01 08:45至2020-02-01 2020-01-01 11:30至2020-01-01 2018-01-01 12:40至2018-
foo1; 2020-03-01 13:33;
foo2; 2020-02-01 08:45;
foo3; 2020-01-01 11:30;
...
...
...
在这个大型txt文件中,我需要替换以下文件中的所有日期值:
Const ForReading = 1
Const ForWriting = 2
intCount = 0
intIndex = 1
Set oFSO = CreateObject("Scripting.FileSystemObject")
str_input = ""
Set oInFile = oFSO.OpenTextFile("expiration.txt", 1)
str_input = oInFile.ReadAll()
Set oRegEx = CreateObject("VBScript.RegExp")
With oRegEx
.Multiline = True
.Global = True
.Pattern = "(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+);"
End With
Do Until oInFile.AtEndOfStream
str_input = oInFile.ReadLine
If (intCount = 0) Then
str_input = oRegEx.Replace(str_input, "$1-$2-$3;")
Set oInFile = oFSO.OpenTextFile("expiration.txt", 2)
oInFile.Write str_input
oInFile.Close
End If
intCount = intCount + 1
If (intCount = 200) Then
intCount = 0
intIndex = intIndex + 1
oInFile.Close
End If
Loop
oInFile.Close
set oFSO = nothing
WScript.echo "ok"
尝试从大型输入文件中读取每一行,处理该行,然后将其一次写入一个新的输出文件中。如有必要,您可以删除原始输入文件,并在脚本末尾重命名新的输出文件(验证后) 我在您当前的脚本中看到了一些问题:
- 它同时调用
和ReadAll()
,这是不必要的ReadLine()
- 在再次打开同一个文件以进行写入之前,它不会调用原始文件句柄的
李>Close
- 当
为0(零)时,它仅尝试转换输入文件的第一行(以及随后的第200行)intCount
- 正则表达式期望列出秒数,但您的示例
YYYY-MM-DD hh:MM代码>日期时间数据不包含秒,因此正则表达式不匹配
intCount=200
块的用途是什么,所以我从我的答案中删除了它。无论如何,我保持了行计数器intCount
变量的完整性,以防以后需要使用它
这里有一个可能的解决方案
Option Explicit
Const ForReading = 1
Dim oRegEx : Set oRegEx = New RegExp
oRegEx.Multiline = True
oRegEx.Global = True
oRegEx.Pattern = "(\d+)-(\d+)-(\d+)\s(\d+):(\d+);"
Dim intCount : intCount = 0
Dim str_input : str_input = ""
Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim oInFile : Set oInFile = oFSO.OpenTextFile("expiration.txt", ForReading)
Dim oOutFile : Set oOutFile = oFSO.CreateTextFile("expiration~2.txt", True)
Do Until oInFile.AtEndOfStream
str_input = oInFile.ReadLine()
If oRegEx.Test(str_input) Then
oOutFile.WriteLine oRegEx.Replace(str_input, "$1-$2-$3;")
Else
oOutFile.WriteLine str_input
End If
intCount = intCount + 1
Loop
oOutFile.Close
oInFile.Close
Set oOutFile = Nothing
Set oInFile = Nothing
' If necessary, use oFSO to delete the original expiration.txt file here, and rename expiration~2.txt to expiration.txt
Set oFSO = Nothing
Set oRegEx = Nothing
WScript.Echo "Ok"
希望这有帮助