如何使用批处理文件写入文本文件? [英] How can I use a batch file to write to a text file?

查看:114
本文介绍了如何使用批处理文件写入文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个脚本,可以将一行文本写入与批处理文件位于同一目录中的文本文件.

解决方案

您可以使用 echo,并将输出重定向到一个文本文件(见下面的注释):

rem 保存在 D:TempWriteText.bat@回声关闭echo 这是一个测试>测试.txt回声 123>>测试.txt回声 245.67>>测试.txt

输出:

<前>D:Temp>WriteTextD:Temp> 输入 test.txt这是一个测验123245.67D:温度>

注意事项:

  • @echo off 关闭将每个命令打印到控制台
  • 除非你给它一个特定的路径名​​,否则使用 >>> 重定向将写入当前目录(运行代码的目录)).
  • echo 这是一个测试 >test.txt 使用一个 > 用新内容覆盖任何已经存在的文件.
  • 其余的 echo 语句使用两个 >> 字符附加到文本文件(添加到),而不是覆盖它.
  • type test.txt 只是将文件输出输入到命令窗口.

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:TempWriteText.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
  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆