通过比较文件来查找和替换Findstr?

时间:2014-10-11 06:02:41

标签: powershell cmd

例如。我得到了我需要的所有字符串并将其写入文件。

---------- .\A.txt
    //...etc
        const-string/jumbo v3, "startedinbetween"
        const-string/jumbo v5, "startedinbetween"
        const-string/jumbo v3, "firsttimeappstarted"
        const-string/jumbo v3, "firsttimeappstarted"
    //...etc

然后我改变这个字符串就像这样

---------- .\A.txt
    //...etc
        const-string/jumbo v3, "1"
        const-string/jumbo v5, "2"
        const-string/jumbo v3, "3"
        const-string/jumbo v3, "4"
    //...etc

是否可以在源文件中再次找到它并替换为更改的字符串,前提是该命令没有被删除?像preg_replace或grep。

"const-string/jumbo v3, 'startedinbetween'" => "const-string/jumbo v3, '1'"
"const-string/jumbo v3, 'startedinbetween'" => "const-string/jumbo v3, '2'"
"const-string/jumbo v3, 'firsttimeappstarted'" => "const-string/jumbo v3, '3'"
//etc

2 个答案:

答案 0 :(得分:0)

这是一个查找和替换程序。

On Error Resume Next
Set ShellApp = CreateObject("Shell.Application")
ReportErrors "Creating Shell.App"
set WshShell = WScript.CreateObject("WScript.Shell")
ReportErrors "Creating Wscript.Shell"
Set objArgs = WScript.Arguments
ReportErrors "Creating Wscript.Arg"
Set regEx = New RegExp
ReportErrors "Creating RegEx"
Set fso = CreateObject("Scripting.FileSystemObject")
ReportErrors "Creating FSO"

If objArgs.Count = 0 then
        wscript.echo "No parameters", 16, "Serenity's ReplaceRegExp"
        ReportErrors "Help"
ElseIf objArgs.Count = 1 then
        wscript.echo "Only one parameter", 16, "Serenity's ReplaceRegExp"
        ReportErrors "Help"
ElseIf objArgs.Count = 2 then
        Set srcfile = fso.GetFile(objArgs(0))
        ReportErrors "srcFile"
        If err.number = 0 then Set TS = srcFile.OpenAsTextStream(1, 0)
        If err.number <> 0 then
            wscript.echo err.description & " " & srcFile.path, 48, "Serenity's Search" 
            err.clear
        else
            ReportErrors "TS" & "     " & srcFile.path
            Src=ts.readall
            If err.number = 62 then
                err.clear
            else
                ReportErrors "ReadTS" & "     " & srcFile.path
                regEx.Pattern = objArgs(1) 
                regEx.IgnoreCase = True
                regEx.Global = True
                If regEx.Test(Src) = True then
                    wscript.echo "Found in " & srcfile.path, 64, "Serenity's Search" 
                End If
            End If
        End If
        ReportErrors "Check OK" & "     " & srcFile.path

Elseif objArgs.count = 3 then
        Set srcfile = fso.GetFile(objArgs(0))
        ReportErrors "srcFile"
        If err.number = 0 then Set TS = srcFile.OpenAsTextStream(1, 0)
        If err.number <> 0 then
            wscript.echo err.description & " " & srcFile.path, 48, "Serenity's Search" 
            err.clear
        else
            ReportErrors "TS" & "     " & srcFile.path
            Src=ts.readall
            If err.number = 62 then
                err.clear
            else
                ReportErrors "ReadTS" & "     " & srcFile.path
                regEx.Pattern = objArgs(1) 
                regEx.IgnoreCase = True
                regEx.Global = True
                NewSrc= regEx.Replace(Src, objArgs(2)) 
                If NewSrc<>Src then
                    wscript.echo "Replacement made in " & srcfile.path, 64, "Serenity's Search" 
                    TS.close
                    Set TS = srcFile.OpenAsTextStream(2, 0)
                    ts.write newsrc
                    ReportErrors "Writing file"
                End If
            End If
        End If
        ReportErrors "Check OK" & "     " & srcFile.path


Else
        wscript.echo "Too many parameters", 16, "Serenity's ReplaceRegExp"
        ReportErrors "Help"

ReportErrors "All Others"
End If

Sub ReportErrors(strModuleName)
    If err.number<>0 then wscript.echo "An unexpected error occurred. This dialog provides details on the error." & vbCRLF & vbCRLF & "Error Details " & vbCRLF & vbCRLF & "Script Name" & vbTab & Wscript.ScriptFullName & vbCRLF & "Module" & vbtab & vbTab & strModuleName & vbCRLF & "Error Number" & vbTab & err.number & vbCRLF & "Description" & vbTab & err.description, vbCritical + vbOKOnly, "Something unexpected"
    Err.clear
End Sub

答案 1 :(得分:0)

您可以使用两个本机批处理脚本来修改文件,称为repl.batfindrepl.bat,它们都可以使用Windows正则表达式。

帮助程序批处理文件findrepl.bat(由aacini提供) - 从以下网址下载:https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

帮助程序批处理文件repl.bat(来自dbenham) - 从以下网址下载:https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

它们具有grepsed中的功能。