[英]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 腳本中。
我怎樣才能在一行中做到這一點?
"F:\- Big Packets -\kitterengine\Common\Template.bat"
可能以call
開頭(參見call /?
)。 或Cd /d "F:\- Big Packets -\kitterengine\Common\" & Template.bat
。
CMD 備忘單
命令文件
獲得幫助
標點
命名文件
啟動程序
鑰匙
命令文件
首先要記住它是一種操作計算機的方式。 這是我們在 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 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 的主要目的。
開始使用 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刪除前一個單詞
首頁行首
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
的末尾后,批處理將在包含此命令行的批處理腳本中繼續進行。
對於上面讀取的當前目錄。
在一個命令行上運行多個命令有 3 個運算符: &
、 &&
和||
.
有關詳細信息,請參閱使用 Windows 批處理文件執行多條命令的單行答案
我建議使用&&
運算符來完成這項任務。
cd /D "F:\- Big Packets -\kitterengine\Common" && Template.bat
與第一個解決方案一樣,如果這是 *.bat 或 *.cmd 文件並且更改目錄並繼續對Template.bat
進行批處理成功,則不會返回當前腳本。
此命令行更改目錄並成功調用批處理文件。
cd /D "F:\- Big Packets -\kitterengine\Common" && call Template.bat
第三種解決方案的不同之處在於在Template.bat
退出處理時返回到當前批處理腳本。
上面的四個解決方案改變了當前目錄,不知道Template.bat
做了什么
如果保持當前 *.bat 或 *.cmd 腳本的環境不受Template.bat
對自身環境的任何更改的影響,建議使用setlocal
和endlocal
。
在命令提示符窗口中運行setlocal /?
和endlocal /?
用於顯示這兩個命令的幫助。 並在 npm install 之后閱讀更改目錄命令 cd ..not working in batch file 的答案,更詳細地解釋這兩個命令的作用。
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 命令運行批處理文件。
這是我的方式=>
cmd /c ""Full_Path_Of_Batch_Here.cmd" "
更多信息 => cmd /?
與 Linux 一樣,要運行myapp.exe
,您只能使用這三種方法中的一種。
將project
目錄添加到您的systeme path
,然后:
myapp.exe
或者
myapp
\path\to\project\myapp.exe
cd \path\to\project
.\myapp.exe
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.