运行 .bat 文件的命令

44

我试图让我的Visual Studio构建脚本执行一个重要的.bat文件。

这是我目前想做的:

cd "F:\- Big Packets -\kitterengine\Common\" Template.bat

但是它不起作用。

我必须这样做才能使其正常工作:

cd "F:\- Big Packets -\kitterengine\Common\"
F:
Template.bat

但是将此添加到Visual Studio脚本中相当困难。

我如何在一行代码中完成这个操作?


3
"F:\- Big Packets -\kitterengine\Common\Template.bat" 可能会在前面加上 call (参见 call /?)。或者是 Cd /d "F:\- Big Packets -\kitterengine\Common\" & Template.bat。请参考我的 CMD 速查表 - http://stackoverflow.com/questions/40371443/what-do-all-commands-in-batch-mean-and-do - user6017774
如果不使用Visual Studio,则不清楚此问题及其答案是否适用于我。要么解释“Visual Studio构建脚本”的上下文,要么通过编辑标题明确限制问题的范围。 - oberlies
5个回答

48

"F:\- Big Packets -\kitterengine\Common\Template.bat" 可能会以 call 为前缀(参见 call /?)。或者使用 Cd /d "F:\- Big Packets -\kitterengine\Common\" & Template.bat


CMD 命令速查表

  • Cmd.exe

  • 获取帮助

  • 标点符号

  • 文件命名

  • 启动程序

  • 键盘快捷键

CMD.exe

首先要记住的是它是一种操作计算机的方式。在WIMP(Windows,Icons,Mouse,Popup menus)变得普遍之前,这是我们使用的方式。它起源于CPM、VMS和Unix。它被用来启动程序、复制和删除文件。此外,您还可以更改时间和日期。

要获取有关启动CMD的帮助,请键入cmd /?。除非您只想在其中键入,否则必须使用/k/c开关启动它。

获取帮助

要获取常规帮助,请在命令提示符中键入Help。对于列出的每个命令,请键入help <command>(例如help dir)或<command> /?(例如dir /?)。

某些命令具有子命令。例如schtasks /create /?

NET 命令的帮助文档很特别。输入 net use /? 可以获得简要帮助。输入 net help use 可以获得完整帮助。在根目录下同样适用 - 输入 net /? 是简要帮助,使用 net help

帮助文档中提到的新行为是从 OS/2 和 Windows NT4 中的 CMD 更改到当前在 Windows 2000 及更高版本中的 CMD。

WMIC 是一个多功能命令。输入 wmic /?


标点符号

&    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. 

命名文件

< > : " / \ | 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

启动程序

查看start /?call /?以获取所有三种方式的帮助。

Windows有两种类型的程序 - 控制台或非控制台(即使它们没有GUI,也称为GUI)。控制台程序附加到当前控制台或Windows创建一个新控制台。GUI程序必须显式地创建自己的窗口。

如果没有给出完整路径,则Windows会查找:

  1. 应用程序加载的目录。

  2. 父进程的当前目录。

  3. Windows NT/2000/XP:32位Windows系统目录。使用GetSystemDirectory函数获取此目录的路径。此目录的名称为System32。

  4. Windows NT/2000/XP:16位Windows系统目录。没有获取此目录路径的函数,但是会被搜索。此目录的名称为System。

  5. Windows目录。使用GetWindowsDirectory函数获取此目录的路径。

  6. 列在PATH环境变量中的目录。

指定程序名称

这是启动程序的标准方式。

c:\windows\notepad.exe

在批处理文件中,批处理将等待程序退出。当在命令提示符中键入时,不会等待图形化程序退出。
如果程序是批处理文件,则控制被转移,调用批处理文件的其余部分不会被执行。 使用Start命令 Start以非标准方式启动程序。
start "" c:\windows\notepad.exe

Start 启动程序并且不等待。控制台程序将在新窗口中启动。使用 /b 开关可以将控制台程序强制放入相同的窗口中,这会抵消 Start 的主要目的。

Start 使用 Windows 图形外壳 - 与在 WinKey + R(运行对话框)中输入相同。试一试。

start shell:cache

还有在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths 下注册的程序名称也可以不指定完整路径而直接输入。

同时请注意,如果有任何引号,则第一组引号必须是窗口标题。

使用 Call 命令

Call 用于启动批处理文件并等待它们退出并继续当前批处理文件。

其他文件名

输入非程序文件名与双击该文件相同。


快捷键

Ctrl + C 可以在不关闭控制台窗口的情况下退出程序。

要查看其他编辑键,请输入 Doskey /?

  • 可以回顾命令历史记录

  • ESC 可以清除命令行

  • F7 可以显示命令历史记录

  • ALT+F7 可以清除命令历史记录

  • F8 可以搜索命令历史记录

  • F9 可以通过编号选择一个命令

  • ALT+F10 可以清除宏定义

还有其他未列出的快捷键。

  • Ctrl + :每次移动一个单词

  • Ctrl + Backspace:删除前一个单词

  • Home:移到行首

  • End:移到行尾

  • Ctrl + End:删除到行尾


4
尽管这是有用的信息,但其中大部分与问题无关。提问者并没有询问如何使用命令行或如何使用计算机或批处理等一般性内容,这就是我为什么给它点了踩的原因:不相关。 - JustAFellowCoder
虽然不相关,但对于像我这样在CMD和任何基于命令行的东西上都是新手的人来说非常有用。StackOverflow应该开设某种维基,以便像这个答案这样有用的答案可以轻松地在Google上搜索 :) - Prid

24

9

有很多方法可以解决这个任务。

1. 使用完整路径运行批处理文件

最简单的解决方案是使用完整路径运行批处理文件。

"F:\- Big Packets -\kitterengine\Common\Template.bat"

一旦批处理文件Template.bat结束,如果上述命令行在*.bat或*.cmd文件中,则无法返回到先前的脚本。
批处理文件Template.bat的当前目录是当前进程的当前目录。如果Template.bat要求该批处理文件的目录是当前目录,则批处理文件Template.bat应在@echo off之后作为第二行包含以下命令行:
cd /D "%~dp0"

在命令提示符窗口中运行cd /?以显示该命令的帮助信息,其中解释了参数/D...可切换到不同驱动器上的指定目录。
在命令提示符窗口中运行call /?以显示该命令的帮助信息,该命令也用于第2、4和5个解决方案,并解释了%~dp0,它是批处理文件名(参数0)的驱动器和路径。

2. 使用完整路径调用批处理文件

另一种解决方案是使用完整路径调用批处理文件。
call "F:\- Big Packets -\kitterengine\Common\Template.bat"

与第一解决方案不同的是,在批处理文件 Template.bat 完成后,批处理继续在包含此命令行的批处理脚本中进行处理。
有关当前目录的信息,请参阅上文。

3. 改变目录并使用一个命令行运行批处理文件

有三个运算符可用于在一个命令行上运行多个命令:&&&||。详情请参见使用Windows批处理文件运行多个命令的单行答案 我建议为此任务使用&&运算符。
cd /D "F:\- Big Packets -\kitterengine\Common" && Template.bat

如果这是一个*.bat或*.cmd文件,并且更改目录并在Template.bat上继续批处理成功,则第一种解决方案没有返回到当前脚本。

4. 一条命令行更改目录并调用批处理文件

此命令行更改目录,并在成功时调用批处理文件。

cd /D "F:\- Big Packets -\kitterengine\Common" && call Template.bat

第三种解决方法与此不同的是,在处理完“Template.bat”后返回当前批处理脚本。
5. 一条命令行改变目录并保持当前环境调用批处理文件
前面提到的四种解决方法都会改变当前目录,而无法确定“Template.bat”在以下方面做出了哪些更改:
1. 当前目录 2. 环境变量 3. 命令扩展状态 4. 延迟扩展状态
如果必须确保当前*.bat或*.cmd脚本的环境不受“Template.bat”对自身环境所做的任何更改的影响,建议使用“setlocal”和“endlocal”。
在命令提示符窗口中运行“setlocal /?”和“endlocal /?”以显示这两个命令的帮助信息。阅读change directory command cd ..not working in batch file after npm install上的答案,了解这两个命令的详细作用。
setlocal & cd /D "F:\- Big Packets -\kitterengine\Common" & call Template.bat & endlocal

现在只使用&而不是&&,因为重要的是在执行setlocal命令后最终也执行endlocal命令。
再注意一下 如果批处理文件Template.bat包含没有参数/Bexit命令,并且实际上执行了该命令,则无论调用层次如何,命令进程始终会退出。因此,请确保Template.bat包含exit /Bgoto :EOF而不是仅使用exit(如果在此批处理文件中使用了exit)。

6
你可以使用Cmd命令来运行批处理文件。
以下是我的方法=>
cmd /c ""Full_Path_Of_Batch_Here.cmd" "

更多信息 => cmd /?


谢谢!我可以从命令提示符中知道错误,因为如果我只是双击运行批处理文件,我就不会知道错误在哪里 :) - Cayenne Teoh

-1

像Linux一样,要运行myapp.exe,您只能使用以下三种方法之一。

使用系统路径

项目目录添加到您的系统路径中,然后:

myapp.exe

或者

myapp

使用完整的长路径

\path\to\project\myapp.exe

进入工作目录

cd \path\to\project
.\myapp.exe

问题是“运行.bat文件的命令”,它涉及到BAT文件而不是EXE! - Oguzhan Bolukbas
当问题和答案更为普遍时,会涉及到很多人,而不仅仅是一个人。 - nextloop

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接