Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 使用批处理文件替换文件中的字符串。获取错误:<;在这个时候,batcch文件是意外的_String_File_Batch File_Replace - Fatal编程技术网

String 使用批处理文件替换文件中的字符串。获取错误:<;在这个时候,batcch文件是意外的

String 使用批处理文件替换文件中的字符串。获取错误:<;在这个时候,batcch文件是意外的,string,file,batch-file,replace,String,File,Batch File,Replace,谁能建议我如何使用脚本替换文件中的字符串?该字符串包含很少的特殊字符(例如:>“”) 我的搜索字符串如下所述 launcher.properties" /> 我的新字符串如下所述 launcher.properties" > <Permission User="Everyone" GenericAll="yes" /> </File> launcher.properties> 当我使用下面的脚本获取错误时 @echo off &setl

谁能建议我如何使用脚本替换文件中的字符串?该字符串包含很少的特殊字符(例如:>“”)

我的搜索字符串如下所述

launcher.properties" />
我的新字符串如下所述

launcher.properties" >     <Permission User="Everyone" GenericAll="yes" /> </File> 
launcher.properties>
当我使用下面的脚本获取错误时

@echo off &setlocal
set "search=launcher.properties" />"
set "replace=launcher.properties" > <Permission User="Everyone"    
GenericAll="yes" /> </File> "
echo replaceing hte string....2222
set "textfile=home.wxs"
set "newfile=home_t2.wxs"
(for /f "delims=" %%i in (%textfile%) do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%newfile%"
del %textfile%
rename %newfile%  %textfile%
@echo off&setlocal
设置“search=launcher.properties”/>”
设置“replace=launcher.properties”>“
回显替换hte字符串…2222
设置“textfile=home.wxs”
设置“newfile=home_t2.wxs”
(对于/f“delims=“%i in(%textfile%)do(
设置“行=%%i”
延迟扩展
设置“行=!行:%search%=%replace%”
回声(!线!
端部
))>%newfile%
删除%textfile%
重命名%newfile%%textfile%
获取以下错误:

The system cannot find the path specified.
< was unexpected at this time.
系统找不到指定的路径。
这在当时是出人意料的。

请在这方面帮助我。

对于如此复杂的替换,我将使用更复杂的脚本。 您可以尝试(文件名之前的
e?
用于评估unicode序列-本例中为引号)

调用replacer.bat e?home.wxs“launcher.properties\u0022/>”“launcher.properties\u0022>”
它是jscript/bat的混合版本,应该使用
.bat
扩展名保存(您可以在任何windows系统上使用它,无需先决条件)


您还可以检查哪些工具更复杂,我认为错误是由(初始)
set
命令和命令行解释器(CLI)的方式造成的解析双引号,因为值中有双引号。CLI通常使用第二个双引号来结束由第一个双引号打开的部分,第四个双引号关闭第三个双引号,依此类推。对于找到的每对双引号之间的所有内容,如分隔符、重定向、管道(
&
|
)未检测到,但在这些对之外可以识别它们。
假设输入文件中双引号字符串之间没有上述特殊字符(例如,
“Everyone”
“yes”
,如
%replace%
字符串中所述),则以下代码应能工作:

@echo off & setlocal
set "search=launcher.properties"" />"
set "replace=launcher.properties"" > <Permission User=""Everyone""    GenericAll=""yes"" /> </File> "
echo replacing hte string....2222
set "textfile=home.wxs"
set "newfile=home_t2.wxs"
(for /F "delims=" %%I in (%textfile%) do (
    set "line=%%I"
    setlocal enabledelayedexpansion
    set "line=!line:"=""!"
    set "line=!line:%search%=%replace%!"
    set "line=!line:""="!"
    echo(!line!
    endlocal
)) > "%newfile%"
del /P "%textfile%"
rename "%newfile%" "%textfile%"
@echo off&setlocal
设置“search=launcher.properties”“/>”
设置“replace=launcher.properties”“>”
echo替换hte字符串…2222
设置“textfile=home.wxs”
设置“newfile=home_t2.wxs”
(对于/F“delims=“%I in(%textfile%)do(
设置“行=%%I”
延迟扩展
设置“行=!行:=”!“
设置“行=!行:%search%=%replace%”
设置“行=!行:”“=!”
回声(!线!
端部
))>“%newfile%”
删除/P“%textfile%”
重命名“%newfile%”“%textfile%”
我在这里要做的是在
搜索
替换
值中,以及在原始文件
%textfile%
的每一读取行中,给两个连续的双引号,而不是每个双引号,并用一个
替换它们。”
在搜索和替换操作之后。在变量扩展/替换的
%%
!!
之间,双引号似乎没有更多特殊意义


注意:要检查
搜索
替换
的最终值,您不能使用
echo
,因为您遇到了与上面所述相同的问题。相反,请键入
设置搜索
设置替换
以显示相应的值。

谢谢您的建议。当我使用替换时r、 我得到的bat文件低于错误replacer.bat(68,3)ADODB.Stream:无法打开文件。@mallikgm是同一目录中的home.wxs?通常这意味着脚本找不到文件。我将
/P
开关添加到
del
命令以停止脚本以供用户提示,因此您可以在删除之前检查输出文件
%newfile%
的内容。
@echo off & setlocal
set "search=launcher.properties"" />"
set "replace=launcher.properties"" > <Permission User=""Everyone""    GenericAll=""yes"" /> </File> "
echo replacing hte string....2222
set "textfile=home.wxs"
set "newfile=home_t2.wxs"
(for /F "delims=" %%I in (%textfile%) do (
    set "line=%%I"
    setlocal enabledelayedexpansion
    set "line=!line:"=""!"
    set "line=!line:%search%=%replace%!"
    set "line=!line:""="!"
    echo(!line!
    endlocal
)) > "%newfile%"
del /P "%textfile%"
rename "%newfile%" "%textfile%"