Rock with IT
Rock with IT

Reputation: 371

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

Anyone help ...

Upvotes: 1

Views: 420

Answers (3)

Harpreet
Harpreet

Reputation: 11

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.

Upvotes: 1

Vikram.exe
Vikram.exe

Reputation: 4585

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.

Upvotes: 0

user541686
user541686

Reputation: 210765

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?

Upvotes: 1

Related Questions