我想在Windows CMD控制台中运行两个命令。
在Linux中我会这样做
touch thisfile ; ls -lstrh
在Windows上是怎么做的呢?
我想在Windows CMD控制台中运行两个命令。
在Linux中我会这样做
touch thisfile ; ls -lstrh
在Windows上是怎么做的呢?
当前回答
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\" &暂停&退出
这就像一个咒语——RegAsm在文件上运行并显示其结果,然后显示“按任意键继续…”提示符,然后当按下一个键时命令提示符窗口关闭。
P.S.对于其他可能感兴趣的人,你可以使用以下.reg文件条目来添加一个dllfile关联到.dll文件,然后再添加一个RegAsm命令扩展名(注意转义的引号和反斜杠):
[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"
现在我有了一个很好的右击菜单来注册程序集。
其他回答
为了同时执行两个命令,必须在两个命令之间加上&(&)符号。像这样:
color 0a & start chrome.exe
干杯!
你可以使用call来克服环境变量被太快求值的问题。
set A=Hello & call echo %A%
尝试用这些行创建一个.bat或.cmd文件,使用doskey和$T,相当于&,在一行中执行多个命令行:
touch=echo off $T echo. ^> $* $T dir /B $T echo on
它将创建一个空文件。
例子:
touch myfile
在cmd中,你会得到这样的东西:
但是正如前面提到的,建议使用&操作符从CMD提示符在一行中执行多个命令行。
喜欢=)
当在同一行上运行多个命令时,可以使用许多处理符号,在某些情况下可能导致处理重定向,在其他情况下可能改变输出,或者导致失败。一个重要的例子是将操作变量的命令放在同一行。
@echo off
setlocal enabledelayedexpansion
set count=0
set "count=1" & echo %count% !count!
0 1
正如您在上面的例子中看到的,当使用变量的命令被放在同一行时,您必须使用延迟展开来更新变量值。如果你的变量被索引了,使用CALL命令和%%修饰符来更新它在同一行的值:
set "i=5" & set "arg!i!=MyFile!i!" & call echo path!i!=%temp%\%%arg!i!%%
path5=C:\Users\UserName\AppData\Local\Temp\MyFile5
在windows 10中,你也可以使用scriprunner:
ScriptRunner.exe -appvscript demoA.cmd arg1 arg2 -appvscriptrunnerparameters -wait -timeout=30 -rollbackonerror -appvscript demoB.ps1 arg3 arg4 -appvscriptrunnerparameters -wait -timeout=30 -rollbackonerror
它允许你在一行上启动几个命令,你可以连续运行它们,也可以不等待彼此,你可以设置超时和错误回滚。