• Please review our updated Terms and Rules here

Daisy chaining dos commands

barythrin

Veteran Member
Joined
Oct 5, 2005
Messages
6,256
Location
Texas
Someone asked me at work if like *nix (;) there was a way to daisy chain commands in dos to run after each other. This almost seemed like I'd done it before but I drew a blank at the time.

Since then I found atleast in XP that you can use & in the cmd processor to run multiple commands back to back.

Anyone happen to know how long this has been implemented? Just curious if this was a true dos function or something added from Windows.
 
I believe you can use the | symbol between commands. But I have never really tried to do it, I simply write batch files to run multiple commands after each other.

Although come to think of it this is a very common command string
Code:
type readme.txt | more
 
Yeah, I usually just did batch files also. I know you can pipe output to another command, but in this instance it was really just us having an annoying problem where we needed to renew and release our IP. The other not that interesting but notable thing is the cli still goes command by command in the batch file so if you're on a network drive, do a command to disable your IP the next command will fail to read the batch file and error with an invalid location.

So yeah, batch files work. I suggested that also knowing you could do something kinda dumb and try to pipe the output of ipconfig /release to ipconfig /renew but it took that as the argument and wasn't happy.

But & worked fine although I'm not sure I tried doing it from a network location cli (not really the point though).
Code:
ipconfig /release & ipconfig /renew
 
I believe you can use the | symbol between commands. But I have never really tried to do it, I simply write batch files to run multiple commands after each other.

Although come to think of it this is a very common command string
Code:
type readme.txt | more

The Pipe character (|) sends the output from one command to the next as input.

So something like:
DIR | SORT
or
DIR | MORE

would work.

The < and > (as well as their << and >> counterparts which append instead of overwrite) redirect output and are usually used in conjunction with files. So

DIR > Directory.txt

redirects output to the file.

There is/was a way to stack commands but I can't for the life of me remember it. . . :)
 
The problem with | in DOS used to chain commands is that it will gobble stdout and redirect stdin, which you may not want to do.

However, there is a way around this, at least in DOS 7 and perhaps earlier versions, although it's a bit arcane--and that's to use the FOR batch command. So, for example:
Code:
FOR %x in ("echo Have a beer" "echo Why, thank you!") do %x

will execute both of the echo commands in sequence, without gobbling any output. Also note that if you put this sort of thing in a batch file, you have to double up on the % characters.

Another way is to create a small batch file that takes commands as arguments:

Code:
:AGAIN 
ECHO Running %1
%1
SHIFT
IF NOT "%1" == "" GOTO AGAIN
 
Someone asked me at work if like *nix (;) there was a way to daisy chain commands in dos to run after each other.

Perhaps you're looking for "CALL"?

ONE.BAT:
Code:
@ECHO ONE
CALL TWO.BAT

TWO.BAT:
Code:
@ECHO TWO
 
Back
Top