如何在VBScript中搜索未知日期并替换为我的选择日期?

时间:2016-11-07 21:39:49

标签: replace vbscript

我正在尝试使用此代码查找未知日期并将其替换为我选择的日期,但它会随着"校准日期\ d {2} - \ d {2} - \ d {继续返回4}未被发现"。我知道使用RegEx将允许我正确地执行此操作,但我不确定如何将其合并到现有代码中?我还没有真正使用RegEx,但我愿意尝试。我对ATLAS非常满意。

' This code replaces one string by another inside files looping though    
' subfolders.
' It is vbscript so copy the text below in a .txt file and rename to .vbs
' It will take any non zero bytes file except for extensions you filter 
' out in advance.

Option Explicit
Dim objFilesystem, objFolder, objFiles, objFile, tFile,  objShell,         
objLogFile,objFSO, objStartFolder, colFiles
Dim SubFolder, FileText, bolWriteLog, strLogName, strLogPath, strCount,
strCount2, strOldText, strNewText, strEXT 
bolWriteLog = True
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
Set objFilesystem = WScript.CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("wscript.Shell")
strLogName = "dateX.txt"
strLogPath = "C:\Users\e344765\Desktop\Find and replace" & strLogName


strCount = 0
strCount2 = 0
strOldText="calibration date \d{2}-\d{2}-\d{4}" 
strNewText="calibration date 01-17-2016"
strEXT = "exe,dll,ps"

ShowSubfolders objFSO.GetFolder(objStartFolder)

Sub  ReplaceText(objFile)

strCount = strCount + 1
    WriteLog("Opening " & objFile.Name)
    Set tFile = objFile.OpenAsTextStream(ForReading, TriStateUseDefault)
    FileText = tFile.ReadAll
    tFile.Close
    If   InStr(FileText, strOldText) Then
         WriteLog("Replacing " & strOldText & " with " & strNewText & ".")
         FileText = Replace(FileText, strOldText, strNewText)
            WriteLog("Text replaced")
    Else
            WriteLog(strOldText & " was not found in the file.")
            strCount2 = strCount2 +1
    End If
    Set tFile = objFile.OpenAsTextStream(ForWriting, TriStateUseDefault)
    tFile.Write FileText
    tFile.Close
    FileText = ""
strCount = 0
strCount2 = 0
End Sub

1 个答案:

答案 0 :(得分:0)

Replace()寻找固定/文字字符串;如果您想搜索变量模式,则需要RegExp

>> s = "calibration date 01-17-2016"
>> Set r = New RegExp
>> r.Pattern = "calibration date \d{2}-\d{2}-\d{4}"
>> WScript.Echo InStr(s, r.Pattern)
>> WScript.Echo r.Replace(s, "abc")
>>
0
abc
>>