cmd files

G

Gene E. Bloch

On Wed, 1 Dec 2010 09:13:06 -0500, Zaphod Beeblebrox wrote:
You 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.
As I said, it was so weird that I set up an experiment to demonstrate it
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.
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!
Code:
@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
 
G

Gene E. Bloch

1. the REM has to be repeated for each line, otherwise, as here,
"backup_code" gets taken as a command.
I didn't think you would try to run the file as written - it was meant
as an example.
 
G

Gene E. Bloch

Just curious, why did you add the regcleanr.exe program to this batch
file? It doesn't seem to be a standard Windows file (it's not on my
Win7 system) and I'm not sure what it does nor how it's related to the
overall backup task. Thanks.
Just so he could contradict what he told me I should do?

I wonder about rem before a blank line too, as well as the pause :)
 
D

Dond13

Ed Cryer said:
This is my user-friendly version.
I'd like your opinion of this, Dond13.

---------------------------------------

@echo off

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/
pause
goto :EOF
)

echo/
ECHO Backing up to %DRIVE%
echo/
CHOICE /C YN /N /m "OK to proceed? Y or N?"
IF ERRORLEVEL 2 (
pause
GOTO :EOF)

REM Place batch progs here
REM
CD C:\Program Files (x86)\RegCleaner
Regcleanr.exe
pause
I will try this and get back to you Ed. I am going to be occupied for a
couple of days but I will get back and let everyone know how it goes. I
would like to thank everyone for their comments. I believe that I
understand the suggestions but I will probably need a clarification or two
before I get it running.

Don D
 
E

Ed Cryer

Just so he could contradict what he told me I should do?

I wonder about rem before a blank line too, as well as the pause :)
RegCleaner was used simply to test the macro; load it, don't load it.

A blank REM is good for layout spacing.

RegCleaner is one of my all-time favourites; I've carried it right
through from about Win95. Freeware. I particularly like the Startup
listing. I look at that regularly. And I often find something's sneaked
in there (or rather "been sneaked in there"). Removing stuff takes just
a tick & click.
http://majorgeeks.com/RegCleaner_d460.html



Ed
 
Z

Zaphod Beeblebrox

Gene E. Bloch said:
On Wed, 1 Dec 2010 09:13:06 -0500, Zaphod Beeblebrox wrote:


As I said, it was so weird that I set up an experiment to
demonstrate it
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.
I'm flabbergasted - I've never noted that behavior before! I suspect
it is because I normally use a different style that avoids the
problem. A bit of research reveals that when called from a .CMD,
'set' resets errorlevel, but when called from a .BAT, it does not.
Not sure what the reasoning is behind the behavior, but it is
consistent back to XP at least, and probably Win2k and NT as well but
I don't have test platforms for those versions easily available.

Thanks for pointing it out and providing the test code!

--
Zaphod

Arthur: All my life I've had this strange feeling that there's
something big and sinister going on in the world.
Slartibartfast: No, that's perfectly normal paranoia. Everyone in the
universe gets that.
 
D

Dond13

Ed Cryer said:
This worked here, saved as a BAT file and then clicked on.

Two points;
1. the REM has to be repeated for each line, otherwise, as here,
"backup_code" gets taken as a command.
2. It could do with putting control back to the user before going ahead
and doing its stuff.
Perhaps;
CHOICE /C YN /N /m "Proceed as displayed (Y or N)?"
IF ERRORLEVEL etc

Ed
OK, I've been getting so many helpful comments that I am probably getting
confused. I am taking the suggestion of the "CHOICE" command and using it.
It seems to work up to a point, and that is when I substitute "%drive%" for
the drive address, it inserts nothing. I receive an error that I can't make
a 'cyclic' copy. I am showing a shortened version of my original cmd file
with the suggested fixes. Can you look it over and see where I am going
wrong please? Thanks

Don D.

CHOICE /C LGDID /N /m "Backup to drive l:, G:, I: D:, (or N for None)?"
IF ERRORLEVEL 1 SET DRIVE = L:
IF ERRORLEVEL 2 SET DRIVE = G:
IF ERRORLEVEL 3 SET DRIVE = I:
IF ERRORLEVEL 4 SET DRIVE = D:
IF ERRORLEVEL 5 (
ECHO nO BACKUP DRIVE CHOSEN
ECHO
PAUSE
rem GOTO EXIT
)

ECHO Backing up to %DRIVE%
echo/
CHOICE /C YN /N /m "OK to proceed? Y or N?"
IF ERRORLEVEL 2 (
pause
GOTO exit)



ECHO

xcopy c:\excel\*.* %DRIVE%\excel\ /v /y /s /d
if errorlevel 1 goto error
xcopy c:\quattro\*.* %DRIVE%\quattro\ /v /y /s /d
if errorlevel 1 goto error
goto exit
:error
echo There was an error writing to the disk.
echo The disk may be full or it may not be
echo formatted. Check and try again.
echo.
pause
:exit
pause
 
G

Gene E. Bloch

I'm flabbergasted - I've never noted that behavior before! I suspect
it is because I normally use a different style that avoids the
problem. A bit of research reveals that when called from a .CMD,
'set' resets errorlevel, but when called from a .BAT, it does not.
Not sure what the reasoning is behind the behavior, but it is
consistent back to XP at least, and probably Win2k and NT as well but
I don't have test platforms for those versions easily available.

Thanks for pointing it out and providing the test code!
You're welcome. We all managed to learn something, for once :)

The only bad thing is that I almost never use scripts (bat, cmd) these
days, so now I have knowledge that all I can do with is brag about it
:)

Maybe I'll do that at lunch later today...
 
G

Gene E. Bloch

RegCleaner was used simply to test the macro; load it, don't load it.
OK, I did much the same - when I ran it here I used Windows Explorer the
same way, but when I posted it I just replaced it by an appropriate
comment (although as you noticed, I left out the REM - or echo, which
would also show it on the screen when it runs).
A blank REM is good for layout spacing.
But a blank line serves the same purpose, whereas the "rem" does nothing
beyond that except visually mask the blankness of the line.
 
G

Gene E. Bloch

OK, I've been getting so many helpful comments that I am probably getting
confused. I am taking the suggestion of the "CHOICE" command and using it.
It seems to work up to a point, and that is when I substitute "%drive%" for
the drive address, it inserts nothing. I receive an error that I can't make
a 'cyclic' copy. I am showing a shortened version of my original cmd file
with the suggested fixes. Can you look it over and see where I am going
wrong please? Thanks

Don D.

CHOICE /C LGDID /N /m "Backup to drive l:, G:, I: D:, (or N for None)?"
IF ERRORLEVEL 1 SET DRIVE = L:
IF ERRORLEVEL 2 SET DRIVE = G:
IF ERRORLEVEL 3 SET DRIVE = I:
IF ERRORLEVEL 4 SET DRIVE = D:
IF ERRORLEVEL 5 (
ECHO nO BACKUP DRIVE CHOSEN
ECHO
PAUSE
rem GOTO EXIT
)

ECHO Backing up to %DRIVE%
echo/
CHOICE /C YN /N /m "OK to proceed? Y or N?"
IF ERRORLEVEL 2 (
pause
GOTO exit)



ECHO

xcopy c:\excel\*.* %DRIVE%\excel\ /v /y /s /d
if errorlevel 1 goto error
xcopy c:\quattro\*.* %DRIVE%\quattro\ /v /y /s /d
if errorlevel 1 goto error
goto exit
:error
echo There was an error writing to the disk.
echo The disk may be full or it may not be
echo formatted. Check and try again.
echo.
pause
:exit
pause
You would get that behavior if drive didn't get set, since in that case
the xcopy command would be interpreted as:
xcopy c:\excel\*.* \excel\ /v /y /s /d

Put a command
echo Selected drive is %drive%

ahead of the copy commands and see what you get.

Also in your choice command you wrote LGDID instead of LGDIN. I have no
idea what that would do. I'll leave the testing up to you - but do the
echo before you make that change, just to see what is happening - that
makes for a better experiment :)
 
Z

Zaphod Beeblebrox

The only bad thing is that I almost never use scripts (bat, cmd)
these
days, so now I have knowledge that all I can do with is brag about
it
:)

Maybe I'll do that at lunch later today...
They are still a regular part of my life both at home and work so I
tend to keep up with the basics - I won't pretend to be anything more
than a casual batch scripter, intermediate level at best. I always
thought I was pretty good at it, but I've since learned where I really
am on the scale - if you've seen what the regulars over in
alt.msdos.batch and alt.msdos.batch.nt are capable of you'd understand
where I'm coming from...
 
G

Gene E. Bloch

They are still a regular part of my life both at home and work so I
tend to keep up with the basics - I won't pretend to be anything more
than a casual batch scripter, intermediate level at best. I always
thought I was pretty good at it, but I've since learned where I really
am on the scale - if you've seen what the regulars over in
alt.msdos.batch and alt.msdos.batch.nt are capable of you'd understand
where I'm coming from...
Maybe I'll look there, but right now I'm not ready to embarrass myself
:)

And I forgot to say thanks for this:
A bit of research reveals that when called from a .CMD,
'set' resets errorlevel, but when called from a .BAT, it does not.
Not sure what the reasoning is behind the behavior, but it is
consistent back to XP at least
....

from an earlier post by you. It's very credible, but testing it could be
hard. It is possible that echo %errorlevel% would return a new
errorlevel. Yeahbut...One could first save the value by:

set tempval %errorlevel%

with no loss of generality (I haven't said *that* for a long time!).
 
Z

Zaphod Beeblebrox

Stan Brown said:
A double colon is even better.
Except that using a broken label (double colon) as comment starter in
a loop or other parenthetical structure will break it. For example:

IF EXIST C:\autoexec.bat (
::
ECHO Do something
::
)

results in the error message:

) was unexpected at this time.

But if you replace them with REM statements, it works fine. You run
into similar problems in FOR loops.

--
Zaphod

Pan-Galactic Gargle Blaster: A cocktail based on Janx Spirit.
The effect of one is like having your brain smashed out
by a slice of lemon wrapped round a large gold brick.
 
E

Esra Sdrawkcab

If you call the .cmd file from a shortcut, or from a cmd.exe window,,
you can pass the F: G: or H: argument on the command line, then within
the .cmd file replace your hard coded drive letter with %1
keep your backup command on the pendrive - then you *have* to find the
drive letter!

e.g. savefile.cmd is

copy c:\users\mystuff\*.* \mystuffbu\

etc
 
S

Stan Brown

Except that using a broken label (double colon) as comment starter in
a loop or other parenthetical structure will break it. For example:

IF EXIST C:\autoexec.bat (
::
ECHO Do something
::
)

results in the error message:

) was unexpected at this time.

But if you replace them with REM statements, it works fine. You run
into similar problems in FOR loops.
Thanks -- I haven't actually written a batch file in native command
prompt since before the ( ... ) construct was added; I've been using
4NT and then TCCLE. So I appreciate the correction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top