想要从文本文件中获取变量的值,将其与其他变量连接并将新值插入新文本文件中

时间:2017-05-31 13:46:59

标签: batch-file cmd

我正在做像

这样的代码
echo on
SET /P CONTROLSTRING_0326 = < sample.txt
rem sample.txt has data as 00000000000000000000000000000000000002017060000000000000000

ECHO %CONTROLSTRING_0326%
pause
SET CONTROL_DATE = %CONTROLSTRING_0326:~39,6%
ECHO %CONTROL_DATE%
PAUSE
SET filler1="2226 0000000 00000000000 00000000000 
SET filler2= CONTROL 06.30"
PAUSE
SET "FINAL_CONTROL_2460 = %filler1%%CONTROL_DATE%%filler2%"
SET FINAL_CONTROL_2460
pause
SET FINAL_CONTROL_2460 > new_control.txt
PAUSE
EXIT

我希望文件new_control.txt应该包含以下数据

2226 0000000 00000000000 00000000000 201706 CONTROL 06.30"

但是我在new_control.txt中获取数据为“echo is on”。 不确定出了什么问题?

1 个答案:

答案 0 :(得分:0)

问题是在SET命令中=之前不应该有空格字符。你创建的是一个名为&#34; CONTROL_DATE&#34;的变量。而不是&#34; CONTROL_DATE&#34;。

更改

SET /P CONTROLSTRING_0326 = < sample.txt
SET CONTROL_DATE = %CONTROLSTRING_0326:~39,6%
SET "FINAL_CONTROL_2460 = %filler1%%CONTROL_DATE%%filler2%"

SET /P "CONTROLSTRING_0326=" < sample.txt
SET "CONTROL_DATE=%CONTROLSTRING_0326:~39,6%"
SET "FINAL_CONTROL_2460=%filler1%%CONTROL_DATE%%filler2%"