开发者

Setting an environment variable

I am using xp. I am facing problem in using Variables.

I am using following code

@echo off set var = "srting"

When i check the value of var using %

set %var%

Environment vari开发者_JAVA技巧able %var% not defined

Anyone help ...


Take out the space before and after the equals sign; IIRC, I think that can cause problems.

Also, you can't put more than one command on a line like that, you have to separate it with ampersands, or instead, change it to this:

@echo off
set var="srting"

Edit:

You said you try:

Set %var%

but %var% is a value, not a variable name. Is that really what you intended?


set %var% Environment variable %var% not defined

White spaces are not allowed in setting variables in DOS batch file.

Try this :

@echo off

set var="srting"

echo %var%

.... that should give you an output "srting" on next line.

If you try now - your own command : set %var%

output should be : "Environment variable srting not defined"

which in my view is correct. Hope that makes sense for you.


If you want to execute different code path based on file content, this should work for you:

@echo off
set FILE_CONTENT=string

for /f %%a in (file.txt) do set var=%%a

if %var%==%FILE_CONTENT% (
 goto MATCHED
 ) else (
     goto NOT_MATCHED
     )

 :MATCHED
 echo "matched"
 goto:EOF

 :NOT_MATCHED
 echo "Not matched"
 goto:EOF

However, if the file name contains 'spaces' or '(' like in 'c:\program files(x86)', the above code will not work. The workaround is to get the short name (probably using: ~dpsx) of the file and then use it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜