Windows:在批处理文件中,将多行写入文本文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5181212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:07:51  来源:igfitidea点击:

Windows: in batch file, write multiple lines to text file?

windowscommand-linebatch-file

提问by user1686

How can I do the following in a Windows batch file?

如何在 Windows 批处理文件中执行以下操作?

  1. Write to a file called subdir/localsettings.py
  2. Overwrite all existing content...
  3. ...with multiple lines of text...
  4. ...including a string that is "[current working directory]/subdir" (which I think might be %cd%/subdir?)
  1. 写入名为 subdir/localsettings.py 的文件
  2. 覆盖所有现有内容...
  3. ...带有多行文本...
  4. ...包括一个字符串“[当前工作目录]/subdir”(我认为可能是%cd%/subdir?)

Please note, I want to do this as part of a batch script so I can't use con+ Enter (at least, maybe I can, but I don't know how to simulate Enter as part of a batch script).

请注意,我想将此作为批处理脚本的一部分来执行,因此我不能使用con+ Enter(至少,也许我可以,但我不知道如何将 Enter 作为批处理脚本的一部分进行模拟)。

Thanks!

谢谢!

回答by user1686

Use output redirection >and >>

使用输出重定向>>>

echo one>%file%
echo two>>%file%
echo three>>%file%

Or in a more readable way: (In cmd.exe, using "echo one >%file%" would include the whitespace before >.)

或者以更易读的方式:(在 中cmd.exe,使用“ echo one >%file%”将包括 之前的空格>。)

>%file%  echo one
>>%file% echo two
>>%file% echo three

You could also use:

您还可以使用:

(
    echo one
    echo two
    echo three
) >%file%

回答by access_granted

echo Line 1^

Line 2^

Line 3 >textfile.txt

Note the double new-lines to force the output:

请注意强制输出的双换行符:

Line1
Line2
Line3

Also:

还:

(echo Line 1^

Line 2^

Line 3)>textfile.txt