Setting an environment variable

社会主义新天地 提交于 2020-01-05 07:09:10

问题


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 variable %var% not defined

Anyone help ...


回答1:


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.




回答2:


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?




回答3:


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.



来源:https://stackoverflow.com/questions/4568737/setting-an-environment-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!