我试图让我的Visual Studio构建脚本执行一个重要的.bat文件。
这是我目前想做的:
cd "F:\- Big Packets -\kitterengine\Common\" Template.bat
但是它不起作用。
我必须这样做才能使其正常工作:
cd "F:\- Big Packets -\kitterengine\Common\"
F:
Template.bat
但是将此添加到Visual Studio脚本中相当困难。
我如何在一行代码中完成这个操作?
"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会查找:
应用程序加载的目录。
父进程的当前目录。
Windows NT/2000/XP:32位Windows系统目录。使用GetSystemDirectory函数获取此目录的路径。此目录的名称为System32。
Windows NT/2000/XP:16位Windows系统目录。没有获取此目录路径的函数,但是会被搜索。此目录的名称为System。
Windows目录。使用GetWindowsDirectory函数获取此目录的路径。
列在PATH环境变量中的目录。
指定程序名称
这是启动程序的标准方式。
c:\windows\notepad.exe
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:删除到行尾
可以参考这里:https://ss64.com/nt/start.html
start "" /D F:\- Big Packets -\kitterengine\Common\ /W Template.bat
有很多方法可以解决这个任务。
最简单的解决方案是使用完整路径运行批处理文件。
"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)的驱动器和路径。
call "F:\- Big Packets -\kitterengine\Common\Template.bat"
Template.bat
完成后,批处理继续在包含此命令行的批处理脚本中进行处理。&
、&&
和||
。详情请参见使用Windows批处理文件运行多个命令的单行答案
我建议为此任务使用&&
运算符。cd /D "F:\- Big Packets -\kitterengine\Common" && Template.bat
如果这是一个*.bat或*.cmd文件,并且更改目录并在Template.bat
上继续批处理成功,则第一种解决方案没有返回到当前脚本。
此命令行更改目录,并在成功时调用批处理文件。
cd /D "F:\- Big Packets -\kitterengine\Common" && call Template.bat
setlocal & cd /D "F:\- Big Packets -\kitterengine\Common" & call Template.bat & endlocal
&
而不是&&
,因为重要的是在执行setlocal
命令后最终也执行endlocal
命令。
Template.bat
包含没有参数/B
的exit
命令,并且实际上执行了该命令,则无论调用层次如何,命令进程始终会退出。因此,请确保Template.bat
包含exit /B
或goto :EOF
而不是仅使用exit
(如果在此批处理文件中使用了exit
)。cmd /c ""Full_Path_Of_Batch_Here.cmd" "
更多信息 => cmd /?
像Linux一样,要运行myapp.exe
,您只能使用以下三种方法之一。
将项目
目录添加到您的系统路径
中,然后:
myapp.exe
或者
myapp
\path\to\project\myapp.exe
cd \path\to\project
.\myapp.exe
"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