使用较旧的日期重命名文件

时间:2013-09-30 06:39:55

标签: batch-file rename

在我的Windows机器中,我正在努力实现以下目标。

目录结构:

C:.
│
└───Reports
    │   md.bat
    │
    ├───23-09-2013
    │       Size automation.xls
    │       Report 15_Sep_2013-21_Sep_2013.xls
    │
    └───Sample
            Size automation.xls
            Report.xls

my currrent md.bat文件:

for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a

mkdir %datestr%

copy Sample\"Size automation.xls" %datestr%

copy Sample\Report.xls %datestr%

这里首先我创建一个包含今天日期的目录,然后将示例文件复制到该目录。

我现在需要的是按以下格式重命名此目录中的文件(使用今天的日期创建的文件):

"Report <today-8days>-<today-2days>.xls"

2 个答案:

答案 0 :(得分:2)

我设法通过以下方式完成:

for /f "usebackq" %%x in (`powershell "(Get-Date).AddDays(-8).ToString('dd_MMM_yyyy')"`) do set sunday=%%x

for /f "usebackq" %%x in (`powershell "(Get-Date).AddDays(-2).ToString('dd_MMM_yyyy')"`) do set saturday=%%x

cd %datestr%

rename Report.xls "Report %sunday%-%saturday%.xls"

答案 1 :(得分:1)

批量解决方案:

@echo off &setlocal
set "StartFolder=C:\Reports"
call:GetInternational
cd /d "%StartFolder%"
for /f "delims=" %%a in ('dir /b /ad ^| findstr "^[0-3][0-9]-[0-1][[0-9]-201[0-9]$"') do set "OldFolder=%%~a"
REM get the seconds from the folders date
call:GetSecs "%OldFolder%" "12:00:00" OldFolderSecs
REM 8 days = 691200 sec
set /a StartSecs=OldFolderSecs-691200
call:GetTMS "%StartSecs%" yyyy mm dd
set "StartTMS=%dd%-%mm%-%yyyy%"
REM 2 days = 172800 sec
set /a EndSecs=OldFolderSecs-172800
call:GetTMS "%EndSecs%" yyyy mm dd
set "EndTMS=%dd%-%mm%-%yyyy%"
for %%a in ("%OldFolder%\*.xls") do echo ren "%%~a" "%%~na %StartTMS%-%EndTMS%%%~xa"
goto:eof

:GetInternational
:: Sets a bundle of variables by reading the registry settings
for /f "tokens=1,2*" %%a in ('reg query "HKCU\Control Panel\International"') do set "%%a=%%c"
goto :eof

:GetSecs "dateIn" "timeIn" secondsOut
::  Output:  Seconds elapsed since 1th Jan. 1970 00:00:00
setlocal
set "dateIn=%~1"
for /f "tokens=2" %%i in ("%dateIn%") do set "dateIn=%%i"
for /f "tokens=1-3 delims=%sDate%" %%a in ("%dateIn%") do (
  if %iDate%==0 set /a mm=100%%a%%100,dd=100%%b%%100,yy=10000%%c%%10000
  if %iDate%==1 set /a dd=100%%a%%100,mm=100%%b%%100,yy=10000%%c%%10000
  if %iDate%==2 set /a yy=10000%%a%%10000,mm=100%%b%%100,dd=100%%c%%100
)
for /f "tokens=1-3 delims=%sTime%%sDecimal% " %%a in ("%~2") do (
  set "hh=%%a"
  set "nn=%%b"
  set "ss=%%c"
)
if 1%hh% lss 20 set hh=0%hh%
if "%nn:~2,1%" equ "p" if "%hh%" neq "12" (set "hh=1%hh%" &set /a hh-=88)
if "%nn:~2,1%" equ "a" if "%hh%" equ "12" set "hh=00"
if "%nn:~2,1%" geq "a" set "nn=%nn:~0,2%"
set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2,j=j/5+dd+y*365+y/4-y/100+y/400-2472633,j=j*86400+hh*3600+nn*60+ss
endlocal &set "%~3=%j%"
goto :eof

:GetTMS
setlocal ENABLEEXTENSIONS
set /a i=%1,ss=i%%60,i/=60,nn=i%%60,i/=60,hh=i%%24,dd=i/24,i/=24
set /a a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
(if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
(if %hh% LSS 10 set hh=0%hh%)&(if %nn% LSS 10 set nn=0%nn%)
if %ss% LSS 10 set ss=0%ss%
endlocal&set %4=%dd%&set %3=%mm%&set %2=%yy%&goto :EOF

如果看起来不错,请查看输出并在echo之前删除ren

<子> ©Dostips