如何使用cmd创建具有不同值的多个txt文件

如何使用cmd创建具有不同值的多个txt文件

我正在尝试创建多个 txt 文件并修改其中的一些行。

我使用下面的代码,但它不起作用。

@echo off
set line1=  ^<StartTime^>2022-11-19^</StartTime^>
set line2=  ^<Port^>8000^</Port^>
set line3=  ^<Port^>Stack Overflow^</Port^>

echo %line1% >setting.txt
echo %line2% >>setting.txt
echo %line3% >>setting.txt

set line1=%line1:2022-11-19=2022-11-18%
set line2=%line2:8000=8001%
set line3=%line2:Stack Overflow=Stack Exchange%

echo %line1% >setting1.txt
echo %line2% >>setting1.txt
echo %line3% >>setting1.txt

set line2=%line2:8001=8002%

echo %line1% >setting2.txt
echo %line2% >>setting2.txt
echo %line3% >>setting2.txt

pause

我期望此代码将在当前目录中创建 3 个 txt 文件。(setting.txt、setting1.txt、setting2.txt)

每个 txt 文件必须包含以下内容。

设置.txt

  ^<StartTime^>2022-11-19^</StartTime^>
  ^<Port^>8000^</Port^>
  ^<Port^>Stack Overflow^</Port^>

设置1.txt

  ^<StartTime^>2022-11-18^</StartTime^>
  ^<Port^>8001^</Port^>
  ^<Port^>Stack Exchange^</Port^>

设置2.txt

  ^<StartTime^>2022-11-18^</StartTime^>
  ^<Port^>8002^</Port^>
  ^<Port^>Stack Exchange^</Port^>

我该如何修复我的代码?

答案1

cmd脚本不起作用

有几件事需要解决

  1. 您的第一个set里面有多余的空格。

  2. 第二行3命令有错误set

修正后的脚本如下:

rem @echo off
set line1=^<StartTime^>2022-11-19^</StartTime^>
set line2=^<Port^>8000^</Port^>
set line3=^<Port^>Stack Overflow^</Port^>

echo %line1% >setting.txt
echo %line2% >>setting.txt
echo %line3% >>setting.txt

set line1=%line1:2022-11-19=2022-11-18%
set line2=%line2:8000=8001%
set line3=%line3:Stack Overflow=Stack Exchange%

echo %line1% >setting1.txt
echo %line2% >>setting1.txt
echo %line3% >>setting1.txt

set line2=%line2:8001=8002%

echo %line1% >setting2.txt
echo %line2% >>setting2.txt
echo %line3% >>setting2.txt

pause

相关内容