我想用以下格式命名一个文件:
2019-11年
2019年-12
2020年-01
基本上,使用批处理文件命名一个日期在1个月内增加的文件。
发布于 2019-11-12 15:19:18
既然我已经有了这段代码,我就把它放在这里,以防您可以根据需要修改它。它以书面形式依赖于语言(英语)。
:: get year, month, day for today
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
for /f "tokens=1-3 delims=/.- " %%A in ("%Date:* =%") do (
set %%a=%%A&set %%b=%%B&set %%c=%%C)
)
)
set /a "yy=10000%yy% %% 10000 %% 2000 + 2000,mm=100%mm% %% 100,dd=100%dd% %% 100"
:: yy is the 4 digit year presumed to be between year 2000 and year 2999; mm is the month; and dd is the day
:: Add 1 month
if /i %mm%==12 (
set mm=0
set /a yy+=1
)
set /a mm+=1
:: mm2 is zero-padded month
:: Pad
set mm2=0%mm%
set mm2=%mm2:~-2%发布于 2019-11-14 12:52:58
根据我关于使用更合适的脚本语言的建议,并基于您没有通过评论回答我的附加问题的事实,下面是一个powershell建议:
Get-ChildItem -Path "." -Filter "????-??" |
Where-Object {!$_.PSIsContainer -And $_.Name -Match "^[\d]{4}-[\d]{2}$"} |
Rename-Item -NewName {$(([DateTime]$_.Name).AddMonths(1)).ToString('yyyy-MM')}如果确实有扩展,那么需要调整-Filter字符串模式并合并.BaseName,以及.BaseName和.Extension,而不是使用.Name。
发布于 2019-11-14 19:37:25
使用wmic使用独立日期/时间布局的bat
@echo off && Setlocal EnableDelayedExpansion
for /f ^tokens^=1^delims^=. %%i in ('"wmic OS Get localdatetime|findstr [0-9]"')do set "_dt=%%i"
if "!_dt:~4,2!" == "12" (set "_m=101" && set /a "_y=!_dt:~0,4!+1") else (set /a "_m=!_dt:~4,2!+101"
set "_y=!_dt:~0,4!") && set "_dn=!_y!-!_m:~1!") && for /f "delims=" %%F in ('dir /s /b "*_"')do (
rename "%%~dpnF" "!_dn!") rename "2019_11" "2019-12"-
https://stackoverflow.com/questions/58808554
复制相似问题