I need to make a script that can write one line of text to a text file in the same directory as the batch file.
我需要创建一个脚本,可以将一行文本写入与批处理文件相同的目录中的文本文件。
7 个解决方案
#1
210
You can use echo
, and redirect the output to a text file (see notes below):
您可以使用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
Output:
输出:
D:\Temp>WriteText D:\Temp>type test.txt This is a test 123 245.67 D:\Temp>
Notes:
笔记:
-
@echo off
turns off printing of each command to the console - @echo off关闭每个命令到控制台的打印
-
@
at the beginning of the remaining lines stops printing of theecho
command itself, but does not suppressecho
output. (It allows the rest of the line after@echo
to display. - @在其余行的开头停止打印echo命令本身,但不抑制回声输出。 (它允许在@echo之后显示其余部分。
- Unless you give it a specific path name, redirection with
>
or>>
will write to the current directory (the directory the code is being run in). - 除非您为其指定特定的路径名,否则使用>或>>重定向将写入当前目录(运行代码的目录)。
- The
@echo This is a test > test.txt
uses one>
to overwrite any file that already exists with new content. - @echo这是一个测试> test.txt使用一个>来覆盖任何已存在的新内容文件。
- The remaining
@echo
statements use two>>
characters to append to the text file (add to), instead of overwriting it. - 剩下的@echo语句使用两个>>字符附加到文本文件(添加到),而不是覆盖它。
- The
type test.txt
simply types the file output to the command window. - 类型test.txt只是将文件输出键入命令窗口。
#2
77
It's easier to use only one code block, then you only need one redirection.
只使用一个代码块更容易,那么您只需要一个重定向。
(
echo Line1
echo Line2
...
echo Last Line
) > filename.txt
#3
12
echo "blahblah"> txt.txt
will erase the txt and put blahblah in it's place
echo“blahblah”> txt.txt将删除txt并将blahblah放入其中
echo "blahblah">> txt.txt
will write blahblah on a new line in the txt
echo“blahblah”>> txt.txt将把blahblah写在txt的新行上
I think that both will create a new txt if none exists (I know that the first one does)
我认为如果不存在,两者都会创建一个新的txt(我知道第一个会这样做)
Where "txt.txt
" is written above, a file path can be inserted if wanted. e.g. C:\Users\<username>\desktop
, which will put it on their desktop.
在上面写入“txt.txt”的地方,如果需要,可以插入文件路径。例如C:\ Users \ <用户名> \桌面,它将把它放在桌面上。
#4
9
@echo off
(echo this is in the first line) > xy.txt
(echo this is in the second line) >> xy.txt
exit
The two >>
means that the second line will be appended to the file (i.e. second line will start after the last line of xy.txt).
这两个>>表示第二行将附加到文件中(即第二行将在xy.txt的最后一行之后开始)。
this is how the xy.txt
looks like:
这就是xy.txt的样子:
this is in the first line
this is in the second line
#5
3
@echo off Title Writing using Batch Files color 0a
@echo off使用批处理文件的标题写作颜色0a
echo Example Text > Filename.txt echo Additional Text >> Filename.txt
echo示例文本> Filename.txt echo其他文本>> Filename.txt
@ECHO OFF
Title Writing Using Batch Files
color 0a
echo Example Text > Filename.txt
echo Additional Text >> Filename.txt
#6
1
- You can use
copy con
to write a long text - 您可以使用复制con来编写长文本
-
Example:
例:
C:\COPY CON [drive:][path][File name]
C:\ COPY CON [drive:] [path] [文件名]
.... Content
....内容
F6
F6
1 file(s) is copied
1个文件被复制
#7
1
@echo off
echo Type your text here.
:top
set /p boompanes=
pause
echo %boompanes%> practice.txt
hope this helps. you should change the string names(IDK what its called) and the file name
希望这可以帮助。你应该改变字符串名称(IDK所谓的)和文件名
#1
210
You can use echo
, and redirect the output to a text file (see notes below):
您可以使用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
Output:
输出:
D:\Temp>WriteText D:\Temp>type test.txt This is a test 123 245.67 D:\Temp>
Notes:
笔记:
-
@echo off
turns off printing of each command to the console - @echo off关闭每个命令到控制台的打印
-
@
at the beginning of the remaining lines stops printing of theecho
command itself, but does not suppressecho
output. (It allows the rest of the line after@echo
to display. - @在其余行的开头停止打印echo命令本身,但不抑制回声输出。 (它允许在@echo之后显示其余部分。
- Unless you give it a specific path name, redirection with
>
or>>
will write to the current directory (the directory the code is being run in). - 除非您为其指定特定的路径名,否则使用>或>>重定向将写入当前目录(运行代码的目录)。
- The
@echo This is a test > test.txt
uses one>
to overwrite any file that already exists with new content. - @echo这是一个测试> test.txt使用一个>来覆盖任何已存在的新内容文件。
- The remaining
@echo
statements use two>>
characters to append to the text file (add to), instead of overwriting it. - 剩下的@echo语句使用两个>>字符附加到文本文件(添加到),而不是覆盖它。
- The
type test.txt
simply types the file output to the command window. - 类型test.txt只是将文件输出键入命令窗口。
#2
77
It's easier to use only one code block, then you only need one redirection.
只使用一个代码块更容易,那么您只需要一个重定向。
(
echo Line1
echo Line2
...
echo Last Line
) > filename.txt
#3
12
echo "blahblah"> txt.txt
will erase the txt and put blahblah in it's place
echo“blahblah”> txt.txt将删除txt并将blahblah放入其中
echo "blahblah">> txt.txt
will write blahblah on a new line in the txt
echo“blahblah”>> txt.txt将把blahblah写在txt的新行上
I think that both will create a new txt if none exists (I know that the first one does)
我认为如果不存在,两者都会创建一个新的txt(我知道第一个会这样做)
Where "txt.txt
" is written above, a file path can be inserted if wanted. e.g. C:\Users\<username>\desktop
, which will put it on their desktop.
在上面写入“txt.txt”的地方,如果需要,可以插入文件路径。例如C:\ Users \ <用户名> \桌面,它将把它放在桌面上。
#4
9
@echo off
(echo this is in the first line) > xy.txt
(echo this is in the second line) >> xy.txt
exit
The two >>
means that the second line will be appended to the file (i.e. second line will start after the last line of xy.txt).
这两个>>表示第二行将附加到文件中(即第二行将在xy.txt的最后一行之后开始)。
this is how the xy.txt
looks like:
这就是xy.txt的样子:
this is in the first line
this is in the second line
#5
3
@echo off Title Writing using Batch Files color 0a
@echo off使用批处理文件的标题写作颜色0a
echo Example Text > Filename.txt echo Additional Text >> Filename.txt
echo示例文本> Filename.txt echo其他文本>> Filename.txt
@ECHO OFF
Title Writing Using Batch Files
color 0a
echo Example Text > Filename.txt
echo Additional Text >> Filename.txt
#6
1
- You can use
copy con
to write a long text - 您可以使用复制con来编写长文本
-
Example:
例:
C:\COPY CON [drive:][path][File name]
C:\ COPY CON [drive:] [path] [文件名]
.... Content
....内容
F6
F6
1 file(s) is copied
1个文件被复制
#7
1
@echo off
echo Type your text here.
:top
set /p boompanes=
pause
echo %boompanes%> practice.txt
hope this helps. you should change the string names(IDK what its called) and the file name
希望这可以帮助。你应该改变字符串名称(IDK所谓的)和文件名