BJTurner
asked on
Batch process to delete folders older than 4 days
I've asked a similar question very recently but the requirements have changed. I need to delete any directory that is AT LEAST 4 days old, not just folders that are exactly 4 days old. After doing some research I've also discovered that I would need to empty the directory of all files before deleting it (unless there is some switch that I dont know about). I've looked at other questions on EE and my problem is very similar but with a difference, the date format for the folders is MM_DD_YYYY. I've seen other examples but can't seem to finagle my dates to fit into the format listed here. There is probably a simple answer related to the solutions I've gotten on previous questions but for some reason can't get my mind wrapped around it. Any help is much appreciated again!
Here is a DOS version, with the date required to be entered:
@echo off
set DeleteSource="C:\XCOPY_TES T"
:GETDELETESOURCE
cls
echo.
echo.
echo Enter the full path you want to delete files from.
echo Please place a quotation mark (") on both sides of the full path.
set /P DeleteSource=EG. "C:\Temp":
echo.
echo.
if not exist %DeleteSource% echo Cannot find %DeleteSource%
if not exist %DeleteSource% pause
if not exist %DeleteSource% goto GETDELETESOURCE
set TempCopyDest="C:\XCOPY_TEM P_DEST"
set CopyDate=""
echo Please enter the date to delete files BEFORE (not inclusive)
echo in the date format of MM-DD-YYYY.
echo.
set /P CopyDate= MM-DD-YYYY:
echo.
echo.
md %TempCopyDest%
XCOPY /D:%CopyDate% /C /E /L /H /R /Y %DeleteSource% %TempCopyDest% > Files_To_Keep.txt
If ErrorLevel 4 goto DATE_ERROR
XCOPY /EXCLUDE:Files_To_Keep.txt /E /L /Y %DeleteSource% %TempCopyDest% > Files_To_Delete.txt
echo Current path is %CD%
echo.
echo Obtaining list of files to delete from
echo %DeleteSource%
echo.
set /a FileCount=0
for /f "delims=| tokens=1" %%i in (Files_To_Keep.txt) do set /a FileCount+=1
set /a FileCount-=1
echo There are %FileCount% files to be kept.
set /a FileCount=0
for /f "delims=| tokens=1" %%i in (Files_To_Delete.txt) do set /a FileCount+=1
set /a FileCount-=1
echo There are %FileCount% files to be deleted.
echo.
echo.
choice /C:YN Are you sure you want to delete these files?
if ErrorLevel 9009 echo.
if ErrorLevel 9009 echo ERROR: You do not have choice.com on your system.
if ErrorLevel 9009 goto END
if ErrorLevel 2 goto NODELETE
if ErrorLevel 1 goto DELETE
:DELETE
echo.
echo Deleting files...
set Non_File=%FileCount% File(s)
for /f "delims=| tokens=1" %%i in (Files_To_Delete.txt) do if /I not "%%i"=="%Non_File%" attrib -s -h -r "%%i"
for /f "delims=| tokens=1" %%i in (Files_To_Delete.txt) do if /I not "%%i"=="%Non_File%" del "%%i"
echo.
echo Files deleted.
echo.
echo Attempting to remove empty directories...
set /a TempNum=0
:LOOP
dir /s /b /ad %DeleteSource% > DirectoryList.txt
dir /s /b /ad %DeleteSource% | Find /I /C "C:\" > DirCount.txt
for /f "delims=| tokens=1" %%i in (DirCount.txt) do set /a DirCount=%%i
for /f "delims=| tokens=1" %%i in (DirectoryList.txt) do rmdir "%%i"
set /a TempNum+=1
if %TempNum% LEQ %DirCount% goto LOOP
echo.
echo Finished attempts to remove directories.
goto END
:NODELETE
echo.
echo Files not deleted.
goto END
:DATE_ERROR
echo.
echo Error with date specified. No action taken.
goto END
:END
if exist Files_To_Keep.txt del Files_To_Keep.txt
if exist Files_To_Delete.txt del Files_To_Delete.txt
if exist DirectoryList.txt del DirectoryList.txt
if exist DirCount.txt del DirCount.txt
rmdir %TempCopyDest%
echo.
echo.
pause
@echo off
set DeleteSource="C:\XCOPY_TES
:GETDELETESOURCE
cls
echo.
echo.
echo Enter the full path you want to delete files from.
echo Please place a quotation mark (") on both sides of the full path.
set /P DeleteSource=EG. "C:\Temp":
echo.
echo.
if not exist %DeleteSource% echo Cannot find %DeleteSource%
if not exist %DeleteSource% pause
if not exist %DeleteSource% goto GETDELETESOURCE
set TempCopyDest="C:\XCOPY_TEM
set CopyDate=""
echo Please enter the date to delete files BEFORE (not inclusive)
echo in the date format of MM-DD-YYYY.
echo.
set /P CopyDate= MM-DD-YYYY:
echo.
echo.
md %TempCopyDest%
XCOPY /D:%CopyDate% /C /E /L /H /R /Y %DeleteSource% %TempCopyDest% > Files_To_Keep.txt
If ErrorLevel 4 goto DATE_ERROR
XCOPY /EXCLUDE:Files_To_Keep.txt
echo Current path is %CD%
echo.
echo Obtaining list of files to delete from
echo %DeleteSource%
echo.
set /a FileCount=0
for /f "delims=| tokens=1" %%i in (Files_To_Keep.txt) do set /a FileCount+=1
set /a FileCount-=1
echo There are %FileCount% files to be kept.
set /a FileCount=0
for /f "delims=| tokens=1" %%i in (Files_To_Delete.txt) do set /a FileCount+=1
set /a FileCount-=1
echo There are %FileCount% files to be deleted.
echo.
echo.
choice /C:YN Are you sure you want to delete these files?
if ErrorLevel 9009 echo.
if ErrorLevel 9009 echo ERROR: You do not have choice.com on your system.
if ErrorLevel 9009 goto END
if ErrorLevel 2 goto NODELETE
if ErrorLevel 1 goto DELETE
:DELETE
echo.
echo Deleting files...
set Non_File=%FileCount% File(s)
for /f "delims=| tokens=1" %%i in (Files_To_Delete.txt) do if /I not "%%i"=="%Non_File%" attrib -s -h -r "%%i"
for /f "delims=| tokens=1" %%i in (Files_To_Delete.txt) do if /I not "%%i"=="%Non_File%" del "%%i"
echo.
echo Files deleted.
echo.
echo Attempting to remove empty directories...
set /a TempNum=0
:LOOP
dir /s /b /ad %DeleteSource% > DirectoryList.txt
dir /s /b /ad %DeleteSource% | Find /I /C "C:\" > DirCount.txt
for /f "delims=| tokens=1" %%i in (DirCount.txt) do set /a DirCount=%%i
for /f "delims=| tokens=1" %%i in (DirectoryList.txt) do rmdir "%%i"
set /a TempNum+=1
if %TempNum% LEQ %DirCount% goto LOOP
echo.
echo Finished attempts to remove directories.
goto END
:NODELETE
echo.
echo Files not deleted.
goto END
:DATE_ERROR
echo.
echo Error with date specified. No action taken.
goto END
:END
if exist Files_To_Keep.txt del Files_To_Keep.txt
if exist Files_To_Delete.txt del Files_To_Delete.txt
if exist DirectoryList.txt del DirectoryList.txt
if exist DirCount.txt del DirCount.txt
rmdir %TempCopyDest%
echo.
echo.
pause
I have re-written the above script in VBScript as well, so that I can automatically pass the required date to it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the suggestions guys but Steve had the best code for me to use. Excellent work!
Glad it worked :)
thanks GOD you put the echo before RD command. I accidentally ran the script...twice :D
You can download it here:
http://www.utopiatemple.com/tutils/tdel268.zip
I have a script that I used for clearing out Exchange server protocol logs older than X days here:
https://www.experts-exchange.com/questions/22443498/Can-I-set-a-FIFO-size-limit-on-the-Exchange-IMF-folder-UCEArchive.html
You're welcome to use it as the basis of your batch script.