开发者

Why is this variable appearing as undefined?

I have this code:

FOR %%d IN (c d e f g h i j k l m n o p q r s t u v w x y z) do (
        IF NOT EXIST %%d:\ (
            echo Free drive %%d
            set D=%%d:
        )
)

echo d=%D%
echo Using %D% to map remote drive
subst %D% /d
subst %D% \\path_to_drive

and after this I'm using the D variable to map a free drive. The thing is, when I first run this, and try to map the drive, the script thinks that the D variable is undefined, or contains nothing. If I print it's value when the script ends, with:

set D

I see that it found a drive, and it got it's value. 开发者_StackOverflow社区When I run the script a second time, it will work as expected. It drives me crazy that batch thinks the variable is undefined, and after the script finishes it's execution it has a value. I'd also like to add that this is the only part where the D variable is used, and that there will ALWAYS be at least one free drive, so, the fact that no drives are available isn't true.

EDIT: I've added the lines that I use to map the drive.


Depending on where exactly you use the environment variable this may happen. Keep in mind that cmd expands environment variables in the parsing state of a command. And any command that has a parenthesized block is a single, large command itself. So what you posted above is one command and any environment variables are expanded immediately when cmd reads it, instead of when it's executed.

To circumvent this, try putting a

setlocal enabledelayedexpansion enableextensions

at the top of your batch file and use the environment variable with !D! instead of %D%.


This works for me, can you see if it works for you on your setup, can you also show us how you reference D?

FOR %%d IN (c d e f g h i j k l m n o p q r s t u v w x y z) do (
        IF NOT EXIST %%d:\ (
                echo Free drive %%d
                set D=%%d:
        )
)
echo %D%
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜