批处理文件中的文件名需要加上 _YYYY_MM 后缀
假设传入的报告日期的日期月份部分没有前导零。通常它作为 param1 传入,但下面我为此示例强制指定一个值。
我想构造一个字符串,其中从传入的日期中提取月份和年份,并构造一个格式为 YYYY_MM 的字符串,其中 MM 具有前导零。
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
问:我对批处理文件的了解有限。当日期月份长度为 1 位数时,我该怎么做才能在月份中强制添加前导零?
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 below 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 if 语句如果月份小于 10,则在前面添加 0:
Use an if-statement to prepend a 0 if the month is less than 10: