windows - How to permanently delete specific file after specific number of days after running the script in a batch file -
i have little batch file script work on windows xp,7,8 , delete occurrences of specific file after amount of time after script executed.
i have tried similar:
forfiles -p "c:\what\ever" -s -m *somefile.pdf* /d -<number of days> /c "cmd /c del @path"
but isn't i'm looking deleting files older specific time. useful advice ?
there 3 ways solve problem:
keep batch file running time, delete file after desired time complete , restart process. method used in woxxom's answer.
use windows task scheduler execute "delete file" command every n days.
place batch file in startup folder run each time user log in. batch file check if file must deleted it, calculate date of next time , store in companion data file. method used in batch file below:
.
@echo off setlocal enabledelayedexpansion rem today's date arranged yyyymmdd rem (adjust lines below if mm/dd/yyyy not current locale date format) /f "tokens=1-3 delims=/" %%a in ("%date%") ( set "mm=%%a" & set "dd=%%b" & set "yyyy=%%c" ) set "today=%yyyy%%mm%%dd%" rem read next date companion file set /p nextdate=< nextdate.txt if %today% lss %nextdate% goto :eof rem delete file, calculate next date , store in companion file del "c:\what\ever\somefile.pdf" set days=5 set mon=0 %%a in (31 28 31 30 31 30 31 31 30 31 30 31) ( set /a mon+=1 set "dayspermonth[!mon!]=%%a" ) set /a ymod4=yyyy %% 4, mm=1%mm% - 100, dd=1%dd% - 100 + days if %ymod4% equ 0 set "dayspermonth[2]=29" if %dd% gtr !dayspermonth[%mm%]! set /a dd-=dayspermonth[%mm%], mm+=1 if %mm% gtr 12 set /a mm=1, yyyy+=1 if %mm% lss 10 set "mm=0%mm%" if %dd% lss 10 set "dd=0%dd%" echo %yyyy%%mm%%dd%> nextdate.txt note must manually create nextdate.txt file first date, although additional code may inserted in order manage initialization step.
Comments
Post a Comment