如何在for循环中的命令中扩展env var [英] How to expand env var in command in for loop

查看:72
本文介绍了如何在for循环中的命令中扩展env var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的for循环,想在下面的脚本中为dir中的每个文件运行一个命令.每当我运行它时,都会出现此错误"INST_PATH \ bin \ testBin.exe在此时是意外的."

I have a simple for loop and want to run a command for each file in dir in a script below. Whenever I run it I get this error "INST_PATH\bin\testBin.exe was unexpected at this time."

我可以看到它无法扩展环境变量.似乎在扩展%% i时也存在问题.

I can see it cant expand the env var. Also seems like there are problems in expanding %%i.

此外,如果我想给变量IF提供默认值,而不是在命令行中提供,我该怎么做?例如如果用户不提供Dir,而我想假设它是当前目录,该如何在脚本中做到这一点?

Also if I want to give a default value to a variable IF, not provided at command line, how do I do that? For e.g. if the user doesn't give the Dir and I want to assume it to be current dir how do I do that in the script?

set Dir=%1
set OutDir=%2
pushd %Dir%
for %i in (*.*) do %INST_PATH%\bin\testBin.exe -I=. --cpp_out=. %%i
popd

预先感谢您的帮助.

推荐答案

两个问题,两个答案:

在批处理文件中,您必须对for变量使用双百分号:

within batchfiles, you have to use double percent-signs for the for variables:

for %%i in (*.*) do echo %%i

要设置默认值,请检查变量是否为空:

to set a default value, check if the variable is empty:

if "%1%=="" (set "dir=Z:\DefaultDir") else (set "dir=%1")

或替代:

set dir=%1
if not defined dir set "dir=Z:\DefaultDir"

这篇关于如何在for循环中的命令中扩展env var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆