2017-11-17 206 views
1

我有类似以下的东西......扩展if语句中设置变量的%userprofile%。批量

set /p dir=Specify Path: 
if exist %dir% (
echo ok 
) else(
echo not ok 
) 

如果用户输入类似%USERPROFILE%的路径或变量

%时,DIR%是用来%USERPROFILE%之内没有扩大

例如

set dir=%userprofile% 
echo %dir% :: this does not expand 
call echo %dir% :: this does expand 
cd %dir% :: does not expand 
call cd %dir% :: expands 

但如何我想有一个if语句中的路径展开。

一直在我的头上,我并不真正了解DelayedExpansion。

谢谢。

+1

你的'SET/P'命令后这样做:'呼叫建立“DIR =%DIR%”' – Squashman

+0

我想我已经得到了我的头周围延迟扩展,这似乎允许改变一个变量是在声明结束之前阅读,如果这是正确的,我认为这对我的情况没有什么帮助,这可能是为什么我很困惑,因为我尝试了所有的组合! &%没有运气。如果('echo %% dir %%')中的/ f %% a做了echo%a我相信也会扩展,但是这对我也没有帮助。:( – Chumbawamba

+0

感谢Squashman现在几乎看起来很明显。 – Chumbawamba

回答

2

如果用户输入环境变量作为路径,则可以使用CALL命令轻松扩展该变量。所以你的代码看起来像这样。

@ECHO OFF 
set /p "dir=Specify Path:" 

CALL set "dir=%dir%" 

if exist "%dir%" (
    echo ok 
) else (
    echo not ok 
)