命令来运行.bat文件

我试图让我的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,图标,鼠标,弹出式菜单)变得普遍之前做的方式。 它的根源在于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 concatinate files. EG copy file1+file2 newfile , used with copy to indicate missing parameters. This updates the files modified date. EG 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会查找

  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 "" c:\windows\notepad.exe 

Start启动一个程序,不等待。 控制台程序在新窗口中启动。 使用/b开关强制控制台程序进入同一窗口,否定了启动的主要目的。

开始使用Windows图形化外壳 – 与在WinKey + R(运行对话框)中输入相同。 尝试

 start shell:cache 

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

还要注意,第一组引号(如果有的话)必须是窗口标题。

使用呼叫命令

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

其他文件名

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


按键

Ctrl + C退出程序而不退出控制台窗口。

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

  • 调用命令

  • ESC清除命令行

  • F7显示命令历史

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

  • F8搜索命令历史记录

  • F9按编号选择一个命令

  • ALT + F10清除宏定义

也没有列出

  • Ctrl + →一次移动一个单词

  • Ctrl + Backspace删除上一个单词

  • 首页开始的行

  • 结束行结束

  • Ctrl + End删除到行尾

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

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.使用一个命令行更改目录和RUN批处理文件

在一个命令行上有三个运算符可以运行多个命令: &&&||
有关详细信息,请参阅单行使用Windows批处理文件的多个命令的答案

我建议这个任务&&操作符。

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

至于第一种解决方案,如果是* .bat或* .cmd文件,则不会返回当前脚本,并且在Template.bat上更改目录和批处理的继续是成功的。

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

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

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

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

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

上面的四个解决方案改变了当前目录, Template.bat不知道关于什么

  1. 当前目录
  2. 环境变量
  3. 命令扩展状态
  4. 延迟扩张状态

如果要保持当前* .bat或* .cmd脚本的环境不受Template.bat在环境上自己改变的任何修改,建议使用setlocalendlocal

在命令提示符窗口中运行setlocal /? endlocal /? 为了显示这两个命令的帮助。 并阅读更改目录命令cd ..不能在批处理文件中工作后npm安装解释更详细的这两个命令做什么。

 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使用的话。

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

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