1

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 ...

3 Answers 3

1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

See my edit: was that a typo, or did you really mean to use Set?
thanks i was using space before and after the equals sign. now its working
1

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.

Comments

0

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.

3 Comments

Are you using a .bat file with the above code or are you issuing these commands from a cmd shell? If you are using cmd shell, replace '%%' with '%'. Also, do you have any special character in your file name?
No any special character in file name. But file text contain string, then : (colon symbol )then space and then some string. script is reading upto : from file.
Okay, so you will have to modify the for loop to read space separated values. Use some "delims" tag with for loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.