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
&
is the Bash equivalent for ;
( run commands) and &&
is the Bash equivalent of &&
(run commands only when the previous has not caused an error).
No, cd / && tree && echo %time%
. The time echoed is at when the first command is executed.
The piping has some issue, but it is not critical as long as people know how it works.
So, I was trying to enable the specific task of running RegAsm
(register assembly) from a context menu. The issue I had was that the result would flash up and go away before I could read it. So I tried piping to Pause
, which does not work when the command fails (as mentioned here Pause command not working in .bat script and here Batch file command PAUSE does not work). So I tried cmd /k
but that leaves the window open for more commands (I just want to read the result). So I added a pause
followed by exit
to the chain, resulting in the following:
cmd /k C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe "%1" /codebase \"%1\" & pause & exit
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):
[HKEY_CLASSES_ROOT\.dll]
"Content Type"="application/x-msdownload"
@="dllfile"
[HKEY_CLASSES_ROOT\dllfile]
@="Application Extension"
[HKEY_CLASSES_ROOT\dllfile\Shell\RegAsm]
@="Register Assembly"
[HKEY_CLASSES_ROOT\dllfile\Shell\RegAsm\command]
@="cmd /k C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\regasm.exe \"%1\" /codebase \"%1\" & pause & exit"
Now I have a nice right-click menu to register an assembly.
cmd /c ipconfig /all & Output.txt
This command execute command and open Output.txt
file in a single command
If you want to create a cmd shortcut (for example on your desktop) add /k parameter (/k means keep, /c will close window):
cmd /k echo hello && cd c:\ && cd Windows
One more example: For example, when we use the gulp
build system, instead of
gulp
- default > build
gulp build
- build build-folder
gulp watch
- start file-watch
gulp dist
- build dist-folder
We can do that with one line:
cd c:\xampp\htdocs\project & gulp & gulp watch