G
Gene E. Bloch
On Wed, 1 Dec 2010 09:13:06 -0500, Zaphod Beeblebrox wrote:
to myself.
I created a new batch file with two sequences, all as copied from your
script except for the order of tests. One sequence is in increasing
order, as you posted originally, and the other is in decreasing order.
I put a switch based on a command line parameter to choose one or the
other of those sequences. Then I saved the file again as .cmd instead of
..bat.
With the bat file, it failed to make correct choices if I chose the
reverse sequence and worked with the forward sequence. With the cmd
file, it failed to make correct choices if I chose the forward sequence
and worked with the reverse sequence. I'll paste the batch file below if
you wish to play with it. Also, I just tested the two files again, just
in case I got trapped into a PEBCAK yesterday
I emphasize: the two files are identical - except that at the start one
echoes test.bat and the other echoes test.cmd so I don't get mixed up
.
Oh - just to be clear: Windows 7 Home Premium 64-bit.
@echo off
echo Test.bat
echo/
if "%1"=="rev" goto reverse
echo Forward list
echo/
CHOICE /C FGHIN /N /M "Backup to drive F:, G:, H:, I: (or N for None)?"
IF ERRORLEVEL 1 SET DRIVE=F:
IF ERRORLEVEL 2 SET DRIVE=G:
IF ERRORLEVEL 3 SET DRIVE=H:
IF ERRORLEVEL 4 SET DRIVE=I:
if errorlevel 5 (
echo No backup drive chosen
echo/
goto :eof
)
goto done
:reverse
echo Reverse list
echo/
CHOICE /C FGHIN /N /M "Backup to drive F:, G:, H:, I: (or N for None)?"
if errorlevel 5 (
echo No backup drive chosen
echo/
goto :eof
)
IF ERRORLEVEL 4 SET DRIVE=I:
IF ERRORLEVEL 3 SET DRIVE=H:
IF ERRORLEVEL 2 SET DRIVE=G:
IF ERRORLEVEL 1 SET DRIVE=F:
goto done
:done
ECHO Backing up to %DRIVE%
echo/
echo Backup code goes here
As I said, it was so weird that I set up an experiment to demonstrate itYou shouldn't have to reverse the order, unless you change from SET
DRIVE=F: to something like GOTO DRIVE_F. The reason you would need to
for the GOTO version is that errorlevel tests are not really an equal
to, rather they are greater than or equal to, so a test for
ERRORLEVEL=0 is always true (unless there is some way to generate a
negative errorlevel, that is) and ERRORLEVEL=1 is true for any
errorlevel greater than 0, etc.
The above, of course, is my understanding of how it works and my
experience with batch files under DOS, XP and Vista, and is not based
on actual testing with a CMD file under Windows 7, so I'll be happy to
be proven wrong if in fact my understanding is inaccurate.
to myself.
I created a new batch file with two sequences, all as copied from your
script except for the order of tests. One sequence is in increasing
order, as you posted originally, and the other is in decreasing order.
I put a switch based on a command line parameter to choose one or the
other of those sequences. Then I saved the file again as .cmd instead of
..bat.
With the bat file, it failed to make correct choices if I chose the
reverse sequence and worked with the forward sequence. With the cmd
file, it failed to make correct choices if I chose the forward sequence
and worked with the reverse sequence. I'll paste the batch file below if
you wish to play with it. Also, I just tested the two files again, just
in case I got trapped into a PEBCAK yesterday
I emphasize: the two files are identical - except that at the start one
echoes test.bat and the other echoes test.cmd so I don't get mixed up
.
Oh - just to be clear: Windows 7 Home Premium 64-bit.
Code:BTW, a great resource for CMD and batch scripting is
alt.msdos.batch.nt (.nt is taken to mean any Windows version from NT
and following, so would include Vista and Win7, btw). If the guys
posting in there can't do it, it can't be done!
@echo off
echo Test.bat
echo/
if "%1"=="rev" goto reverse
echo Forward list
echo/
CHOICE /C FGHIN /N /M "Backup to drive F:, G:, H:, I: (or N for None)?"
IF ERRORLEVEL 1 SET DRIVE=F:
IF ERRORLEVEL 2 SET DRIVE=G:
IF ERRORLEVEL 3 SET DRIVE=H:
IF ERRORLEVEL 4 SET DRIVE=I:
if errorlevel 5 (
echo No backup drive chosen
echo/
goto :eof
)
goto done
:reverse
echo Reverse list
echo/
CHOICE /C FGHIN /N /M "Backup to drive F:, G:, H:, I: (or N for None)?"
if errorlevel 5 (
echo No backup drive chosen
echo/
goto :eof
)
IF ERRORLEVEL 4 SET DRIVE=I:
IF ERRORLEVEL 3 SET DRIVE=H:
IF ERRORLEVEL 2 SET DRIVE=G:
IF ERRORLEVEL 1 SET DRIVE=F:
goto done
:done
ECHO Backing up to %DRIVE%
echo/
echo Backup code goes here