1.

Solve : "delayed environment variable expansion" & "setlocal"?

Answer»

Recently Dark Blade posted a very interesting batch file USING the "set /p num= "......" command
(see Topic: goto help... - reply 3)

I didn't want to get too far off the tangent and HIJACK the thread, so here is my QUESTION.

How does "delayed environment variable expansion" & "setlocal" tie in with the set/p command. The set/p help(in Windows XP) was a bit hard for me to follow. My google search resulted in more confusion. Is there a simple way to explain the proper uses.

(I was trying to set var= ..., then type %var% > textfile.txt). Repeated attempts(and variations type !var!, ect.) always resulted in an empty file. When I tried to echo the var the result was "echo is off".

A simple explanation would be greatly appreciated.the command:

setlocal enabledelayedexpansion

does what you would expect, but it does not have a lot to do with set /p

I don't know what you were doing to set variable values, but this is how you do it:

set (variable name)=value

with no spaces between the value name & the equals sign

set var=whatever

to see the value:

echo %var%

or to put it in a file:

echo %var% > filename.ext

Perhaps you could post a piece of your code that was giving you trouble?

Quote from: llmeyer1000 on February 20, 2008, 09:07:11 AM

How does "delayed environment variable expansion" & "setlocal" tie in with the set/p command.
Delayed expansion wouldn't have anything to do with the set /p unless the set /p was used inside of a FOR loop.

The SETLOCAL command doesn't have much to do with set /p either except it would clean up / reset the variable(s) after the script has finished executing.

I think your main question is how set /p works and what it does? The /P is basically a way for a batch script to Prompt a user for input. Without any parameters the SET command will simply assign a value t a variable. With SET /P the SET command will ask the user to input some text and then assign that entered text as the value for the variable.

Example: Set the environment variable NAME:
Code: [Select]set Name=llmeyer
echo Name is %Name%Here the variable Name is hard coded with llmeyer
Or for an interactive script you can do:
Code: [Select]set /p Name=Enter your name:
echo Name is %Name%
In both of these cases, the environment variable "Name" would be set when the script was finished. To see what effect SETLOCAL has on the script, you can use
Code: [Select]setlocal
set /p Name=Enter your name:
echo Name is %Name%When the script was finished the environment variable of "Name" would have the same value as before you ran the script, or if it was null / not set (which it probably would be unless you had something that already set it) then it would be null again at the end of the script.

Is that what you were LOOKING for?Thank you both for your help. I am at work right now and can't check out the code from the batch file right now. If I had to guess, I strongly suspect that one of my mistakes was including spaces. I'll try to go back over my test file and sort it out, using the information and suggestions you provided. I'll let you know in the next couple of days how it works out.

Thanks again.If you are still having problems, post your code so we can help you see where the problem is.I really do appreciate your help. I haven't had time to even look at the batch file I was working on. It may be a few days, but I have copied the text from the thread and will get back to it. I will try to work it out myself, with the help already provided, but if I get too bogged down, I will be back with the problem code. If I get it sorted out, I'll let you know also. Thanks again!Hey Guys,

I finally got back to the set /p = ... problem I was having, and Dias de VERANO, you were right on with the "no spaces between the value name & the equals sign" suggestion. Thanks!!! I wasted quite a bit of time on that one.

GuruGary, thanks for clarifying the "delayed environment variable expansion" & "setlocal" usages for me.

I am so far behind the times. Back in 1995, I learned the CHOICE - IF ERRORLEVEL method in DOS 6.22 batch file programming. Now, in Windows XP, it seems choice is gone, with the set /p apparently filling the void. From reading a number of other threads, I can see that set/p is very powerful, but I can't understand Microsoft's logic in dropping the choice command? What if I want to write a batch file that is backward compatible, from Vista to Win 98???

Thanks again!Quote from: llmeyer1000 on March 03, 2008, 08:11:48 AM

... I learned the CHOICE - IF ERRORLEVEL method in DOS 6.22 batch file programming.

Me too.

Quote
... What if I want to write a batch file that is backward compatible, from Vista to Win 98???

Visit:
http://hp.vector.co.jp/authors/VA007219/dkclonesup/choice.html

Lots to read there.

You can download an executable there that will run under XP. Can't remember if it is choice.exe there, or choice.com. Maybe both are there.
No matter... you can grab it, and put it in a dir in your path.



Discussion

No Comment Found