.bat文件无法正常工作,希望保持领先零

时间:2016-09-07 14:28:18

标签: batch-file

我得到的数字无效。数字常量是十进制(17),十六进制(0x11)或八进制(021)。它也一直在重命名20161201_2012312016即使我现在正在运行它应该是20160801_20160831。我已粘贴代码。感谢

@echo off

set FirstDay=01

set /a Month=%date:~4,2%-1
set Month=0%Month%
set Month=%Month:~-2%

set Year=%date:~10,4%

if %Month%==00 (
  set Month=12
  set /a Year=%Year%-1
)

if %Month%==01 set "LastDay=31" & goto foundate
if %Month%==02 set "LastDay=28" & goto foundate
if %Month%==03 set "LastDay=31" & goto foundate
if %Month%==04 set "LastDay=30" & goto foundate
if %Month%==05 set "LastDay=31" & goto foundate
if %Month%==06 set "LastDay=30" & goto foundate
if %Month%==07 set "LastDay=31" & goto foundate
if %Month%==08 set "LastDay=31" & goto foundate
if %Month%==09 set "LastDay=30" & goto foundate
if %Month%==10 set "LastDay=31" & goto foundate
if %Month%==11 set "LastDay=30" & goto foundate
if %Month%==12 set "LastDay=31" & goto foundate



:foundate
echo The year is: %Year%
echo The month is: %Month%
echo First day of this month is: %FirstDay%
echo Last day of this month is: %LastDay%

set FileDir=Z:\"EpicCare Reports"\Reports_Team\Scheduled_Reports_Output \PreUHC\


set fileext=.txt
set Ifile=CPT_000318_
set ofile=%ifile%%Year%%Month%%FirstDay%_%Year%%Month%%Lastday%%fileext%

echo Output Filename: %ofile%
ren %FileDir%%ifile%.txt %ofile% 

  move /Y %FileDir%%ofile% Z:\"EpicCare Reports"\Reports_Team\Scheduled_Reports_Output\UHC\

1 个答案:

答案 0 :(得分:0)

问题在于前导零,一旦你尝试执行计算就会被解释:

set month=08
set /A month+=1

将给出您报告的确切错误,因为08被解释为八进制数,并且无效,因为8不能是八进制数字。

可以通过多种方式修复。我个人的:

set month=%month:08=8%
set month=%month:09=9%
set /A month+=1
set month=%month:8=08%
set month=%month:9=09%