0

Batch script to delete file older than 1 day.

D means: Run date am passing through command prompt.

How to subtract one day in D.

I want to delete one day old file,the file name starting with olddayfile_20140323.txt in same code.

Am using below code for Deleting D (D:current day file) files.

@echo on

rem %1 %2 %3 - YYYY MM DD

set folder1="C:\FX\in"

set folder2="C:\FX\out"

set D=%1%2%3%

cd /d %folder2%

del test1_%1%2%3*

del test2_%1%2%3*

del test3_%1%2%3*

Please note below script is working,but it's working only modified date files.

forfiles.exe /p C:\FX\out /s /m olddayfile_* /d -1 /c "cmd /c del @file"

Please suggest me am new for Batch script.

  • Many questions on this question. See [Batch file to delete files older than N days](http://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days?rq=1) – Laura Mar 24 '14 at 08:58
  • Is filename pattern the only criteria to determine file age? And, should the code delete only one day old files or anything older than that? – MC ND Mar 24 '14 at 09:03
  • possible duplicate of [Batch script to delete files older than X days (based on creation date, not modified date)](http://stackoverflow.com/questions/17785216/batch-script-to-delete-files-older-than-x-days-based-on-creation-date-not-modi) – Endoro Mar 24 '14 at 09:14
  • Do your files have YYYYMMDD in the filename, and you want to delete files one day old when using that date? – foxidrive Mar 24 '14 at 10:29
  • Are you assuming D as Current date or it as your manual input and whether file name is in the format of YYYYMMDD – SAT Mar 24 '14 at 11:11
  • @SAT D means: Run date am passing through command prompt.yes file format is in YYMMDD . ex: olddayfile_20140323.txt – user3243364 Mar 24 '14 at 11:17

1 Answers1

0

Try below code for delete the file with name of one day less than current date (format YYYYMMDD)

 @echo off
 setlocal

 Call :GetDateTime Year Month Day
 set A=%Year%%Month%%Day%

 Call :SubtractDate %Year% %Month% %Day% -1 Ret

 set b=%Ret%
 @echo ###start Deleting
 :: variables
 Set source=G:\


 echo ### Deleting up old backup...
 DEL /Q /S "%source%\%B%*.*"


 :SubtractDate Year Month Day <+/-Days> Ret
 ::Adapted from DosTips Functions::
 setlocal & set a=%4
 set "yy=%~1"&set "mm=%~2"&set "dd=%~3"
 set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
 if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
 set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
 if %a:~0,1% equ + (set /a JD=%JD%+%a:~1%) else set /a JD=%JD%-%a:~1%
 set /a L= %JD%+68569,     N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
 set /a L= L-1461*I/4+31, J= 80*L/2447,  K= L-2447*J/80,      L= J/11
 set /a J= J+2-12*L,      I= 100*(N-49)+I+L
 set /a YYYY= I, MM=100+J, DD=100+K
 set MM=%MM:~-2% & set DD=%DD:~-2%
 set ret=%YYYY: =%%MM: =%%DD: =%
 endlocal & set %~5=%ret%
 exit /b

 :GetDateTime Year Month Day Hour Minute Second
 @echo off & setlocal
 for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
 set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
 set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
 ( ENDLOCAL
    IF "%~1" NEQ "" set "%~1=%YYYY%" 
    IF "%~2" NEQ "" set "%~2=%MM%" 
    IF "%~3" NEQ "" set "%~3=%DD%"
    IF "%~4" NEQ "" set "%~4=%HH%" 
    IF "%~5" NEQ "" set "%~5=%Min%"
    IF "%~6" NEQ "" set "%~6=%Sec%"
 )
 exit /b
SAT
  • 647
  • 1
  • 12
  • 23