Alternative to CHOICE command script - explanation?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gruff999
Posts: 5
Joined: 21 Feb 2014 15:08

Alternative to CHOICE command script - explanation?

#1 Post by gruff999 » 25 Feb 2014 13:14

Hi all,

As a long-time user of the CHOICE command I was impressed to find the XCOPY alternative on your forum:

Code: Select all

for /F "delims=" %%L in ('xcopy /L /w "%~f0" "%~f0" 2^>NUL') do (
if not defined key set "key=%%L"
)

(It works nicely in Vista 32-bit BTW)

When I tried to test it at work today (from memory) I forgot to include the "if not defined" part and just tried to use the SET "KEY=%%L".

It failed horribly.

Can you explain what role the "if not defined" is playing here?

I have several other questions and points but will scour the existing posts before asking!

Thanks

gruff999

jeb
Expert
Posts: 883
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Alternative to CHOICE command script - explanation?

#2 Post by jeb » 25 Feb 2014 16:36

The main idea is to use the capability of XCOPY to accept one key and ends then without pressing any other key.
And the key is also printed, as opposed to PAUSE.

But there is more output than needed.

XCOPY Output (german) wrote:C:\temp>xcopy /L /w some.bat some.bat
Eine beliebige Taste drücken, um das Kopieren der Datei(en) zu starten A
C:some.bat
Datei kann nicht auf sich selbst kopiert werden
0 Datei(en) kopiert

I pressed "A", so the relevant information is now in the first line and it's the last character.

So I only want to retrieve the first line in the FOR loop, that's the cause for the IF NOT DEFINED.
After reading the first line, the variable isn't empty anymore and the other lines are skipped.

jeb

gruff999
Posts: 5
Joined: 21 Feb 2014 15:08

Re: Alternative to CHOICE command script - explanation?

#3 Post by gruff999 » 26 Feb 2014 04:44

Thank you jeb, I don`t know why I didn`t figure that out myself during testing. I`ll try harder next time.

gruff999
Posts: 5
Joined: 21 Feb 2014 15:08

Re: Alternative to CHOICE command script - explanation?

#4 Post by gruff999 » 26 Feb 2014 10:43

One small point now I`ve come to use this technique instead of CHOICE.

Ideally I`d like a prompt under my menu, something like "Select an option: ", and have the cursor remain on the same line awaiting input, but using the XCOPY trick to capture the users selection.

I`ve managed to approximate the behaviour with some inelegant use of CALL and GOTO etc but there must be a neater way?

Revised code since original posting, this is tolerable (to me) and by extending all parts to be contained in functions it happily ignores invalid selections now.

Code: Select all

@ECHO OFF
cd /d %~dp0

:TOP
set KEY=

call :MENU_TOP
call :CAPTURE
call :PROCESS

cls
goto :TOP

goto :CAPTURE_END
:CAPTURE
for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do (
if not defined KEY set "KEY=%%a"
)
set KEY=%KEY:~-1%
if defined KEY cls && goto :MENU_TOP
:CAPTURE_END

goto :MENU_END
:MENU_TOP
TITLE Choose an option from the menu:
echo.
echo  1. DATABASE1
echo  2. DATABASE2
echo  3. DATABASE3
echo.
echo  q. Quit
echo.
echo  Choose: %KEY%
:MENU_END

goto :PROCESS_END
:PROCESS
if /i "%KEY%" EQU "1" call :WORK SERVER1 DATABASE1
if /i "%KEY%" EQU "2" call :WORK SERVER2 DATABASE2
if /i "%KEY%" EQU "3" call :WORK SERVER3 DATABASE3
if /i "%KEY%" EQU "q" exit
:PROCESS_END

goto :WORK_END
:WORK
TITLE Checking %2 on %1
ping %1
tnsping %2
pause
:WORK_END

Post Reply