简体   繁体   English

运行 .bat 文件的命令

[英]Command to run a .bat file

I'm trying to make my Visual Studio build script execute a .bat file that does something important.我试图让我的 Visual Studio 构建脚本执行一个 .bat 文件,该文件做一些重要的事情。

Here is what I'm want to do right now:这是我现在想做的事情:

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

But it doesn't work.但它不起作用。

I have to do this to make it work:我必须这样做才能使其工作:

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

But this is pretty difficult to add to the Visual Studio script.但这很难添加到 Visual Studio 脚本中。

How can I do this in one single line?我怎样才能在一行中做到这一点?

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


CMD Cheat Sheet CMD 备忘单

  • Cmd.exe命令文件

  • Getting Help获得帮助

  • Punctuation标点

  • Naming Files命名文件

  • Starting Programs启动程序

  • Keys钥匙

CMD.exe命令文件

First thing to remember its a way of operating a computer.首先要记住它是一种操作计算机的方式。 It's the way we did it before WIMP (Windows, Icons, Mouse, Popup menus) became common.这是我们在 WIMP(Windows、图标、鼠标、弹出菜单)变得普遍之前所做的方式。 It owes it roots to CPM, VMS, and Unix.它源于 CPM、VMS 和 Unix。 It was used to start programs and copy and delete files.它用于启动程序以及复制和删除文件。 Also you could change the time and date.您也可以更改时间和日期。

For help on starting CMD type cmd /?有关启动 CMD 类型的帮助cmd /? . . You must start it with either the /k or /c switch unless you just want to type in it.除非您只想输入它,否则您必须使用/k/c开关启动它。

Getting Help获得帮助

For general help.寻求一般帮助。 Type Help in the command prompt.在命令提示符中键入Help For each command listed type help <command> (eg help dir ) or <command> /?对于列出的每个命令,键入help <command> (例如help dir )或<command> /? (eg dir /? ). (例如dir /? )。

Some commands have sub commands.有些命令有子命令。 For example schtasks /create /?例如schtasks /create /? . .

The NET command's help is unusual. NET命令的帮助是不寻常的。 Typing net use /?键入net use /? is brief help.是简短的帮助。 Type net help use for full help.键入net help use以获得完整的帮助。 The same applies at the root - net /?这同样适用于根 - net /? is also brief help, use net help .也是简要帮助,使用net help

References in Help to new behaviour are describing changes from CMD in OS/2 and Windows NT4 to the current CMD which is in Windows 2000 and later.帮助中对新行为的引用描述了从 OS/2 和 Windows NT4 中的 CMD 到 Windows 2000 和更高版本中的当前 CMD 的更改。

WMIC is a multipurpose command. WMIC是一个多用途命令。 Type wmic /? wmic /? . .


Punctuation标点

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

Naming Files命名文件

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

Starting a Program启动程序

See start /?start /? and call /?call /? for help on all three ways.寻求所有三种方式的帮助。

There are two types of Windows programs - console or non console (these are called GUI even if they don't have one).有两种类型的 Windows 程序 - 控制台或非控制台(即使它们没有,也称为 GUI)。 Console programs attach to the current console or Windows creates a new console.控制台程序附加到当前控制台或 Windows 创建一个新控制台。 GUI programs have to explicitly create their own windows. GUI 程序必须明确创建自己的窗口。

If a full path isn't given then Windows looks in如果未给出完整路径,则 Windows 会查找

  1. The directory from which the application loaded.加载应用程序的目录。

  2. The current directory for the parent process.父进程的当前目录。

  3. Windows NT/2000/XP: The 32-bit Windows system directory. Windows NT/2000/XP:32 位 Windows 系统目录。 Use the GetSystemDirectory function to get the path of this directory.使用 GetSystemDirectory 函数获取该目录的路径。 The name of this directory is System32.此目录的名称是 System32。

  4. Windows NT/2000/XP: The 16-bit Windows system directory. Windows NT/2000/XP:16 位 Windows 系统目录。 There is no function that obtains the path of this directory, but it is searched.没有函数获取这个目录的路径,但是被搜索了。 The name of this directory is System.此目录的名称是 System.

  5. The Windows directory. Windows 目录。 Use the GetWindowsDirectory function to get the path of this directory.使用 GetWindowsDirectory 函数获取该目录的路径。

  6. The directories that are listed in the PATH environment variable. PATH 环境变量中列出的目录。

Specify a program name指定程序名称

This is the standard way to start a program.这是启动程序的标准方式。

c:\windows\notepad.exe

In a batch file the batch will wait for the program to exit.在批处理文件中,批处理将等待程序退出。 When typed the command prompt does not wait for graphical programs to exit.键入时,命令提示符不会等待图形程序退出。

If the program is a batch file control is transferred and the rest of the calling batch file is not executed.如果程序是批处理文件,则控制被转移,调用批处理文件的其余部分不被执行。

Use Start command使用启动命令

Start starts programs in non standard ways. Start以非标准方式启动程序。

start "" c:\windows\notepad.exe

Start starts a program and does not wait. Start启动一个程序并且不等待。 Console programs start in a new window.控制台程序在新窗口中启动。 Using the /b switch forces console programs into the same window, which negates the main purpose of Start.使用/b开关强制控制台程序进入同一个窗口,这否定了 Start 的主要目的。

Start uses the Windows graphical shell - same as typing in WinKey + R (Run dialog).开始使用 Windows 图形外壳 - 与键入 WinKey + R(运行对话框)相同。 Try尝试

start shell:cache

Also program names registered under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths can also be typed without specifying a full path.此外,在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths下注册的程序名称也可以在不指定完整路径的情况下键入。

Also note the first set of quotes, if any, MUST be the window title.还要注意第一组引号,如果有的话,必须是窗口标题。

Use Call command使用呼叫命令

Call is used to start batch files and wait for them to exit and continue the current batch file.调用用于启动批处理文件并等待它们退出并继续当前的批处理文件。

Other Filenames其他文件名

Typing a non program filename is the same as double clicking the file.键入非程序文件名与双击文件相同。


Keys钥匙

Ctrl + C exits a program without exiting the console window. Ctrl + C退出程序而不退出控制台窗口。

For other editing keys type Doskey /?对于其他编辑键类型Doskey /? . .

  • and recall commands 调用命令

  • ESC clears command line ESC清除命令行

  • F7 displays command history F7显示命令历史

  • ALT + F7 clears command history ALT + F7清除命令历史

  • F8 searches command history F8搜索命令历史

  • F9 selects a command by number F9按编号选择命令

  • ALT + F10 clears macro definitions ALT + F10清除宏定义

Also not listed也未列出

  • Ctrl + or Moves a word at a time Ctrl + 一次移动一个单词

  • Ctrl + Backspace Deletes the previous word Ctrl + Backspace删除前一个单词

  • Home Beginning of line首页行首

  • End End of line End行尾

  • Ctrl + End Deletes to end of line Ctrl + End删除到行尾

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

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

There are many possibilities to solve this task.解决这个任务有很多可能性。

1. RUN the batch file with full path 1.使用完整路径运行批处理文件

The easiest solution is running the batch file with full path.最简单的解决方案是使用完整路径运行批处理文件。

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

Once end of batch file Template.bat is reached, there is no return to previous script in case of the command line above is within a *.bat or *.cmd file.一旦到达批处理文件Template.bat的结尾,如果上面的命令行位于 *.bat 或 *.cmd 文件中,则不会返回到先前的脚本。

The current directory for the batch file Template.bat is the current directory of the current process.批处理文件Template.bat的当前目录是当前进程的当前目录。 In case of Template.bat requires that the directory of this batch file is the current directory, the batch file Template.bat should contain after @echo off as second line the following command line:如果Template.bat要求此批处理文件的目录为当前目录,则批处理文件Template.bat应在@echo off之后包含以下命令行:

cd /D "%~dp0"

Run in a command prompt window cd /?在命令提示符窗口中运行cd /? for getting displayed the help of this command explaining parameter /D ... change to specified directory also on a different drive.为了显示此命令的帮助,解释参数/D ... 也更改到不同驱动器上的指定目录。

Run in a command prompt window call /?在命令提示符窗口中运行call /? for getting displayed the help of this command used also in 2., 4. and 5. solution and explaining also %~dp0 ... drive and path of argument 0 which is the name of the batch file.为了显示此命令的帮助,也在 2.、4. 和 5. 解决方案中使用,并解释%~dp0 ... 驱动器和参数 0 的路径,这是批处理文件的名称。

2. CALL the batch file with full path 2.调用带有完整路径的批处理文件

Another solution is calling the batch file with full path.另一种解决方案是使用完整路径调用批处理文件。

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

The difference to first solution is that after end of batch file Template.bat is reached the batch processing continues in batch script containing this command line.与第一个解决方案的不同之处在于,在到达批处理文件Template.bat的末尾后,批处理将在包含此命令行的批处理脚本中继续进行。

For the current directory read above.对于上面读取的当前目​​录。

3. Change directory and RUN batch file with one command line 3. 一个命令行切换目录和运行批处理文件

There are 3 operators for running multiple commands on one command line: & , && and ||在一个命令行上运行多个命令有 3 个运算符: &&&|| . .
For details see answer on Single line with multiple commands using Windows batch file有关详细信息,请参阅使用 Windows 批处理文件执行多条命令的单行答案

I suggest for this task the && operator.我建议使用&&运算符来完成这项任务。

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

As on first solution there is no return to current script if this is a *.bat or *.cmd file and changing the directory and continuation of batch processing on Template.bat is successful.与第一个解决方案一样,如果这是 *.bat 或 *.cmd 文件并且更改目录并继续对Template.bat进行批处理成功,则不会返回当前脚本。

4. Change directory and CALL batch file with one command line 4. 一个命令行切换目录和调用批处理文件

This command line changes the directory and on success calls the batch file.此命令行更改目录并成功调用批处理文件。

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

The difference to third solution is the return to current batch script on exiting processing of Template.bat .第三种解决方案的不同之处在于在Template.bat退出处理时返回到当前批处理脚本。

5. Change directory and CALL batch file with keeping current environment with one command line 5. 使用一个命令行保持当前环境更改目录和调用批处理文件

The four solutions above change the current directory and it is unknown what Template.bat does regarding上面的四个解决方案改变了当前目录,不知道Template.bat做了什么

  1. current directory当前目录
  2. environment variables环境变量
  3. command extensions state命令扩展状态
  4. delayed expansion state延迟扩展状态

In case of it is important to keep the environment of current *.bat or *.cmd script unmodified by whatever Template.bat changes on environment for itself, it is advisable to use setlocal and endlocal .如果保持当前 *.bat 或 *.cmd 脚本的环境不受Template.bat对自身环境的任何更改的影响,建议使用setlocalendlocal

Run in a command prompt window setlocal /?在命令提示符窗口中运行setlocal /? and endlocal /?endlocal /? for getting displayed the help of these two commands.用于显示这两个命令的帮助。 And read answer on change directory command cd ..not working in batch file after npm install explaining more detailed what these two commands do.在 npm install 之后阅读更改目录命令 cd ..not working in batch file 的答案,更详细地解释这两个命令的作用。

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

Now there is only & instead of && used as it is important here that after setlocal is executed the command endlocal is finally also executed.现在只有&而不是&&使用,因为这里重要的是,在执行setlocal之后,命令endlocal最终也会执行。


ONE MORE NOTE再一注

If batch file Template.bat contains the command exit without parameter /B and this command is really executed, the command process is always exited independent on calling hierarchy.如果批处理文件Template.bat包含不带参数/B的命令exit并且该命令被真正执行,则命令进程总是退出独立于调用层次结构。 So make sure Template.bat contains exit /B or goto :EOF instead of just exit if there is exit used at all in this batch file.因此,请确保Template.bat包含exit /Bgoto :EOF而不是仅在此批处理文件中使用了exit时才exit

You can use Cmd command to run Batch file.您可以使用 Cmd 命令运行批处理文件。

Here is my way =>这是我的方式=>

cmd /c ""Full_Path_Of_Batch_Here.cmd" "

More information => cmd /?更多信息 => cmd /?

Like Linux, to run the myapp.exe , you can use only one of these three methods.与 Linux 一样,要运行myapp.exe ,您只能使用这三种方法中的一种。

use system path使用系统路径

add project directory to your systeme path , then:project目录添加到您的systeme path ,然后:

myapp.exe

or或者

myapp

use full long path使用完整的长路径

\path\to\project\myapp.exe

go to working directory进入工作目录

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM