I want to run two commands in a Windows CMD console.
In Linux I would do it like this: touch thisfile ; ls -lstrh.
How is it done on Windows?
Join Stack Overflow to learn, share knowledge, and build your career.
|
I want to run two commands in a Windows CMD console. In Linux I would do it like this: How is it done on Windows? |
||||
|
|
|
Like this on all Microsoft OSes since 2000, and still good today:
If you want the second command to execute only if the first exited successfully:
The single ampersand (&) syntax to execute multiple commands on one line goes back to Windows XP, Windows 2000, and some earlier NT versions. (4.0 at least, according to one commenter here.) There are quite a few other points about this that you'll find scrolling down this page. Historical data follows, for those who may find it educational. Prior to that, the && syntax was only a feature of the shell replacement 4DOS before that feature was added to the Microsoft command interpreter. In Windows 95, 98 and ME, you'd use the pipe character instead:
In MS-DOS 5.0 and later, through some earlier Windows and NT versions of the command interpreter, the (undocumented) command separator was character 20 (Ctrl+T) which I'll represent with ^T here.
|
|||||||||||||||||||||
|
|
A quote from the documentation:
|
|||||||||||||||||||||
|
|
|
|||||||||
|
|
You can use & to run commands one after another. Example: |
|||
|
|
|
If you want to create a cmd shortcut (for example on your desktop) add /k parameter (/k means keep, /c will close window):
|
|||
|
|
|
You can use call to overcome the problem of environment variables being evaluated too soon - e.g.
|
|||||||||||||||||
|
|
This command execute command and open |
||||
|
|
|
So, I was trying to enable the specific task of running
This works like a charm -- RegAsm runs on the file and shows its results, then a "Press any key to continue..." prompt is shown, then the command prompt window closes when a key is pressed. P.S. For others who might be interested, you can use the following .reg file entries to add a dllfile association to .dll files and then a RegAsm command extension to that (notice the escaped quotes and backslashes):
Now I have a nice right-click menu to register an assembly. |
|||||
|
|
A number of processing symbols can be used when running several commands on the same line, and may lead to processing redirection in some cases, altering output in other case, or just fail. One important case is placing on the same line commands that manipulate variables.
As you see in the above example, when commands using variables are placed on the same line, you must use delayed expansion to update your variable values. If your variable is indexed, use CALL command with %% modifiers to update its value on the same line:
|
|||||
|
|
In order to execute two commands at the same time, you must put an & (ampersand) symbol between the two commands. Like so:
Cheers! |
|||
|
|
|
I try to have two pings in the same window, and it is a serial command on the same line. After finishing the first, run the second command. The solution was to combine with Start as usual, without The command used to launch in the same line is:
Any way, if you wish to parse the output, I don't recommend to use this. I noticed the output is scrambled between the output of the commands. |
||||
|
|
|
Well, you have two options: Piping, or just
Or,
Piping ( |
|||||||||
|
|
No, The piping has some issue, but it is not critical as long as people know how it works. |
||||
|
|
|
It's simple: just differentiate them with && signs. Example:
Goodbye World will be printed after Hello World. |
|||||||||||||||||
|
|
One more example: For example, when we use the
We can do that with one line:
|
||||
|
|
|
When you try to use or manipulate variables in one line beware of their content! E.g. a variable like the following
may lead to a lot of unhand-able trouble if you use it as
|
|||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?