multiple commands in single line


A

Aaron

is this possible in cmd without writing a batch file?

ie

c:/>cd windows;cd system32
c:/windows/system32>

Aaron
 
Ad

Advertisements

T

Torgeir Bakken \(MVP\)

Aaron said:
is this possible in cmd without writing a batch file?

ie

c:/>cd windows;cd system32
c:/windows/system32>
Hi,

C:\>cd %windir% && cd system32
C:\WINNT\SYSTEM32>


&& means "Do the command after && if the command before && succeeded".
 
T

Todd Vargo

Torgeir Bakken (MVP) said:
&& means "Do the command after && if the command before && succeeded".
I've seen this a number of times but can not figure what exactly determines
if the command before succeeded. I'm guessing an errorlevel of zero.
However, some programs return non-zero values for success too.

So what is the criteria for && to mean a program succeeded?
 
W

William Allen

I've seen this a number of times but can not figure what exactly determines
if the command before succeeded. I'm guessing an errorlevel of zero.
However, some programs return non-zero values for success too.

So what is the criteria for && to mean a program succeeded?
did not complete successfully=receives an error code greater than zero

Quoted from:

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_o.mspx
under sub-item Concepts, sub-sub-item Command Shell Overview

===Quote starts
&& [...]
command1 && command2
Use to run the command following && only if the
command preceding the symbol is successful. Cmd.exe
runs the first command, and then runs the second command
only if the first command completed successfully.

|| [...]
command1 || command2
Use to run the command following || only if the command
preceding || fails. Cmd.exe runs the first command, and
then runs the second command only if the first command did
not complete successfully (receives an error code greater
than zero).

===End quote

============Screen capture Windows 2000 simulated in Win95
C:\WORK>echo n|choice || echo counts as failed
[Y,N]?N
counts as failed

C:\WORK>echo n|choice && echo counts as failed
[Y,N]?N

C:\WORK>
============End screen capture
 
T

Todd Vargo

William Allen said:
I've seen this a number of times but can not figure what exactly determines
if the command before succeeded. I'm guessing an errorlevel of zero.
However, some programs return non-zero values for success too.

So what is the criteria for && to mean a program succeeded?
did not complete successfully=receives an error code greater than zero

Quoted from:

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-
us/ntcmds_o.mspx
under sub-item Concepts, sub-sub-item Command Shell Overview

===Quote starts
&& [...]
command1 && command2
Use to run the command following && only if the
command preceding the symbol is successful. Cmd.exe
runs the first command, and then runs the second command
only if the first command completed successfully.

|| [...]
command1 || command2
Use to run the command following || only if the command
preceding || fails. Cmd.exe runs the first command, and
then runs the second command only if the first command did
not complete successfully (receives an error code greater
than zero).

===End quote

============Screen capture Windows 2000 simulated in Win95
C:\WORK>echo n|choice || echo counts as failed
[Y,N]?N
counts as failed

C:\WORK>echo n|choice && echo counts as failed
[Y,N]?N

C:\WORK>
============End screen capture #1
==== Windows XP screen capture
E:\>echo test && echo success
test
success

E:\>echo /? && echo success
Displays messages, or turns command-echoing on or off.

ECHO [ON | OFF]
ECHO [message]

Type ECHO without parameters to display the current echo setting.

E:\>echo /? || echo success
Displays messages, or turns command-echoing on or off.

ECHO [ON | OFF]
ECHO [message]

Type ECHO without parameters to display the current echo setting.
success

E:\>rem /? && echo success
Records comments (remarks) in a batch file or CONFIG.SYS.

REM [comment]
success

E:\>
==== end capture

Why is displaying ECHO's help considered a failure but REM's is not? Also,
since I can set errorlevel to any value before the test and it will be
unchanged afterwards, it seems the errorlevel state is bypassed/ignored.

ISTM, Microsoft's explanation for && and || operation does not hold water.
The result above is inconsistent with the documentation.
 
W

William Allen

in message
....snip
Why is displaying ECHO's help considered a failure but REM's is not? Also,
since I can set errorlevel to any value before the test and it will be
unchanged afterwards, it seems the errorlevel state is bypassed/ignored.

ISTM, Microsoft's explanation for && and || operation does not hold water.
The result above is inconsistent with the documentation.
There's documentation and there's documentation :)

As I've remarked before, part of the task of learning Batch is to
refine and further define the documentation. So, as to && and ||,
the situation appears to be:

SomeCommand && Echo success message
SomeCommand || Echo failure message

(a) If SomeCommand sets the ERRORLEVEL then:
If ERRORLEVEL set is 0: && statement executed
|| statement not executed
If ERRORLEVEL set to non-zero: || statement executed
&& statement not executed

(b) If SomeCommand doesn't change the ERRORLEVEL then:
Regardless of current ERRORLEVEL, neither success nor failure
is defined and both && and || act as a normal command separator,
in other words both && and || act as &

This refinement is more-or-less what I'd take the Microsoft
documentation to mean, although (b) is not spelled out at all
(but I guess they'd argue it's "sort of" implied :)

OK, so what about REM?

I'd say this is a particular case for which you've discovered a new
(bug)/(feature of command parsing) that applies to REM (REM has more
code that has to handle ignoring |, >, >>, <, ||, and && in _true_
comments, so it's feasible that there could be a REM-specific bug in
the && and || handler). So noted.

Are there any cases other than REM that appear to contradict my
(a) and (b) version of Microsoft's documentation?
 
Ad

Advertisements

W

William Allen

(replaces earlier cancelled message)
in message
....snip
Why is displaying ECHO's help considered a failure but REM's is not? Also,
since I can set errorlevel to any value before the test and it will be
unchanged afterwards, it seems the errorlevel state is bypassed/ignored.

ISTM, Microsoft's explanation for && and || operation does not hold water.
The result above is inconsistent with the documentation.
Useful points. So noted.

Treating:

ECHO /? || ECHO this will be displayed
ECHO /? && ECHO this will not be displayed

as the "rule" for /? help commands, are you aware of any other /?
exceptions apart from the REM command?

And are you aware of any other apparent inconsistencies in && and ||
handling apart from the /? treatment?

Motto for documentation fans:
"The only accurate program documentation is what the program does."
 
Ad

Advertisements

T

Todd Vargo

William Allen said:
(replaces earlier cancelled message)
in message
...snip

Useful points. So noted.

Treating:

ECHO /? || ECHO this will be displayed
ECHO /? && ECHO this will not be displayed

as the "rule" for /? help commands, are you aware of any other /?
exceptions apart from the REM command?

And are you aware of any other apparent inconsistencies in && and ||
handling apart from the /? treatment?

Motto for documentation fans:
"The only accurate program documentation is what the program does."
I've only tested about a half dozen commands, so I'm now aware of others
yet. If I have time, I might tinker with it again but if anyone is
interested in tinkering, I still don't see why displaying /? help is
considered a failure and how it is actually determined.

ECHO foo && ECHO this will be displayed
ECHO /? && ECHO this will not be displayed

Maybe one of those MVP's out there knows something about this. ;-)
 

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