How to change SET /P statements on the fly?

Microsoft Windows xp professional w/serv...
December 12, 2009 at 16:02:14
Specs: Windows XP
What's the simplest way to display default options that can change, in SET /P statements?

For example:

The default options would be stored in a text file “Settings.txt” like this:

1=Number of copies
Max=Write speed
Y=Eject disc
Y=Play sound

I extract the options on the left of the equals sign and assign them to their respective variables. (Like Max is assigned to a variable WriteSpeed)

-----

I give the user a chance to change any options by using a series of SET statements something like this ( Default values are indicated by enclosing them in parenthesis. Supporting code not shown for simplification):

set /p NbrCopies=How many copies? (1) to 99:
set /p WriteSpeed=Select write speed. (Max), 32x, 16x, 8x, 4x, 2x, 1x:
set /p EjectDisc=Eject disc after operation? (y=yes), n=no:
set /p PlaySnd=Play sound after operation? (y=yes), n=no:

If the user presses “Enter” alone, the existing default option remains in the variable.

-----

Now say the user changes the “WriteSpeed” default option to “8x=Write speed”. This change is stored in the default text file. When the batch file is run, 8x is assigned to the variable “WriteSpeed” instead of “Max”.

The set /p statement for the write speed needs to be changed to this:
set /p WriteSpeed=Select write speed. Max, 32x, 16x, (8x), 4x, 2x, 1x:

I know I could make a whole slew of different options like this:
If “%WriteSpeed%”==”Max” set WSpeed=(Max), 32x, 16x, 8x, 4x, 2x, 1x
If “%WriteSpeed%”==”32x” set WSpeed=Max, (32x), 16x, 8x, 4x, 2x, 1x
If “%WriteSpeed%”==”16x” set WSpeed=Max, 32x, (16x), 8x, 4x, 2x, 1x
If “%WriteSpeed%”==”8x” set WSpeed=Max, 32x, 16x, (8x), 4x, 2x, 1x
If “%WriteSpeed%”==”4x” set WSpeed=Max, 32x, 16x, 8x, (4x), 2x, 1x
If “%WriteSpeed%”==”2x” set WSpeed=Max, 32x, 16x, 8x, 4x, (2x), 1x
If “%WriteSpeed%”==”1x” set WSpeed=Max, 32x, 16x, 8x, 4x, 2x, (1x)

and then use:

set /p WriteSpeed=%WSpeed%:

-----

But is there a simpler way to do it?
Thanks,
Sky


See More: How to change SET /P statements on the fly?

Report •


#1
December 12, 2009 at 20:46:15
good problem! I'll throw my dart (blindfolded, as usual)

@echo off && setlocal enabledelayedexpansion
set /a cc=0
for /f "tokens=* delims=" %%a in (list) do (
set mm!cc!=%%a
set /a cc+=1
echo %mm!cc!%
echo mm!cc!)
set /a cc-=1
for /L %%b in (0 1 !cc!) do (
set pline=!mm%%b!
set hline=!pline!
echo prompt is !pline!
set /p gg=!pline!
if "!gg!" neq "" (
:: remove old pars
set pline=!pline:^(=!
set pliine=!pline:^)=!
::set new pars
echo prompt stripped of default: !pline!
pause
set ff=(!gg!^)
echo ff is !ff!
set hold=!pline!
call :fff
)
)
move /y newlist list
goto :EOF

:fff
set pline=!pline:%gg%=%ff%!
if "!hold!" neq "!pline!" (
echo new prompt is !pline!
echo !pline! >> newlist
) else (
echo %gg% not an option
echo !hline! >> newlist)


wow, that was "fun". i "like to never" got those darn parentheses to cooperate, among lots other things. they really foul up the pipeline! this one may still need escaped:
set ff=(!gg!^)
to:
set ff=^(!gg!^)

might be why i needed a "call" to :fff instead of just having the code right there...
this one seems unstable, things that should
have worked didn't, and things i did that finally made it work should not have been critical, so there's something wrong/unstable (probably having to do with (), i really really wish you would have used [] instead!)
i don't know! it worked last time i tried it, but after midnight, who knows? all debugging left in so you can test. This was a "fun" problem, but I tend to be masochistic... I'm sure you'll get better answers, just thought i'd "throw my dart".
ps: i just now noticed i worked only on "cosmetics" and did nothing to handle the actual default assignment! Also error-input handling is very poor. i'll leave this posted as-is but it needs much more/better code.


Report •

#2
December 12, 2009 at 23:14:01
-----------------------------
nbrane said:
good problem! I'll throw my dart (blindfolded, as usual)...snip...
-----------------------------

Whoa, that's a solution and a half. I think the brute force method I originally posted might be simpler (but not as much fun).. ;o)

Any one else care to post a simple solution?
Sky


Report •

#3
December 13, 2009 at 00:54:44
I find myself with little patience these days. Can we restate the issue as:

use defaults unless user sets other values


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
December 13, 2009 at 10:51:07
----------
Mechanix2go said:
I find myself with little patience these days. Can we restate the issue as:

use defaults unless user sets other values
----------

Hi Mechanix2go,

Sorry but I'm not sure what you mean by restate the issue as:

"use defaults unless user sets other values"

Was my original post too long and detailed? If so, I genuinely apologize. I didn't know how else to provide all of the information that others might need to come up with simpler code.

Actually I was hoping that you might respond with a solution since you're probably the smartest batch file coder that I've ever seen.

Best regards,
Sky


Report •

#5
December 14, 2009 at 00:48:40
Hi Sky, Thanks for the encouragement. If you don't have CHOICE you can use the one from DOS 6.22 or WIN98.

====================================
@echo off & setLocal EnableDELAYedExpansion

for %%n in (1 2 3) do (set v%%n=)

choice use defaults ?

goto :!errorlevel!

:2
set /p v1= v1 ? : & if not defined v1 set v1=abc
set /p v2= v2 ? : & if not defined v2 set v2=def
set /p v3= v3 ? : & if not defined v3 set v3=xyz
goto :main

:1
set v1=abc
set v2=def
set v3=xyz

:main
echo vars are set
set v


=====================================
Helping others achieve escape felicity

M2


Report •

#6
December 14, 2009 at 14:27:42
----------
mechanix2go said:
Hi Sky, Thanks for the encouragement. If you don't have CHOICE you can use the one from DOS 6.22 or WIN98. .....snip.....
----------

Hi m2,

After sitting here looking at your code for about 5 minutes, I think I might understand how to use it now. Very nice.

However, at the command prompt I just typed CHOICE /? and got the error msg that it does not exist on my WinXP Pro PC. I want my batch file to work on any PC running WinXP, Vista or Win7 without the user having to load anything else, so I need to avoid using CHOICE.

Thanks much for taking the time,
Sky


Report •

#7
December 14, 2009 at 22:11:36
I thought CHOICE had been left out of w2k but included in versions since.

To confirm, I just looked at my XP pro CD and sure enough, it's there.

Run this at the prompt:

attrib /s c:\choice.*

[assuming c: is the OS drive]


=====================================
Helping others achieve escape felicity

M2


Report •

#8
December 15, 2009 at 05:18:25
----------
mechanix2go said:
snip...
Run this at the prompt:
attrib /s c:\choice.*
[assuming c: is the OS drive]
----------

Tried that. When I press ENTER, the underline cursor goes to the next line and then cmd hangs. Unable to type anything. I have to kill cmd by clicking the red X.

Sky


Report •

#9
December 16, 2009 at 03:54:47
OK, now we know you have a corrupt windows installation.

Check the drive for errors. Then run SFC.

Then, at the promp, try ATTRIB again.


=====================================
Helping others achieve escape felicity

M2


Report •

#10
December 16, 2009 at 07:17:43
Not sure about that, I imported choice.com from another OS (Windows 98 I think) into my Windows XP. It's too useful to not have it, but it indeed seems that CHOICE.com is not fully standard (on XP). Maybe I'm missing out somewhere, but that is how I see it.

Anyway, to the question of the threadstarter ... and a good question it is. First of all, I would make my setting script (settings.txt) fully runnable, so that I can run it when I manage to change the extension. Example :

set Number_of_copies=1
set Write_speed=Max
set Eject_disc=Y
set Play_sound=Y

If you want to run this one, just copy settings.txt to settings.cmd, and run. OK, the format is less nice, but it's easier for a programmer to work with. If you really want a "nice looking" TXT file, it can be done, only the manipulations would be more complex. Instead of just copying the file to a file with another extension, you would need to parse. Nothing very special, just have to do it. Depends how important the TXT file is.

OK, then. I was just thinking like this:

C:\>set Number_of_copies=1

C:\>set var1=%Number_of_copies%

C:\>set /p var1=Give value for Number of copies (%Number_of_copies%)
Give value for Number of copies (1)

C:\>echo %var1%
1

C:\>


Explanation : the first command sets the default value. In my above info, that would mean : running the CMD file.

The second command takes the default value as current value.

The third command asks for a value. You see it display the default value, I just pressed enter.

The fourth command shows that the third command did not uptake any value at all, which means it is left with the one previously set, which is the default one.


There you go, as simple as I could think of it ...


Report •

#11
December 16, 2009 at 10:23:19
----------
Mechanix2Go said:

OK, now we know you have a corrupt windows installation.
Check the drive for errors. Then run SFC.
Then, at the promp, try ATTRIB again.
----------

Hi M2,

Since you scared me, I asked my friend Google. Here's what he said:

As the Windows operating system evolved to Windows 95 and then Windows 98, the Choice command came along for the ride. But when Windows 2000 came on to the scene, the Choice command was absent. It wasn’t included in Windows XP either. While you could download the Choice command and add it to Windows 2000 or Windows XP, it just wasn’t the same as having it available as a native command –especially when you were sharing your batch files with other folks.
Well, I recently discovered that Microsoft put the Choice command back in Windows Vista and made it a bit more powerful.
http://blogs.techrepublic.com.com/w...

Below is a simple example of how you can implement the choice options into your batch files. Each line that is in red can be left out of the batch file. They have been included to help explain some of what the batch file means. Windows 2000 and Windows XP users will need to substitute the choice command with the set command; see our set command page for additional help and information with this command.
http://www.computerhope.com/batch.htm

Whew, I was worried for a while,
Sky


Report •

#12
December 16, 2009 at 11:00:27
----------
tvc said:

First of all, I would make my setting script (settings.txt) fully runnable, so that I can run it when I manage to change the extension. Example :

set Number_of_copies=1
set Write_speed=Max
set Eject_disc=Y
set Play_sound=Y

If you want to run this one, just copy settings.txt to settings.cmd, and run.
----------

Hi tvc,

On my very first post on computing.net, I posted in the WinXP forum by mistake. I was going to store the default values in a text file in non executable format and extract the values using some code. There you replied with:

"1. If you immediately create a CMD file, instead of a TXT file
, you don't need to change the extension. It's a runnable CMD file anyway (well almost, see point 4), so why create it as a TXT file first ?"

I saw your well taken point and changed my code to what you just explained above. I'm storing it in a Settings.bat file though instead of a Settings.cmd file. If there's a preference for storing in a .cmd file, please let me know.

----------
tvc said:
OK, then. I was just thinking like this:

C:\>set Number_of_copies=1
C:\>set var1=%Number_of_copies%
C:\>set /p var1=Give value for Number of copies (%Number_of_copies%)
----------

In this code, the set /p statement gives the user only the default value. I need to show the user all of the possible values. I guess I could echo all of the acceptable values first without any parenthesis around any values and then follow it with the set /p statement that shows only the default value. I'll look into this as a possible solution. It would definitely be simpler code than my brute force method in my original post.

Thanks!
Sky


Report •

#13
December 16, 2009 at 11:10:45
So did you unscrew your lock-up problem?


=====================================
Helping others achieve escape felicity

M2


Report •

#14
December 16, 2009 at 12:28:11
> C:\>set /p var1=Give value for Number of copies (%Number_of_copies%)
> ----------
>
> In this code, the set /p statement gives the user only the default
> value. I need to show the user all of the possible values. I guess I
> could echo all of the acceptable values first without any arenthesis
> around any values and then follow it with the set /p statement that
> shows only the default value. I'll look into this as a possible
> solution. It would definitely be simpler code than my brute force
> method in my original post.

No no, it is even simplier. If you want to display all the values, just change the SET command:

set /p var1=Give value for Number of copies (1 - 99) (default: %Number_of_copies%)

The SET command (with parameter /P) works as a sort of enhanced ECHO command already, you can put any text there!
I just left it out because ... well, cause I'm lazy


Report •

#15
December 16, 2009 at 12:32:20
> If there's a preference for storing in a .cmd file,
> please let me know.

I see you refer to BAT versus CMD ... well, that may be personal preference : I think CMD is used for later Windows versions as standard, and BAT for older Windows. I may be wrong, but I always use CMD ...


Didn't really know all details, but a lot is stated here : http://stackoverflow.com/questions/...

Well, I would say don't make BAT on XP or higher, unless you need to have them run on W9X


Report •

#16
December 16, 2009 at 16:48:12
----------
Mechanix2Go said:
So did you unscrew your lock-up problem?
----------

Chalked it up to "IF NOT EXIST".... ;o)
Sky


Report •

#17
December 16, 2009 at 16:59:44
----------
tvc said:
No no, it is even simplier. If you want to display all the values, just change the SET command:

set /p var1=Give value for Number of copies (1 - 99) (default: %Number_of_copies%)
----------

I screwed up. In my mind I was thinking about diplaying all of the values for "WriteSpeed (Max) 32x, 16x, 8x, 4x, 2x, 1x" while you were referring to "NbrCopies 1-99".

I think I would need to echo the allowed write speeds before the SET /P statement since only certain values are allowed.

Sorry for "mind elsewhere" post,
Sky


==========
EDIT: OK, now I see what you mean. Usage for WriteSpeed would be:

set /p WriteSpeed=Enter write speed: MAX, 32X, 16X, 8X, 4X, 2X, 1X (Default is %WriteSpeed%):

Kinda slow but slowly getting it. Nice solution! I like it.
Sky
==========


Report •

#18
December 16, 2009 at 17:04:09
----------
tvc said:
I see you refer to BAT versus CMD ... well, that may be personal preference : I think CMD is used for later Windows versions as standard, and BAT for older Windows. I may be wrong, but I always use CMD ...

Didn't really know all details, but a lot is stated here : http://stackoverflow.com/questions/...

Well, I would say don't make BAT on XP or higher, unless you need to have them run on W9X
----------

tvc,
Thanks for your insight and the link. Good info!
Sky


Report •

#19
December 16, 2009 at 17:24:02
"Chalked it up to "IF NOT EXIST".... ;o)"

So can you run attrib?


=====================================
Helping others achieve escape felicity

M2


Report •

#20
December 16, 2009 at 18:07:13
----------------------------------------
Skyhawk said:
"Chalked it up to "IF NOT EXIST".... ;o)"

Mechanix2Go said
So can you run attrib?
----------------------------------------

I haven't checked my system drive for errors yet and run SFC. I'll try to do that tonight and then run

attrib /s c:\choice.*

from the command prompt. Will let you know what happens.

Thanks,
Sky


Report •

#21
December 17, 2009 at 00:10:46
M2,

I check my system drive for errors, none found. I ran SFC /scannow and no errors found there either.

It dawned on me that I could try running the

attrib /s c:\choice.*

cmd on my laptop running Vista just to see what it outputs. The cursor went to the next line and hung for a while (the same as on my WinXP Pro PC), then it displayed some data.

Seeing that short hang symptom, I tried the attrib cmd on my WinXP Pro PC again. The cursor went to the next line and hung like before, only this time I looked at the HDD led and saw that the system was searching the HDD. I waited a while and finally the following message was displayed:

File not found - C:\choice.*

So my WinXP Pro system was not hung after all. It was just taking a long time to search for a file which isn't on the HDD.

Sky


Report •

#22
December 17, 2009 at 02:38:46
Hi,

Good to know it's not hanging.

So we have two main options:

[1] Create CHOICE.EXE on the box.

[2] Get to grips with set /p


=====================================
Helping others achieve escape felicity

M2


Report •

#23
December 17, 2009 at 08:56:19
Not really on the subject, but CHOICE.com has got an annoying side feature on my computer:

C:\Documents and Settings>choice.com
[Y,N]?N

C:\DOCUME~1>


Report •

#24
December 17, 2009 at 10:04:21
tvc,

That's the main reason to use choice.exe.


=====================================
Helping others achieve escape felicity

M2


Report •

#25
December 22, 2009 at 13:02:56
I'm talking about DOCUME~1


Report •

#26
December 23, 2009 at 03:25:41
tvc,

So am I.

Here's a bat to check for choice.exe in the PATH and create it if needed.

http://golden-triangle.com/MAKCHOI.ZIP

Name the file, let's say, mymy.bat


=====================================
Helping others achieve escape felicity

M2


Report •

#27
December 23, 2009 at 04:20:50
That one works better indeed:

C:\Documents and Settings>d:\temp\choice.exe
[Y,N]?Y

C:\Documents and Settings>


Thanks


Report •


Ask Question