我目前正在努力去除xml文件中的所有&符号。但是,由于不熟悉编写Windows批处理文件,我在实现这一点上遇到了一些困难。
如果有人可以指定我这个问题,我们将不胜感激。
亲切的问候
答案 0 :(得分:2)
这是一个使用本机脚本工具的强大批处理解决方案
它会从&
file.xml
个字符
type "file.xml"|repl "&" "" L >"newfile.xml"
这使用名为repl.bat
的帮助程序批处理文件 - 从https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
将repl.bat
放在与批处理文件相同的文件夹中或放在路径上的文件夹中。
答案 1 :(得分:0)
这是一个vbs文件。它使用正则表达式进行搜索和替换。它不是基于行的butwhole文件。
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, "ReplaceRegExp"
ReportErrors "Help"
ElseIf objArgs.Count = 1 then
MsgBox "Only one parameter", 16, "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, "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, "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, "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, "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
在命令提示符下。指定了完整路径并且&符号(您的搜索字符串)已转义。
"C:\path\ReplaceRegExp.vbs" "%userprofile%\Desktop\Folder Property List.txt" "^&" ""
请注意,它设计用于开始 - 运行。运行一次,然后安装。
ReplaceRegExp.vbs "%userprofile%\Desktop\Folder Property List.txt" "&" ""
这是模式文档。
设置 特殊字符和序列用于编写正则表达式的模式。下表描述并给出了可以使用的字符和序列的示例。
字符描述
\
将下一个字符标记为特殊字符或文字。例如,“n”匹配字符“n”。 “\ n”匹配换行符。序列“\”匹配“\”和“(”匹配“(”。
^ 匹配输入的开头。
$ 匹配输入的结尾。
匹配前面的字符零次或多次。例如,“zo *”匹配“z”或“zoo”。
匹配前一个字符一次或多次。例如,“zo +”匹配“zoo”但不匹配“z”。
? 匹配前面的字符零或一次。例如,“一个?”?匹配“从不”中的“ve”。
。 匹配除换行符之外的任何单个字符。
(图案) 匹配模式并记住匹配。可以使用Item [0] ... [n]从生成的Matches集合中检索匹配的子字符串。要匹配括号字符(),请使用“(”或“)”。
X | Y 匹配x或y。例如,“z | wood”匹配“z”或“wood”。 “(z | w)oo”匹配“zoo”或“wood”。
{N} n是非负整数。正好匹配n次。例如,“o {2}”与“Bob”中的“o”不匹配,但匹配“foooood”中的前两个o。
{N,} n是非负整数。匹配至少n次。例如,“o {2,}”与“Bob”中的“o”不匹配,并匹配“foooood”中的所有o。 “o {1,}”相当于“o +”。 “o {0,}”相当于“o *”。
{n,m} m和n是非负整数。匹配至少n次,最多m次。例如,“o {1,3}”匹配“fooooood”中的前三个o。 “o {0,1}”相当于“o?”。
[xyz] 一个字符集。匹配任何一个包含的字符。例如,“[abc]”匹配“plain”中的“a”。
[^ xyz] 负字符集。匹配任何未包含的字符。例如,“[^ abc]”匹配“plain”中的“p”。
[a-z] 一系列人物。匹配指定范围内的任何字符。例如,“[a-z]”匹配范围“a”到“z”中的任何小写字母字符。
[^ m-z] 负范围字符。匹配不在指定范围内的任何字符。例如,“[m-z]”匹配不在“m”到“z”范围内的任何字符。
\ b 匹配单词边界,即单词和空格之间的位置。例如,“er \ b”匹配“never”中的“er”而不匹配“verb”中的“er”。
\乙 匹配非单词边界。 “ea * r \ B”与“never early”中的“ear”相匹配。
\ d 匹配数字字符。相当于[0-9]。
\ d 匹配非数字字符。相当于[^ 0-9]。
\˚F 匹配换页符。
\ n 匹配换行符。
\ r 匹配回车符。
\ S 匹配任何空格,包括空格,制表符,换页等。相当于“[\ f \ n \ r \ t \ v]”。
\ S 匹配任何非白色空格字符。相当于“[^ \ f \ n \ r \ t \ t \ v]”。
\吨 匹配制表符。
符\ v 匹配垂直制表符。
\瓦特 匹配任何单词字符,包括下划线。相当于“[A-Za-z0-9_]”。
\ W 匹配任何非单词字符。相当于“[^ A-Za-z0-9_]”。
\ NUM 匹配num,其中num是正整数。回顾记住的比赛的参考。例如,“(。)\ 1”匹配两个连续的相同字符。
\ n 匹配n,其中n是八进制转义值。八进制转义值必须为1,2或3位数。例如,“\ _11”和“\ 011”都匹配制表符。 “\ 0011”相当于“\ 001”&amp; “1”。八进制转义值不得超过256.如果满足,则只有前两位包含表达式。允许ASCII代码用于正则表达式。
\ XN 匹配n,其中n是十六进制转义值。十六进制转义值必须正好是两位数。例如,“\ x41”匹配“A”。 “\ x041”相当于“\ x04”&amp; “1”。允许ASCII代码用于正则表达式。