windows 运行 .bat 文件的命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41030190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command to run a .bat file
提问by BingBang32
I'm trying to make my Visual Studio build script execute a .bat file that does something important.
我正在尝试让我的 Visual Studio 构建脚本执行一个 .bat 文件,该文件执行一些重要的操作。
Here is what I'm want to do right now:
这是我现在想要做的:
cd "F:\- Big Packets -\kitterengine\Common\" Template.bat
But it doesn't work.
但它不起作用。
I have to do this to make it work:
我必须这样做才能使其工作:
cd "F:\- Big Packets -\kitterengine\Common\"
F:
Template.bat
But this is pretty difficult to add to the Visual Studio script.
但这很难添加到 Visual Studio 脚本中。
How can I do this in one single line?
我怎样才能在一行中做到这一点?
采纳答案by jwgan
Can refer to here: https://ss64.com/nt/start.html
可以参考这里:https: //ss64.com/nt/start.html
start "" /D F:\- Big Packets -\kitterengine\Common\ /W Template.bat
回答by
"F:\- Big Packets -\kitterengine\Common\Template.bat"
maybe prefaced with call
(see call /?
). Or Cd /d "F:\- Big Packets -\kitterengine\Common\" & Template.bat
.
"F:\- Big Packets -\kitterengine\Common\Template.bat"
可能以call
(参见call /?
)开头。或者Cd /d "F:\- Big Packets -\kitterengine\Common\" & Template.bat
。
CMD Cheat Sheet
CMD备忘单
Cmd.exe
Getting Help
Punctuation
Naming Files
Starting Programs
Keys
命令行工具
获得帮助
标点
命名文件
启动程序
钥匙
CMD.exe
命令行工具
First thing to remember its a way of operating a computer. It's the way we did it before WIMP (Windows, Icons, Mouse, Popup menus) became common. It owes it roots to CPM, VMS, and Unix. It was used to start programs and copy and delete files. Also you could change the time and date.
首先要记住它是一种操作计算机的方式。在 WIMP(Windows、图标、鼠标、弹出菜单)变得普遍之前,我们就是这样做的。它源于 CPM、VMS 和 Unix。它用于启动程序以及复制和删除文件。您也可以更改时间和日期。
For help on starting CMD type cmd /?
. You must start it with either the /k
or /c
switch unless you just want to type in it.
有关启动 CMD 类型的帮助cmd /?
。您必须使用/k
或/c
开关启动它,除非您只想输入它。
Getting Help
获得帮助
For general help. Type Help
in the command prompt. For each command listed type help <command>
(eg help dir
) or <command> /?
(eg dir /?
).
对于一般帮助。键入Help
在命令提示。对于列出的每个命令类型help <command>
(例如help dir
)或<command> /?
(例如dir /?
)。
Some commands have sub commands. For example schtasks /create /?
.
有些命令有子命令。例如 schtasks /create /?
。
The NET
command's help is unusual. Typing net use /?
is brief help. Type net help use
for full help. The same applies at the root - net /?
is also brief help, use net help
.
该NET
命令的帮助是不寻常的。打字net use /?
是简短的帮助。键入net help use
以获得完整帮助。这同样适用于根 -net /?
也是简短的帮助,使用net help
.
References in Help to new behaviour are describing changes from CMD in OS/2 and Windows NT4 to the current CMD which is in Windows 2000 and later.
帮助中对新行为的引用描述了从 OS/2 和 Windows NT4 中的 CMD 到 Windows 2000 和更高版本中的当前 CMD 的更改。
WMIC
is a multipurpose command. Type wmic /?
.
WMIC
是一个多用途命令。键入wmic /?
。
Punctuation
标点
& seperates commands on a line.
&& executes this command only if previous command's errorlevel is 0.
|| (not used above) executes this command only if previous command's
errorlevel is NOT 0
> output to a file
>> append output to a file
< input from a file
2> Redirects command error output to the file specified. (0 is StdInput, 1 is StdOutput, and 2 is StdError)
2>&1 Redirects command error output to the same location as command output.
| output of one command into the input of another command
^ escapes any of the above, including itself, if needed to be passed
to a program
" parameters with spaces must be enclosed in quotes
+ used with copy to concatenate files. E.G. copy file1+file2 newfile
, used with copy to indicate missing parameters. This updates the files
modified date. E.G. copy /b file1,,
%variablename% a inbuilt or user set environmental variable
!variablename! a user set environmental variable expanded at execution
time, turned with SelLocal EnableDelayedExpansion command
%<number> (%1) the nth command line parameter passed to a batch file. %0
is the batchfile's name.
%* (%*) the entire command line.
%CMDCMDLINE% - expands to the original command line that invoked the
Command Processor (from set /?).
%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop.
Single % sign at command prompt and double % sign in a batch file.
\ (\servername\sharename\folder\file.ext) access files and folders via UNC naming.
: (win.ini:streamname) accesses an alternative steam. Also separates drive from rest of path.
. (win.ini) the LAST dot in a file path separates the name from extension
. (dir .\*.txt) the current directory
.. (cd ..) the parent directory
\?\ (\?\c:\windows\win.ini) When a file path is prefixed with \?\ filename checks are turned off.
Naming Files
命名文件
< > : " / \ | Reserved characters. May not be used in filenames.
Reserved names. These refer to devices eg,
copy filename con
which copies a file to the console window.
CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4,
COM5, COM6, COM7, COM8, COM9, LPT1, LPT2,
LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9
CONIN$, CONOUT$, CONERR$
--------------------------------
Maximum path length 260 characters
Maximum path length (\?\) 32,767 characters (approx - some rare characters use 2 characters of storage)
Maximum filename length 255 characters
Starting a Program
启动程序
See start /?
and call /?
for help on all three ways.
请参阅start /?
并call /?
获取有关所有三种方式的帮助。
There are two types of Windows programs - console or non console (these are called GUI even if they don't have one). Console programs attach to the current console or Windows creates a new console. GUI programs have to explicitly create their own windows.
有两种类型的 Windows 程序 - 控制台程序或非控制台程序(即使它们没有,也称为 GUI)。控制台程序附加到当前控制台或 Windows 创建一个新控制台。GUI 程序必须显式地创建它们自己的窗口。
If a full path isn't given then Windows looks in
如果未给出完整路径,则 Windows 会查找
The directory from which the application loaded.
The current directory for the parent process.
Windows NT/2000/XP: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is System32.
Windows NT/2000/XP: The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The directories that are listed in the PATH environment variable.
应用程序加载的目录。
父进程的当前目录。
Windows NT/2000/XP:32 位 Windows 系统目录。使用 GetSystemDirectory 函数获取该目录的路径。该目录的名称是 System32。
Windows NT/2000/XP:16 位 Windows 系统目录。没有获取这个目录路径的函数,但是会搜索。该目录的名称是 System。
Windows 目录。使用 GetWindowsDirectory 函数获取该目录的路径。
PATH 环境变量中列出的目录。
Specify a program name
指定程序名称
This is the standard way to start a program.
这是启动程序的标准方式。
c:\windows\notepad.exe
In a batch file the batch will wait for the program to exit. When typed the command prompt does not wait for graphical programs to exit.
在批处理文件中,批处理将等待程序退出。键入时,命令提示符不会等待图形程序退出。
If the program is a batch file control is transferred and the rest of the calling batch file is not executed.
如果程序是批处理文件,则控制被转移并且调用批处理文件的其余部分不执行。
Use Start command
使用启动命令
Start
starts programs in non standard ways.
Start
以非标准方式启动程序。
start "" c:\windows\notepad.exe
Start
starts a program and does not wait. Console programs start in a new window. Using the /b
switch forces console programs into the same window, which negates the main purpose of Start.
Start
启动一个程序,不等待。控制台程序在新窗口中启动。使用该/b
开关会强制控制台程序进入同一窗口,这与 Start 的主要目的背道而驰。
Start uses the Windows graphical shell - same as typing in WinKey + R (Run dialog). Try
开始使用 Windows 图形外壳 - 与键入 WinKey + R(运行对话框)相同。尝试
start shell:cache
Also program names registered under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
can also be typed without specifying a full path.
也HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
可以在不指定完整路径的情况下键入在其下注册的程序名称。
Also note the first set of quotes, if any, MUST be the window title.
还要注意第一组引号(如果有)必须是窗口标题。
Use Call command
使用调用命令
Call is used to start batch files and wait for them to exit and continue the current batch file.
调用用于启动批处理文件并等待它们退出并继续当前批处理文件。
Other Filenames
其他文件名
Typing a non program filename is the same as double clicking the file.
键入非程序文件名与双击文件相同。
Keys
钥匙
Ctrl + Cexits a program without exiting the console window.
Ctrl + C退出程序而不退出控制台窗口。
For other editing keys type Doskey /?
.
对于其他编辑键,请键入Doskey /?
.
↑and ↓recall commands
ESCclears command line
F7displays command history
ALT+F7clears command history
F8searches command history
F9selects a command by number
ALT+F10clears macro definitions
↑并↓调用命令
ESC清除命令行
F7显示命令历史
ALT+F7清除命令历史
F8搜索命令历史
F9按编号选择命令
ALT+F10清除宏定义
Also not listed
也没有列出
Ctrl+ ←or→Moves a word at a time
Ctrl+ BackspaceDeletes the previous word
HomeBeginning of line
EndEnd of line
Ctrl+ EndDeletes to end of line
Ctrl+←或 一次→移动一个词
Ctrl+Backspace删除上一个单词
Home行首
End行结束
Ctrl+End删除到行尾
回答by Mofi
There are many possibilities to solve this task.
有很多可能性可以解决这个任务。
1. RUN the batch file with full path
1.使用完整路径运行批处理文件
The easiest solution is runningthe batch file with full path.
最简单的解决方案是使用完整路径运行批处理文件。
"F:\- Big Packets -\kitterengine\Common\Template.bat"
Once end of batch file Template.bat
is reached, there is no return to previous script in case of the command line above is within a *.bat or *.cmd file.
一旦Template.bat
到达批处理文件的末尾,如果上面的命令行在 *.bat 或 *.cmd 文件中,则不会返回到前一个脚本。
The current directory for the batch file Template.bat
is the current directory of the current process. In case of Template.bat
requires that the directory of this batch file is the current directory, the batch file Template.bat
should contain after @echo off
as second line the following command line:
批处理文件Template.bat
的当前目录是当前进程的当前目录。如果Template.bat
要求此批处理文件的目录是当前目录,则批处理文件Template.bat
应包含@echo off
以下命令行作为第二行:
cd /D "%~dp0"
Run in a command prompt window cd /?
for getting displayed the help of this command explaining parameter /D
... change to specified directory also on a different drive.
在命令提示符窗口中运行以cd /?
显示此命令的帮助解释参数/D
...更改到不同驱动器上的指定目录。
Run in a command prompt window call /?
for getting displayed the help of this command used also in 2., 4. and 5. solution and explaining also %~dp0
... drive and path of argument 0 which is the name of the batch file.
在命令提示符窗口中运行以call /?
显示也在 2.、4. 和 5. 解决方案中使用的此命令的帮助,并解释了%~dp0
......参数 0 的驱动器和路径,即批处理文件的名称。
2. CALL the batch file with full path
2.使用完整路径调用批处理文件
Another solution is callingthe batch file with full path.
另一种解决方案是使用完整路径调用批处理文件。
call "F:\- Big Packets -\kitterengine\Common\Template.bat"
The difference to first solution is that after end of batch file Template.bat
is reached the batch processing continues in batch script containing this command line.
与第一个解决方案的不同之处在于,在Template.bat
到达批处理文件末尾后,批处理将在包含此命令行的批处理脚本中继续。
For the current directory read above.
对于当前目录,请阅读上面的内容。
3. Change directory and RUN batch file with one command line
3. 用一个命令行更改目录和运行批处理文件
There are 3 operators for running multiple commands on one command line: &
, &&
and ||
.
For details see answer on Single line with multiple commands using Windows batch file
有 3 个运算符可用于在一个命令行上运行多个命令:&
、&&
和||
。
有关详细信息,请参阅使用 Windows 批处理文件的带有多个命令的单行的答案
I suggest for this task the &&
operator.
我建议&&
操作员执行此任务。
cd /D "F:\- Big Packets -\kitterengine\Common" && Template.bat
As on first solution there is no return to current script if this is a *.bat or *.cmd file and changing the directory and continuation of batch processing on Template.bat
is successful.
作为第一个解决方案,如果这是一个 *.bat 或 *.cmd 文件并且更改目录并继续批处理Template.bat
成功,则不会返回到当前脚本。
4. Change directory and CALL batch file with one command line
4. 使用一个命令行更改目录并调用批处理文件
This command line changes the directory and on success callsthe batch file.
此命令行更改目录并成功调用批处理文件。
cd /D "F:\- Big Packets -\kitterengine\Common" && call Template.bat
The difference to third solution is the return to current batch script on exiting processing of Template.bat
.
与第三种解决方案的不同之处在于在退出处理Template.bat
.
5. Change directory and CALL batch file with keeping current environment with one command line
5.更改目录并调用批处理文件,并使用一个命令行保持当前环境
The four solutions above change the current directory and it is unknown what Template.bat
does regarding
上面四种解决方案改变了当前目录,Template.bat
不知道是做什么的
- current directory
- environment variables
- command extensions state
- delayed expansion state
- 当前目录
- 环境变量
- 命令扩展状态
- 延迟扩张状态
In case of it is important to keep the environment of current *.bat or *.cmd script unmodified by whatever Template.bat
changes on environment for itself, it is advisable to use setlocal
and endlocal
.
如果保持当前 *.bat 或 *.cmd 脚本的环境不受Template.bat
环境本身的任何更改的修改很重要,建议使用setlocal
和endlocal
。
Run in a command prompt window setlocal /?
and endlocal /?
for getting displayed the help of these two commands. And read answer on change directory command cd ..not working in batch file after npm installexplaining more detailed what these two commands do.
在命令提示符窗口中运行setlocal /?
并endlocal /?
显示这两个命令的帮助。并阅读有关更改目录命令 cd ..not working in batch file after npm install 的答案,更详细地解释了这两个命令的作用。
setlocal & cd /D "F:\- Big Packets -\kitterengine\Common" & call Template.bat & endlocal
Now there is only &
instead of &&
used as it is important here that after setlocal
is executed the command endlocal
is finally also executed.
现在只有&
而不是&&
使用,因为在这里很重要的是,在setlocal
执行之后,命令endlocal
最终也会被执行。
ONE MORE NOTE
多一点注意
If batch file Template.bat
contains the command exit
without parameter /B
and this command is really executed, the command process is always exited independent on calling hierarchy. So make sure Template.bat
contains exit /B
or goto :EOF
instead of just exit
if there is exit
used at all in this batch file.
如果批处理文件中Template.bat
包含exit
不带参数的命令/B
并且该命令确实被执行,则命令进程总是独立于调用层次结构而退出。因此,请确保Template.bat
包含exit /B
或goto :EOF
而不只是exit
如果有exit
在这个批处理文件中使用的。
回答by scientist_7
You can use Cmd command to run Batch file.
您可以使用 Cmd 命令来运行批处理文件。
Here is my way =>
这是我的方式=>
cmd /c ""Full_Path_Of_Batch_Here.cmd" "
More information => cmd /?
更多信息=> cmd /?