Ken*_*ite 267
您可以使用echo,并将输出重定向到文本文件(请参阅下面的注释):
rem Saved in D:\Temp\WriteText.bat
@echo off
@echo This is a test> test.txt
@echo 123>> test.txt
@echo 245.67>> test.txt
Run Code Online (Sandbox Code Playgroud)
输出:
D:\Temp>WriteText D:\Temp>type test.txt This is a test 123 245.67 D:\Temp>
笔记:
@echo off 关闭每个命令到控制台的打印@在剩余行的开头停止打印echo命令本身,但不抑制echo输出.(它允许@echo显示后的其余部分.>或>>将写入当前目录(运行代码的目录).@echo This is a test > test.txt使用一个>覆盖已经有新的内容存在的任何文件.@echo语句使用两个>>字符附加到文本文件(添加到),而不是覆盖它.type test.txt简单类型的文件输出到命令窗口.jeb*_*jeb 99
只使用一个代码块更容易,那么您只需要一个重定向.
(
echo Line1
echo Line2
...
echo Last Line
) > filename.txt
Run Code Online (Sandbox Code Playgroud)
Dar*_*ter 16
echo "blahblah"> txt.txt 将抹掉txt并将blahblah放在它的位置
echo "blahblah">> txt.txt 将把blahblah写在txt的新行上
我认为如果不存在,两者都会创建一个新的txt(我知道第一个会这样做)
如果txt.txt上面写的是" ",则可以根据需要插入文件路径.例如C:\Users\<username>\desktop,它将把它放在他们的桌面上.
小智 11
@echo off
(echo this is in the first line) > xy.txt
(echo this is in the second line) >> xy.txt
exit
Run Code Online (Sandbox Code Playgroud)
这两个>>意味着第二行将附加到文件(即第二行将在xy.txt的最后一行之后开始).
这是xy.txt看起来像:
this is in the first line
this is in the second line
Run Code Online (Sandbox Code Playgroud)
小智 5
@echo off Title Writing 使用批处理文件颜色 0a
echo Example Text > Filename.txt echo Additional Text >> Filename.txt
@ECHO OFF
Title Writing Using Batch Files
color 0a
echo Example Text > Filename.txt
echo Additional Text >> Filename.txt
Run Code Online (Sandbox Code Playgroud)