回声创建文件

使用 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