我如何使用批处理文件写入到一个文本文件? [英] How can I use a batch file to write to a text file?
本文介绍了我如何使用批处理文件写入到一个文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个脚本,可以写一行文本到文本文件在同一目录中的批处理文件。
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.
推荐答案
您可以使用的 回声
,并将输出重定向到一个文本文件(参见下面的注释):
You can use echo
, and redirect the output to a text file (see notes below):
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
输出:
D:\Temp>WriteText
D:\Temp>type test.txt
This is a test
123
245.67
D:\Temp>
注:
-
关闭@echo
关闭每个命令打印到控制台 -
@
在剩余行的开头停止回声
命令本身的印刷,但不燮preSS回声
输出。 (它允许线后剩下的@echo
显示。 - 除非你给它一个特定的路径名,重定向与
>
或>>
将写入当前目录(code正在运行的目录)。 - 的
@echo这是一个试验> test.txt的
使用一个方式>
覆盖已经用新的内容存在任何文件 - 剩下的
@echo
语句中使用两个>>
字符追加到文本文件(添加到),而不是覆盖它。 - 的
键入的test.txt
简单类型的文件输出到命令窗口。
@echo off
turns off printing of each command to the console@
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.- 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. - The remaining
@echo
statements use two>>
characters to append to the text file (add to), instead of overwriting it. - The
type test.txt
simply types the file output to the command window.
这篇关于我如何使用批处理文件写入到一个文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文