用于运行.bat文件的命令

Bin*_*g32 22 windows command cmd prompt

我正在尝试使我的Visual Studio构建脚本执行一个重要的.bat文件.

这就是我现在想做的事情:

cd "F:\- Big Packets -\kitterengine\Common\" Template.bat
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

我必须这样做才能使它工作:

cd "F:\- Big Packets -\kitterengine\Common\"
F:
Template.bat
Run Code Online (Sandbox Code Playgroud)

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

我怎么能在一行中做到这一点?

小智 29

"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 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. 
Run Code Online (Sandbox Code Playgroud)

命名文件

< > : " / \ | 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
Run Code Online (Sandbox Code Playgroud)

启动一个程序

查看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
Run Code Online (Sandbox Code Playgroud)

在批处理文件中,批处理将等待程序退出.键入时,命令提示符不等待图形程序退出.

如果程序是批处理文件,则传输控制并且不执行其余的调用批处理文件.

使用开始命令

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

start "" c:\windows\notepad.exe
Run Code Online (Sandbox Code Playgroud)

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

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

start shell:cache
Run Code Online (Sandbox Code Playgroud)

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

另请注意第一组引号(如果有)必须是窗口标题.

使用呼叫命令

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

其他文件名

键入非程序文件名与双击该文件相同.


按键

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

对于其他编辑键类型Doskey /?.

  • 召回命令

  • ESC 清除命令行

  • F7 显示命令历史记录

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

  • F8 搜索命令历史记录

  • F9 按编号选择命令

  • ALT+ F10清除宏定义

也没有列出

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

  • Ctrl+ Backspace删除上一个单词

  • Home 线的开头

  • End 行结束

  • Ctrl+ End删除到行尾

  • 尽管这是有用的信息,但其中大部分与问题无关。该操作员没有询问如何使用命令行或如何使用计算机或批处理,这就是我否决它的原因:无关紧要。 (4认同)

小智 8

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

start "" /D F:\- Big Packets -\kitterengine\Common\ /W Template.bat
Run Code Online (Sandbox Code Playgroud)


小智 6

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

这是我的方法 =>

cmd /c ""Full_Path_Of_Batch_Here.cmd" "
Run Code Online (Sandbox Code Playgroud)

更多信息=>cmd /?


Mof*_*ofi 5

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

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

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

"F:\- Big Packets -\kitterengine\Common\Template.bat"
Run Code Online (Sandbox Code Playgroud)

Template.bat到达批处理文件的末尾后,如果上面的命令行位于* .bat或* .cmd文件中,则不会返回到先前的脚本。

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

cd /D "%~dp0"
Run Code Online (Sandbox Code Playgroud)

在命令提示符窗口中运行,cd /?以获取该命令的帮助,以解释参数/D...更改为指定目录,该目录也位于其他驱动器上。

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

2.调用具有完整路径的批处理文件

另一种解决方案是使用完整路径调用批处理文件。

call "F:\- Big Packets -\kitterengine\Common\Template.bat"
Run Code Online (Sandbox Code Playgroud)

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

对于当前目录,请阅读上文。

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

有3个操作员用于在一个命令行运行多个命令:&&&||
有关详细信息,请参阅使用Windows批处理文件单行中使用多个命令的答案。

我建议这项工作的&&经营者。

cd /D "F:\- Big Packets -\kitterengine\Common" && Template.bat
Run Code Online (Sandbox Code Playgroud)

与第一个解决方案一样,如果这是* .bat或* .cmd文件,则不会返回到当前脚本,并且更改目录并继续进行批处理Template.bat是成功的。

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

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

cd /D "F:\- Big Packets -\kitterengine\Common" && call Template.bat
Run Code Online (Sandbox Code Playgroud)

与第三种解决方案的区别在于,在退出时,将返回到当前的批处理脚本Template.bat

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

上面的四个解决方案更改了当前目录,并且未知如何Template.bat处理

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

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

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

setlocal & cd /D "F:\- Big Packets -\kitterengine\Common" & call Template.bat & endlocal
Run Code Online (Sandbox Code Playgroud)

现在,只有一个&而不是被&&使用,因为在这里重要的是在setlocal执行命令之后endlocal最终还要执行该命令。


一个更多的注意

如果批处理文件Template.bat包含exit不带参数的命令,/B并且该命令确实执行,则始终独立于调用层次结构退出命令进程。因此,请确保Template.bat包含exit /Bgoto :EOF而不只是exit如果有exit在这个批处理文件中使用的。