Windows 用通配符掩码替换文件内容
我已经搜索了这么多,但找不到类似的主题,所以我决定写一个。我希望在我的Windows批处理文件.bat中有一个命令,可以用通配符掩码替换一个或多个文件的内容。例如:Windows 用通配符掩码替换文件内容,windows,batch-file,Windows,Batch File,我已经搜索了这么多,但找不到类似的主题,所以我决定写一个。我希望在我的Windows批处理文件.bat中有一个命令,可以用通配符掩码替换一个或多个文件的内容。例如: replace *.txt "searchin? str?ng*" "replacing string" 如果可能的话,有人能告诉我吗 非常感谢,这是一个vbs程序 ReplaceRegEx.vbs Filename SearchString [ReplaceString] 这是代码 On Error Resume Next S
replace *.txt "searchin? str?ng*" "replacing string"
如果可能的话,有人能告诉我吗
非常感谢,这是一个vbs程序
ReplaceRegEx.vbs Filename SearchString [ReplaceString]
这是代码
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
MsgBox "No parameters", 16, "Serenity's ReplaceRegExp"
ReportErrors "Help"
ElseIf objArgs.Count = 1 then
MsgBox "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
Msgbox 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
Msgbox "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
Msgbox 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
Msgbox "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
MsgBox "Too many parameters", 16, "Serenity's ReplaceRegExp"
ReportErrors "Help"
ReportErrors "All Others"
End If
Sub ReportErrors(strModuleName)
If err.number<>0 then Msgbox "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
这是一个vbs程序
ReplaceRegEx.vbs Filename SearchString [ReplaceString]
这是代码
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
MsgBox "No parameters", 16, "Serenity's ReplaceRegExp"
ReportErrors "Help"
ElseIf objArgs.Count = 1 then
MsgBox "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
Msgbox 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
Msgbox "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
Msgbox 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
Msgbox "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
MsgBox "Too many parameters", 16, "Serenity's ReplaceRegExp"
ReportErrors "Help"
ReportErrors "All Others"
End If
Sub ReportErrors(strModuleName)
If err.number<>0 then Msgbox "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
这是一个vbs程序
ReplaceRegEx.vbs Filename SearchString [ReplaceString]
这是代码
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
MsgBox "No parameters", 16, "Serenity's ReplaceRegExp"
ReportErrors "Help"
ElseIf objArgs.Count = 1 then
MsgBox "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
Msgbox 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
Msgbox "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
Msgbox 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
Msgbox "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
MsgBox "Too many parameters", 16, "Serenity's ReplaceRegExp"
ReportErrors "Help"
ReportErrors "All Others"
End If
Sub ReportErrors(strModuleName)
If err.number<>0 then Msgbox "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
这是一个vbs程序
ReplaceRegEx.vbs Filename SearchString [ReplaceString]
这是代码
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
MsgBox "No parameters", 16, "Serenity's ReplaceRegExp"
ReportErrors "Help"
ElseIf objArgs.Count = 1 then
MsgBox "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
Msgbox 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
Msgbox "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
Msgbox 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
Msgbox "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
MsgBox "Too many parameters", 16, "Serenity's ReplaceRegExp"
ReportErrors "Help"
ReportErrors "All Others"
End If
Sub ReportErrors(strModuleName)
If err.number<>0 then Msgbox "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
Windows没有可以执行此操作的本机命令行工具(PowerShell除外)。但是,像sed这样的unix实用程序的Windows端口可以很好地完成这项工作。任何支持正则表达式搜索和替换的实用程序都可以很好地工作,尽管通配符的语法与示例中的不同。正则表达式功能非常强大,您需要阅读一些关于如何使用它们的教程。网上有很多教程 我已经编写了一个函数,它在stdin上执行正则表达式搜索/替换,并将结果写入stdout。该实用程序是纯脚本,从XP开始将在任何现代Windows机器上运行。脚本中嵌入了完整的文档 使用REPL.BAT,您的示例可以实现为:
@echo off
for %%F in (*.txt) do (
type "%%F" | repl "searchin. str.ng.*" "replacing string" >"%%F.new"
move /Y "%%F.new" "%%F"
)
请注意,问题中的?
(任意1个字符)在正则表达式中表示为
,而*
(任意0个或更多字符)表示为*
上述内容可以轻松转换为通用的REPLACE.BAT“命令”
然后,将使用以下函数调用上述函数:
replace *.txt "searchin. str.ng.*" "replacing string"
这假定REPL.BAT和REPLACE.BAT都在当前目录中,或者在路径中的某个位置。Windows没有可以执行此操作的本机命令行工具(PowerShell除外)。但是,像sed这样的unix实用程序的Windows端口可以很好地完成这项工作。任何支持正则表达式搜索和替换的实用程序都可以很好地工作,尽管通配符的语法与示例中的不同。正则表达式功能非常强大,您需要阅读一些关于如何使用它们的教程。网上有很多教程 我已经编写了一个函数,它在stdin上执行正则表达式搜索/替换,并将结果写入stdout。该实用程序是纯脚本,从XP开始将在任何现代Windows机器上运行。脚本中嵌入了完整的文档 使用REPL.BAT,您的示例可以实现为:
@echo off
for %%F in (*.txt) do (
type "%%F" | repl "searchin. str.ng.*" "replacing string" >"%%F.new"
move /Y "%%F.new" "%%F"
)
请注意,问题中的?
(任意1个字符)在正则表达式中表示为
,而*
(任意0个或更多字符)表示为*
上述内容可以轻松转换为通用的REPLACE.BAT“命令”
然后,将使用以下函数调用上述函数:
replace *.txt "searchin. str.ng.*" "replacing string"
这假定REPL.BAT和REPLACE.BAT都在当前目录中,或者在路径中的某个位置。Windows没有可以执行此操作的本机命令行工具(PowerShell除外)。但是,像sed这样的unix实用程序的Windows端口可以很好地完成这项工作。任何支持正则表达式搜索和替换的实用程序都可以很好地工作,尽管通配符的语法与示例中的不同。正则表达式功能非常强大,您需要阅读一些关于如何使用它们的教程。网上有很多教程 我已经编写了一个函数,它在stdin上执行正则表达式搜索/替换,并将结果写入stdout。该实用程序是纯脚本,从XP开始将在任何现代Windows机器上运行。脚本中嵌入了完整的文档 使用REPL.BAT,您的示例可以实现为:
@echo off
for %%F in (*.txt) do (
type "%%F" | repl "searchin. str.ng.*" "replacing string" >"%%F.new"
move /Y "%%F.new" "%%F"
)
请注意,问题中的?
(任意1个字符)在正则表达式中表示为
,而*
(任意0个或更多字符)表示为*
上述内容可以轻松转换为通用的REPLACE.BAT“命令”
然后,将使用以下函数调用上述函数:
replace *.txt "searchin. str.ng.*" "replacing string"
这假定REPL.BAT和REPLACE.BAT都在当前目录中,或者在路径中的某个位置。Windows没有可以执行此操作的本机命令行工具(PowerShell除外)。但是,像sed这样的unix实用程序的Windows端口可以很好地完成这项工作。任何支持正则表达式搜索和替换的实用程序都可以很好地工作,尽管通配符的语法与示例中的不同。正则表达式功能非常强大,您需要阅读一些关于如何使用它们的教程。网上有很多教程 我已经编写了一个函数,它在stdin上执行正则表达式搜索/替换,并将结果写入stdout。该实用程序是纯脚本,从XP开始将在任何现代Windows机器上运行。脚本中嵌入了完整的文档 使用REPL.BAT,您的示例可以实现为:
@echo off
for %%F in (*.txt) do (
type "%%F" | repl "searchin. str.ng.*" "replacing string" >"%%F.new"
move /Y "%%F.new" "%%F"
)
请注意,问题中的?
(任意1个字符)在正则表达式中表示为
,而*
(任意0个或更多字符)表示为*
上述内容可以轻松转换为通用的REPLACE.BAT“命令”
然后,将使用以下函数调用上述函数:
replace *.txt "searchin. str.ng.*" "replacing string"
这假设REPL.BAT和REPLACE.BAT都在您当前的目录中,或者在您路径中的某个地方。我相信这涵盖了相同的主题:@holtavolt:您提到的问题是用另一个固定字符串替换一个固定字符串。然而,dbenhams的答案(
repl.bat
)进行正则表达式搜索,非常适合这个问题。该页面上有多个答案支持正则表达式