命令 运行 .bat 文件

Command to run a .bat file

我正在尝试让我的 Visual Studio 构建脚本执行一个 .bat 文件,它会做一些重要的事情。

这是我现在想做的事情:

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

但是没用。

我必须这样做才能让它发挥作用:

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

但这很难添加到 Visual Studio 脚本中。

如何在一行中完成此操作?

解决这个任务有很多可能性。

1。 运行 完整路径的批处理文件

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

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

一旦到达批处理文件的末尾Template.bat,如果上面的命令行在 *.bat 或 *.cmd 文件中,则没有 return 到先前的脚本。

批处理文件的当前目录Template.bat是当前进程的当前目录。如果 Template.bat 要求此批处理文件的目录是当前目录,则批处理文件 Template.bat 应在 @echo off 之后的第二行包含以下命令行:

cd /D "%~dp0"

运行 在命令提示符中 window cd /? 用于显示此命令的帮助解释参数 /D ... 也更改为不同的指定目录开车。

运行 在命令提示符中 window call /? 用于显示此命令的帮助也用于 2.、4. 和 5. 解决方案并解释 %~dp0 ...参数 0 的驱动器和路径,这是批处理文件的名称。

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

另一个解决方案是调用具有完整路径的批处理文件。

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

与第一个解决方案的不同之处在于,在到达批处理文件 Template.bat 末尾后,批处理将在包含此命令行的批处理脚本中继续。

对于上面阅读的当前目录。

3。使用一个命令行

更改目录和 运行 批处理文件

一个命令行上有 3 个用于 运行 多个命令的运算符:&&&||
有关详细信息,请参阅 Single line with multiple commands using Windows batch file

上的答案

我建议 && 操作员完成此任务。

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

与第一个解决方案一样,如果这是一个 *.bat 或 *.cmd 文件并且更改目录并继续对 Template.bat 进行批处理,则当前脚本没有 return。

4。使用一个命令行更改目录和调用批处理文件

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

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

与第三种解决方案的不同之处在于 return 当前批处理脚本退出处理 Template.bat

5。使用一个命令行更改目录和 CALL 批处理文件并保持当前环境

以上四种方案改变了当前目录,不知道Template.bat

做了什么
  1. 当前目录
  2. 环境变量
  3. 命令扩展状态
  4. 延迟展开状态

如果保持当前 *.bat 或 *.cmd 脚本的环境不受任何 Template.bat 自身环境变化的影响很重要,建议使用 setlocalendlocal.

运行 在命令提示符中 window setlocal /?endlocal /? 用于显示这两个命令的帮助。并阅读 上的答案,更详细地解释这两个命令的作用。

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

现在只有 & 而不是 &&,因为这里很重要,在执行 setlocal 之后,命令 endlocal 最终也被执行。


再注意一点

如果批处理文件Template.bat 包含不带参数/B 的命令exit 并且该命令确实被执行,则命令进程总是独立于调用层次退出。因此,如果此批处理文件中完全使用了 exit,请确保 Template.bat 包含 exit /Bgoto :EOF 而不仅仅是 exit

"F:\- Big Packets -\kitterengine\Common\Template.bat" 可能以 call 开头(参见 call /?)。或者 Cd /d "F:\- Big Packets -\kitterengine\Common\" & Template.bat


CMD作弊Sheet

  • Cmd.exe

  • 获得帮助

  • 标点

  • 命名文件

  • 启动程序

CMD.exe

首先要记住它是一种操作计算机的方式。在 WIMP(Windows、图标、鼠标、弹出菜单)变得普遍之前,我们就是这样做的。它归功于 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)。控制台程序附加到当前控制台或 Windows 创建一个新控制台。 GUI 程序必须显式创建自己的 windows.

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

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

  2. parent 进程的当前目录。

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

  4. Windows NT/2000/XP:16位Windows系统目录。没有 获取该目录路径的函数,但它是 搜索。这个目录的名字是系统。

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

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

指定程序名称

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

c:\windows\notepad.exe

在批处理文件中,批处理将等待程序退出。什么时候 输入命令提示符不等待图形 要退出的程序。

如果程序是一个批处理文件,控制权将被转移,调用批处理文件的其余部分将不会被执行。

使用启动命令

Start 以非标准方式启动程序。

start "" c:\windows\notepad.exe

Start 启动程序,不等待。控制台程序以新的 window 开始。使用 /b 开关强制控制台程序进入相同的 window,这否定了启动的主要目的。

开始使用 Windows 图形 shell - 与键入 WinKey + R(运行 对话框)相同。尝试

start shell:cache

此外,在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths 下注册的程序名称也可以在不指定完整路径的情况下键入。

另请注意第一组引号(如果有)必须是 window 标题。

使用调用命令

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

其他文件名

键入非程序文件名与双击文件的效果相同。


Ctrl + C 退出程序而不退出控制台window.

对于其他编辑键,键入 Doskey /?

  • 调用命令

  • ESC 清除命令行

  • F7 显示命令历史记录

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

  • F8 搜索历史命令

  • F9 按编号选择命令

  • ALT+F10 清除宏定义

也未列出

  • Ctrl + 移动一次一个字

  • Ctrl + Backspace 删除前一个字

  • 首页行首

  • 结束 行结束

  • Ctrl + End 删除到行尾

可以参考这里:https://ss64.com/nt/start.html

start "" /D F:\- Big Packets -\kitterengine\Common\ /W Template.bat

您可以使用 Cmd 命令来 运行 批处理文件。

这是我的方式=>

cmd /c ""Full_Path_Of_Batch_Here.cmd" "

更多信息 => cmd /?