【问题标题】:Writing to a file with Windows batch使用 Windows 批处理写入文件
【发布时间】:2025-12-31 09:50:12
【问题描述】:

我有一个这样的文件

Some text here, with a %var% .... and other text with the same %var%

基本上,我想创建另一个文件并将 %var% 替换为我选择的变量。我正在使用 Windows 批处理(.bat)。用户输入变量。

有什么想法吗?

【问题讨论】:

    标签: windows batch-file


    【解决方案1】:

    如果您的批处理文件中有一些变量,如下所示:

    set var=text
    set nextvar=moretext
    

    然后您可以将其导出到文件中,选择包含您想要的变量,如下所示:

    echo Some text here, with a %var% or %nextvar% >C:\file.txt
    

    【讨论】:

      【解决方案2】:

      完成巴厘岛的回答,要求用户输入变量的值..

      阅读HELP SET然后尝试

      SET /P var=Enter some text: 
      ECHO Some text here, with a %var% >out.txt
      ECHO ... and other text with the same %var% >>out.txt
      

      【讨论】: