2013-09-30 62 views
0

在我的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 

我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" 
+0

日期操作是一項艱鉅的任務。我建議你[標籤:VBScript]。 –

+0

@LS_dev我設法得到結果,請檢查一次。 – debal

回答

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" 
+0

+1好的解決方案。注意''usebackq「'在這裏不需要。 –

+0

JFTR如果''usebackq''被移除,那麼需要正常的ticks來替換back tick。 – foxidrive

+0

@LS_dev:'IN'子句中的命令不能用單引號分隔,因爲它將它們作爲命令行的一部分。所以,你需要使用不同的分隔符,因此''usebackq'''。 –

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 

查看輸出和除去如果看起來不錯,可以在ren之前使用echo。在批量

©Dostips

相關問題