Batch file 在批处理中使用变量中的字符串修剪字符串
我有一个批处理,如果您的参数无效,它将显示用法 例如Batch file 在批处理中使用变量中的字符串修剪字符串,batch-file,trim,Batch File,Trim,我有一个批处理,如果您的参数无效,它将显示用法 例如 Running "sample.bat update fuh" :usageactions set actions=%* set toremove=update set todisplay=%actions:%toremove% =% echo Error: Invalid Arguments - '%todisplay%'. echo Type sample.bat %toremove% -h f
Running "sample.bat update fuh"
:usageactions
set actions=%*
set toremove=update
set todisplay=%actions:%toremove% =%
echo Error: Invalid Arguments - '%todisplay%'.
echo Type sample.bat %toremove% -h for usage.
预期产出:
Error: Invalid Arguments - 'fuh'.
Type sample.bat update -h for usage
但我的输出是:
Error: Invalid Arguments - 'update'.
Type sample.bat update -h for usage
如何实现它?任何帮助都将不胜感激。谢谢
(顺便说一句,如果这个问题如此令人困惑,请修改它。)您尝试在一行中多次展开表达式,但解析器无法猜测哪些百分比是成对的
set todisplay=%actions:%toremove% =%
您可以在这里使用延迟扩展
setlocal EnableDelayedExpansion
set toDisplay=!actions:%toRemove% =!
首先将扩展%toRemove%,然后该行看起来像
set toDisplay=!actions:update =!
然后对感叹号表达式求值。幸运的是,我找到了问题的解决方案,doh。不管怎样,这是解决办法
:usageactions
setLocal EnableDelayedExpansion
set actions=%*
set toremove=update
set todisplay=!actions:%toremove% =!
echo Error: Invalid Arguments - '%todisplay%'.
echo Type sample.bat %toremove% -h for usage.
endlocal
这将显示
Error: Invalid Arguments - 'fuh'.
Type sample.bat update -h for usage
程序只支持一个参数还是多个参数?对于参数,但第二个参数应该是正确的。