JSI Tip 8253. How can I delete all the files in a folder that have not be updated in xx days, and optionally archive them?

I have scripted DelNotUpd.bat to delete all the files in a folder, and optionally the sub-folders. The DelNotUpd.bat script also has an option to archive the files before deleting them.

The syntax for using DelNotUpd.bat is:

DelNotUpd Days Folder ReportFile \[/S\] \[/A:ArchiveFolder\]


Days is the number of days since the file has been updated, or created.
Folder is the folder path of the files.
ReportFile is a file that will contain the list of deleted files.
/S is an optional parameter which causes sub-folders to be processed.
/A:ArchiveFolder    is an optional parameter that specifies the archive folder path.

NOTE: DelNotUpd.bat uses UnivDate.bat, JSIDateM.bat, Date2JD.bat, and JD2Date.bat, which must be stored in a folder that is in your path.

NOTE: The script relies on the fact that a DIR /TW command returns MM/DD/YYYY as the first field.

DelNotUpd.bat contains:

@echo off
if \{%3\}

\{\} @echo Syntax: DelNotUpd Days Folder ReportFile \[/S\] \[/A:ArchiveFolder\]&goto :EOF if not exist %2 @echo Syntax: DelNotUpd - Folder %2 not found.&goto :EOF setlocal call UnivDate set /a days=%1 call JSIDateM %yy% %mm% %dd% - %days% set old=%aymd% set folder=%2# set folder=%folder:"=% set folder=%folder:\#=% set folder=%folder:#=% set report=%3 set work=N set arc=N if not \{%4\}

\{\} set work=%4 set work=%work:"=% if /i "%work:~0,2%" EQU "/S" set sub= /S if /i "%work:~0,3%" EQU "/A:" set arc=%work:~3% if not \{%5\}==\{\} set work=%5 set work=%work:"=% if /i "%work:~0,2%" EQU "/S" set sub= /S if /i "%work:~0,3%" EQU "/A:" set arc=%work:~3% If "%arc%" EQU "N" goto begin set arc=%arc%# set arc=%arc:\#=% set arc=%arc:#=% if exist "%arc%" goto begin @echo DelNotUpd - Archive "%arc%" not found. endlocal goto :EOF :begin if exist %report% del /q %report% pushd "%folder%" for /f "Tokens=*" %%f in ('dir "%folder%" /a /a-d /b%sub%') do ( set file=%%f call :test ) popd endlocal goto :EOF :test for /f "Tokens=1-3 Delims=/ " %%a in ('dir "%file%" /a /tw^|Findstr /c:"/"') do ( set m=%%a set d=%%b set y=%%c ) if "%y%%m%%d%" GEQ "%old%" goto :EOF @echo "%file%">>%report% if "%arc%" NEQ "N" call :cpy>nul 2>&1 del /q "%file%" goto :EOF :cpy set fld=%folder::=#% set new=%arc::=#% set work=%file::=#% call set work=%%work:%fld%=%new%%% set work=%work% set work=%work:#=:% if "%file%" EQU "%work%" set work=%arc%\%file% call :getpath "%work%" copy "%file%" "%work%" goto :EOF :getpath set new=%~dp1 if not exist "%new%" MD "%new%"

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.