【问题标题】:When appending to a file using Windows batch commands, how to append immediately after the next word in the file?使用 Windows 批处理命令附加到文件时,如何在文件中的下一个单词之后立即附加?
【发布时间】:2011-02-02 02:50:27
【问题描述】:

使用 Windows 批处理命令附加到文件时,如何在文件中的下一个单词之后立即附加?

例如,这些命令

echo here is the date of > c:\arun.txt
date /t >> c:\arun.txt 

将以下文本写入 arun.txt 文件:

这里是日期
29-03-2010

但我希望输出是这样的:

这里是 29-03-2010 的日期

追加时如何避免回车?

【问题讨论】:

    标签: windows command-line scripting batch-file


    【解决方案1】:

    echo 输出始终包含一个尾随新行。要输出没有尾随换行的文本,您可以使用herehere 中描述的set /p 技巧:

    < nul (set /p s=Today is ) > c:\arun.txt
    date /t >> c:\arun.txt
    

    但在这种特定情况下,您可以简单地使用%date% 变量而不是date /t,因为%date% 使用相同的格式:

    echo Today is %date% > c:\arun.txt
    

    【讨论】:

    • %date% 没有环境变量。它只是一个由 shell 扩展为当前日期的伪变量。
    • 谢谢你。你知道如何在文本文件中添加一个空行吗?
    • @Arunachalam:试试echo.&gt;&gt;file.txt
    【解决方案2】:

    您可以存储到变量并附加

    C:\test>set s=here is today's date
    C:\test>for /F "tokens=*" %i in ('date /t') do set d=%i    
    C:\test>set d=Tue 03/30/2010    
    C:\test>echo %d%%s%
    Tue 03/30/2010 here is today's date    
    

    【讨论】:

      猜你喜欢
      • 2021-11-24
      • 2015-08-09
      • 2021-10-31
      • 2019-08-24
      • 2010-10-26
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多