Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.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
Batch file 通过替换“全部”来编辑字符串&&引用;在.bat脚本中具有另一个字符的字符_Batch File - Fatal编程技术网

Batch file 通过替换“全部”来编辑字符串&&引用;在.bat脚本中具有另一个字符的字符

Batch file 通过替换“全部”来编辑字符串&&引用;在.bat脚本中具有另一个字符的字符,batch-file,Batch File,我想编辑.bat脚本中的字符串以替换其中的所有“&”字符。该字符串将用作ffmpeg命令路径的一部分 以下是我尝试的简化版本: 一,/ 这将给出: C:\Users\Administrator\Desktop>set string=aaa & bbb & ccc 'bbb' is not recognized as an internal or external command, operable program or batch file. 'ccc' is not r

我想编辑.bat脚本中的字符串以替换其中的所有“&”字符。该字符串将用作ffmpeg命令路径的一部分

以下是我尝试的简化版本:

一,/

这将给出:

C:\Users\Administrator\Desktop>set string=aaa  & bbb & ccc
'bbb' is not recognized as an internal or external command,
operable program or batch file.
'ccc' is not recognized as an internal or external command,
operable program or batch file.
二,/

这会给出正确的结果,但“这会导致以后ffmpeg命令出现问题:

"aaa_bbb_ccc"
我基本上想得到第二个结果,但没有

我不想使用PowerShell。定制图书馆就可以了。如有任何建议,将不胜感激


(有关详细信息,请编辑)

您的问题在于变量的处理

第一个问题是您
设置string=aaa&bbb&ccc

这无法工作,因为
&
将作为命令分隔符处理

echo%string%
在字符串中有
&
时失败。
如果使用批处理扩展变量,则仍然会分析内容中的特殊字符

要处理这些问题,可以使用扩展集语法。
以及延迟扩展以访问内容

setlocal EnableDelayedExpansion
set "string=aaa&bbb&ccc"
echo Before: !string!
set "string=!string:&=_!"
echo After: !string!

您的代码可以工作,但只能在代码块之外工作。您应该显示更多的代码。充其量只是一个最小、完整的示例我编辑了这个问题并提供了更多详细信息。我建议在变量赋值后启用延迟扩展,这样它甚至可能包含
和/或
^
没有任何问题。。。
"aaa_bbb_ccc"
setlocal EnableDelayedExpansion
set "string=aaa&bbb&ccc"
echo Before: !string!
set "string=!string:&=_!"
echo After: !string!