使用批量变量中的字符串修整字符串
问题描述:
如果参数无效,我有一个批次将显示使用情况。使用批量变量中的字符串修整字符串
例如,
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
如何achived呢?任何帮助,将不胜感激。谢谢。
(顺便说一句,请修改如果如此混乱的问题。)
答
试图展开的表达不止一次在一条线上,但是解析器不能猜测其百分比是对。
set todisplay=%actions:%toremove% =%
你可以在这里使用延迟扩展
setlocal EnableDelayedExpansion
set toDisplay=!actions:%toRemove% =!
首先%的文档,删除%将扩大再行看起来像
set toDisplay=!actions:update =!
和惊叹号表达进行评估。
+0
彻底的答案应该得到复选标记。 :d – quinekxi 2012-07-26 13:29:10
答
幸运的是,我找到了解决方案,我的问题,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
该程序是否仅支持一个参数或多个参数? – LittleBobbyTables 2012-07-26 13:20:31
要参数但第二个参数应该是正确的。 – quinekxi 2012-07-26 13:22:29