开发者

Need to on _YYYY_MM suffix to file name in a batch file

Assume that a reporting date is passed in that does not have leading zeros for the date month part. Normally it is passed in as param1 but b开发者_开发技巧elow I am forcing a value for this example.

I would like to construct a string where teh month and year are extracted from the date that is passed in and construct a string in the format YYYY_MM where MM has leading zeros.

REM SET ReportingDate=%1 
SET ReportingDate=7/31/2011

@For /F "tokens=1,2,3 delims=/ " %%A in ("%ReportingDate%") do @( 
Set Month=%%A
Set Day=%%B
Set Year=%%C
)

echo Year=%Year%
echo Month=%Month%

SET NewFileName=MEMBOB_%Year%_%Month%.csv

Q: My knowledge of batch files is limited. What do I have do do to force a leading zero in the montgh when the date month is 1 digit long?


Use an if-statement to prepend a 0 if the month is less than 10:

if %Month% LSS 10 Set Month=0%Month%
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜