我是Windows批处理的新手 我想编写批处理来生成一系列日志文件。 抱歉,我的英语很差。 让我试着解释一下我想要的东西。
我有5个变量。
我将所有变量存储到文本文件中。
Period.txt
----------
15min 30min
Symbol.txt
----------
AAA BBB CCC
StartYear.txt
-------------
2012 2013 2014 2015 2016
EndYear.txt (Actually the End Year is Start Year +1 )
-----------------------------------------------------
2013 2014 2015 2016 2017
Setting.txt
-----------
Setting1 Setting2 Setting3 Setting4 Setting5
我想生成一个XXX.log文件列表
名为Setting_StartYear_EndYear_Max_period_Symbol.log
例如:
Setting1_2012_2013_Max__15min_AAA.log Setting1_2012_2013_Max__15min_BBB.log Setting1_2012_2013_Max__15min_CCC.log Setting1_2012_2013_Max__30min_AAA.log Setting1_2012_2013_Max__30min_BBB.log Setting1_2012_2013_Max__30min_CCC.log
; Tested settings Report
Tested Period=15min
Tested Symbol=AAA
Tested Start Year=2012
Tested End Year=2013
Tested Setting=Setting2
; End settings Report
我写了一个名为 gen.bat
的批处理文件@echo off
SET output_filename=%%E_%%C_%%D_Max_%Symbol%_%%A_%%B
SET BaseDir=%cd%
For /f "delims=" %%E in (%BaseDir%\Setting.txt) do (
For /f "delims=" %%D in (%BaseDir%\EndYear.txt) do (
For /f "delims=" %%C in (%BaseDir%\StartYear.txt) do (
For /f "delims=" %%B in (%BaseDir%\Symbol.txt) do (
For /f "delims=" %%A in (%BaseDir%\Period.txt) do (
REM --------GEN Then log file-----------
Echo.; Tested settings Report>%output_filename%.log
Echo.Tested Period=%%A>>%output_filename%.log
Echo.Tested Symbol=%%B>>%output_filename%.log
Echo.Tested Start Year=%%C>>%output_filename%.log
Echo.Tested End Year=%%D>>%output_filename%.log
Echo.Tested Setting=%%E>>%output_filename%.log
Echo.; End settings Report>>%output_filename%.log
)))))
结果是,它生成了大量带有错误开始和结束年份的文件
Setting1_2012_2012_Max__15min_AAA.log << Wrong
Setting1_2012_2012_Max__15min_BBB.log << Wrong
Setting1_2012_2012_Max__15min_CCC.log << Wrong
Setting1_2012_2012_Max__30min_AAA.log << Wrong
Setting1_2012_2012_Max__30min_BBB.log << Wrong
Setting1_2012_2012_Max__30min_CCC.log << Wrong
Setting1_2012_2013_Max__15min_AAA.log << Correct (as end year must be next year of start year )
Setting1_2012_2013_Max__15min_BBB.log << Correct (as end year must be next year of start year )
Setting1_2012_2013_Max__15min_CCC.log << Correct (as end year must be next year of start year )
Setting1_2012_2013_Max__30min_AAA.log << Correct (as end year must be next year of start year )
Setting1_2012_2013_Max__30min_BBB.log << Correct (as end year must be next year of start year )
Setting1_2012_2013_Max__30min_CCC.log << Correct (as end year must be next year of start year )
Setting1_2012_2014_Max__15min_AAA.log << Wrong
Setting1_2012_2014_Max__15min_BBB.log << Wrong
Setting1_2012_2014_Max__15min_CCC.log << Wrong
Setting1_2012_2014_Max__30min_AAA.log << Wrong
有人能帮我调整脚本吗?
我曾经尝试了几天,但不知道如何更正批次。
答案 0 :(得分:0)
以下是使用Start Year + 1
set /a
的示例
另请参阅以下示例中的setlocal delayedexpansion如何使用!
代替%
获取更多信息,来自cmdline run setlocal /?
@echo off
setlocal enabledelayedexpansion
For /f "delims=" %%C in (%BaseDir%\StartYear.txt) do (
set /a Sdate=%%C
for %%i in (!Sdate!) do set /a Edate=%%i+1
echo !Edate!
)
使用 StartYear.txt 中的示例会为每个示例添加+1:
2012 --> 2013
2013 --> 2014
2014 --> 2015
2015 --> 2016
2016 --> 2017
..etc..