回聲建立檔案

使用 echo 命令建立檔案的方法:

ECHO. > example.bat (creates an empty file called "example.bat")

ECHO message > example.bat (creates example.bat containing "message")
ECHO message >> example.bat (adds "message" to a new line in example.bat)
(ECHO message) >> example.bat (same as above, just another way to write it)

如果要通過 ECHO 命令建立檔案,則在計算機的特定目錄中可能會遇到問題。

ECHO Hello how are you? > C:\Program Files\example.bat

(This will NOT make a file in the folder "Program Files", and might show an error message)

但那我們該怎麼做呢?嗯,它實際上非常簡單…當鍵入一個包含在其名稱中的空格的路徑或檔名時,請記住使用引號

ECHO Hello how are you? > "C:\Program Files\example.bat"
(This will create "example.bat" in the folder "Program Files")

但是如果你想建立一個輸出新檔案的檔案呢?

ECHO message > file1.bat > example.bat

(example.bat is NOT going to contain "message > file1.bat")
example.bat will just contain "message"... nothing else

那我們怎麼做呢?好吧,我們使用^符號。

ECHO message ^> file1.bat > example.bat

(example.bat is going to contain "message > file1.bat")

批次中的其他東西也是如此

下一步需要你對變數和語句有一些瞭解:

點選這裡瞭解變數 | 點選這裡瞭解 if 和 else 語句

變數:

SET example="text"
ECHO %example% > file.bat
(This will output "text" to file.bat)

如果我們不希望它輸出 text 但只是簡單的%example%然後寫:

ECHO ^%example^% > file.bat
(This will output "%example%" to file.bat)

IF / ELSE 宣告:

ELSE statements are written with "pipes" ||

IF ^%example^%=="Hello" ECHO True || ECHO False > file.bat

(example.bat is going to contain "if %example%=="Hello" echo True")
[it ignores everything after the ELSE statement]

輸出整行然後我們像以前一樣。

IF ^%example^%=="Hello" ECHO True ^|^| ECHO False > file.bat

This will output:
IF %example%=="Hello" ECHO True || ECHO False

如果變數等於 Hello 那麼它會說 True,ELSE 會說 False