1.

Solve : Batch file that deletes files after a specif time?

Answer»
Ok,..I've been lookin all AROUND for a tool or a script that deletes file after a specific time period.
Such as I want to delete all .wav's that are older that two weeks old, from the time the batch runs.

Any HELP or direction would be appreicated Batch could probably do this, but laziness wins out every time. This little snippet will age the files:

Code: [Select]Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("folderspec") <== Change
Set colSubFolders = f.SubFolders

For Each objFolder in colSubFolders
ShowFiles objFolder
Next

Sub ShowFiles(Fld)
Set k = fso.GetFolder(Fld)
Set s = k.SubFolders
Set kf = k.Files

For Each objFile In kf
If UCase(fso.GetExtensionName(objFile)) = "AVI" Then
If objFile.DateCreated < date - 14 Then
Wscript.Echo objFile & " " & objFile.DateCreated
End If
End If
Next

For Each SubFolder In s
ShowFiles SubFolder
Next
End Sub

The line marked CHANGE needs a quoted fully qualified foldername.

Save the script with a VBS extension and run from the command line as cscript scriptname.vbs.

As written, the script will list the files to be deleted. If you like what you see, replace Wscript.Echo objFile & " " & objFile.DateCreated with fso.DeleteFile objfile,True

Good luck. 8-)If you do want something in DOS, the following code should work:

Code: [Select]@echo off
setlocal

set DayCnt=14
set DelDir="*.wav"

REM ** Date format DD/MM/YYYY
for /F "tokens=2-4 delims=/ " %%f in ('date /t') do set dd=%%f&set mm=%%g&set yyyy=%%h

REM Substract your days here
set /A dd=1%dd% - 100 - %DayCnt%
set /A mm=1%mm% - 100

:CHKDAY
if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1

:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31
set /A dd=31 + %dd%
goto CHKDAY

:SET30
set /A dd=30 + %dd%
goto CHKDAY

:LEAPCHK
set /A TT=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29

:SET28
set /A dd=28 + %dd%
goto CHKDAY

:SET29
set /A dd=29 + %dd%
goto CHKDAY

:DONE

if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%
set _CutOff=%yyyy%/%mm%/%dd%

for /f "delims=" %%i in ('dir /b /a-d %DelDir%') do call :CHECK "%%i" %%~ti

goto :EOF

:CHECK
set _ChkDate=%2
set /A _Year=1%_ChkDate:~-2% - 100
if /I %_Year% LSS 40 (
set /A _Year+=2000
) else (
set /A _Year+=1900
)

set _ChkDate=%_Year%/%_ChkDate:~3,2%/%_ChkDate:~0,2%
if %_ChkDate% GTR %_CutOff% goto :EOF
echo del %1

Note that the script as-is will jsut tell you what files it is GOING to delete. You need to TAKE the "echo" off of the very last line in the script for it to actually delete the files.


Discussion

No Comment Found