For Loop and ErrorLevel

P

Praetorian Guard

Hi,

I have the batch file below. It will move all doc files from D: to C: but
I'd like to have an error level if there is an error encountered it will not
continue to move the rest of the files.

FOR %%a IN (D:\*.doc) DO (move %%a c:\)

Something like this.

FOR %%a IN (D:\*.doc) DO
(
move %%a c:\
if %errorlevel% NEQ 0 goto end
)
:end

How do I do thins?

Thank you in advance.
 
P

Pegasus \(MVP\)

Praetorian Guard said:
Hi,

I have the batch file below. It will move all doc files from D: to C: but
I'd like to have an error level if there is an error encountered it will
not continue to move the rest of the files.

FOR %%a IN (D:\*.doc) DO (move %%a c:\)

Something like this.

FOR %%a IN (D:\*.doc) DO
(
move %%a c:\
if %errorlevel% NEQ 0 goto end
)
:end

How do I do thins?

Thank you in advance.

Your syntax, with a slight modification, should work:
FOR %%a IN (D:\*.doc) DO (
move %%a c:\
if %errorlevel% NEQ 0 goto end
)

You could also do it like so:
FOR %%a IN (D:\*.doc) DO move %%a c:\ || goto :eof

Since your files names could have embedded spaces, it would be better to
code it like so:
FOR %%a IN (D:\*.doc) DO move "%%a" c:\ || goto :eof

This routine will terminate if the target file already exists unless you
modify it like so:
FOR %%a IN (D:\*.doc) DO move /y "%%a" c:\ || goto :eof
 
P

Praetorian Guard

Thank you I will try it and let you know.

Pegasus (MVP) said:
Your syntax, with a slight modification, should work:
FOR %%a IN (D:\*.doc) DO (
move %%a c:\
if %errorlevel% NEQ 0 goto end
)

You could also do it like so:
FOR %%a IN (D:\*.doc) DO move %%a c:\ || goto :eof

Since your files names could have embedded spaces, it would be better to
code it like so:
FOR %%a IN (D:\*.doc) DO move "%%a" c:\ || goto :eof

This routine will terminate if the target file already exists unless you
modify it like so:
FOR %%a IN (D:\*.doc) DO move /y "%%a" c:\ || goto :eof
 
P

Praetorian Guard

I think I found it.

FOR %%a IN (D:\*.doc) DO move /y "%%a" c:\ || goto :eof || echo
%errorlevel%
 
P

Praetorian Guard

Sorry but I think this one always returns 1 even if the move is successful.

Any idea?
 
P

Pegasus \(MVP\)

Use your initial syntax (with the parenthesis in the right place!) if you
need to know the value of %ErrorLevel%.
 
P

Praetorian Guard

Can you show me how to do that please?

Pegasus (MVP) said:
Use your initial syntax (with the parenthesis in the right place!) if you
need to know the value of %ErrorLevel%.
 
P

Praetorian Guard

To those who want to know.

FOR %%a IN (D:\*.doc) DO (move %%a c:\) || echo %errorlevel% && goto :eof
 
P

Pegasus \(MVP\)

This should really be

FOR %%a IN (D:\*.doc) DO (move %%a c:\) || (echo %errorlevel% & goto :eof)

The double ampersand "&&" tests for an ErrorLevel of 0. In your particular
case the "echo" command generates an ErrorLevel of 0 but if someone
introduces a variation then the ErrorLevel may be non-zero. You must
therefore use the concatenation character, a single "&".

I note that you have dropped the /y switch and the double quotes arroun %%a,
presumably because you're prepared to accept the associated risks.
 
T

Timo Salmi

Pegasus (MVP) said:
This should really be

FOR %%a IN (D:\*.doc) DO (move %%a c:\) || (echo %errorlevel% & goto :eof)

The double ampersand "&&" tests for an ErrorLevel of 0. In your particular
case the "echo" command generates an ErrorLevel of 0 but if someone
introduces a variation then the ErrorLevel may be non-zero. You must
therefore use the concatenation character, a single "&".

Right. Apropos http://www.netikka.net/tsneti/info/tscmd009.htm

Consider the following example code which demonstrates several special
tricks of command line script programming
@echo off & setlocal enableextensions
copy "C:\_D\TEST\My file.txt" "C:\_M\TEMP\" >nul 2>&1^
&&(echo The copying succeeded&goto _continue)^
||(echo The copying failed&echo Exiting&goto :EOF)
:_continue
echo Continuing whatever ...
endlocal & goto :EOF

1. Suppressing standard output
2. Suppressing a potential error message
3. Continuing a line
4. Run a command if the preceding command is successful.
5. Enclosing a set of commands in parentheses
6. Run a command if the preceding command fails.
7. Multiple commands on one line

The output could be e.g.
C:\_D\TEST>cmdfaq
The copying succeeded
Continuing whatever ...
or
C:\_D\TEST>cmdfaq
The copying failed
Exiting

All the best, Timo
 
R

Rich Pasco

Pegasus said:
FOR %%a IN (D:\*.doc) DO move "%%a" c:\ || goto :eof

It seems that the symbol "||" means to do what follows if the
errorlevel from what precedes is non-zero. Where is this
documented? I looked in "for /?" but didn't see it there.

- Rich
 
B

billious

Rich Pasco said:
It seems that the symbol "||" means to do what follows if the
errorlevel from what precedes is non-zero. Where is this
documented? I looked in "for /?" but didn't see it there.

- Rich

Start>Help & Support>Command-line reference >Command shell overview

(alt.msdos.batch.nt is for serious batchers)
 
R

Rich Pasco

Rich said:
Thanks, but I can't find "Command-line reference" on my Help & Support
screen, a copy of which is here:
http://www.richpasco.org/tech/help-and-support.png
What am I missing?

Entering the term "Command shell overview" into the Search box found
a page by that title, explaining the || syntax. But "Command-line
reference" doesn't seem to be linked from my Help & Support page,
which is obviously why I didn't know it existed. :)

- Rich
 
B

billious

Rich Pasco said:
Entering the term "Command shell overview" into the Search box found
a page by that title, explaining the || syntax. But "Command-line
reference" doesn't seem to be linked from my Help & Support page,
which is obviously why I didn't know it existed. :)

- Rich


From Help & Support, try searching for "reference", grab the full-text
search matches and select "Managing System Information from the command
line." Poke the "Command-line reference A-Z" link and that should get you to
the appropriate area - the "||" documentation is at the "command-line
reference" link from there.

I saved the link to "..A-Z" years ago...
 
T

Timo Salmi

Rich said:
But "Command-line
reference" doesn't seem to be linked from my Help & Support page,
which is obviously why I didn't know it existed. :)

Just enter C:\WINDOWS\Help\ntcmds.chm from the command line.

All the best, Timo
 
R

Rich Pasco

Timo said:
Just enter C:\WINDOWS\Help\ntcmds.chm from the command line.

Thanks. I just now put on my desktop a shortcut to that
heretofore well-hidden document!

- Rich
 

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