Deleting the oldest folders in a directory
Posted: 17 May 2011 15:10
I have this bat file created, it is supposed to look at at directory and keep X amount of recent folders (It calculates it by date modified) and deletes the rest. Now I have been testing it out and it works great with empty folders. Once I have files in the folders it keeps giving me "the directory is not empty" and wont let me delete. I have been at it for a bit and could use some help. I need it to delete folders with anything in them. Feel free to try it with your own path. If you feel you have a better code feel free to post. Thanks in advance :
@Echo Off
:: User Variables
:: Set this to the number of folders you want to keep
Set _NumtoKeep=5
:: Set this to the folder that contains the folders to check and delete
Set _Path=C:\Test\databackup
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PushD %_Path%
Set _s=%_NumtoKeep%
If %_NumtoKeep%==1 set _s=single
Echo Please wait, searching for folders other than the %_s% most recent
For /F "tokens=* skip=%_NumtoKeep%" %%I In ('dir "%_Path%" /AD /B /O-D /TW') Do (
If Exist "%temp%\tf}1{" (
Echo %%I:%%~fI >>"%temp%\tf}1{"
) Else (
Echo.>"%temp%\tf}1{"
:: Echo Do you wish to delete the following folders?>>"%temp%\tf}1{"
Echo Date Name>>"%temp%\tf}1{"
Echo %%I:%%~fI >>"%temp%\tf}1{"
))
PopD
If Not Exist "%temp%\tf}1{" Echo No Folders Found to delete & Goto _Done
Type "%temp%\tf}1{" | More
Set _rdflag= /q
::If you want a prompt to come out
::_Prompt1
::Set /P _resp=Delete All, None, or Prompt for each (A/N/P)?
::If /I "%_resp:~0,1%"=="N" Goto _Done
::If /I "%_resp:~0,1%"=="A" Goto _Removeold
::If /I NOT "%_resp:~0,1%"=="P" (del
::Echo (A/N/P only please)&Goto _Prompt1
::Set _rdflag=
:_Removeold
For /F "tokens=1* skip=3 Delims=:" %%I In ('type "%temp%\tf}1{"') Do (
If "%_rdflag%"=="" echo Deleting
pause
rd /S /q%_rdflag% "%%J")
:_Done
If Exist "%temp%\tf}1{" del "%temp%\tf}1{"
pause
@Echo Off
:: User Variables
:: Set this to the number of folders you want to keep
Set _NumtoKeep=5
:: Set this to the folder that contains the folders to check and delete
Set _Path=C:\Test\databackup
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PushD %_Path%
Set _s=%_NumtoKeep%
If %_NumtoKeep%==1 set _s=single
Echo Please wait, searching for folders other than the %_s% most recent
For /F "tokens=* skip=%_NumtoKeep%" %%I In ('dir "%_Path%" /AD /B /O-D /TW') Do (
If Exist "%temp%\tf}1{" (
Echo %%I:%%~fI >>"%temp%\tf}1{"
) Else (
Echo.>"%temp%\tf}1{"
:: Echo Do you wish to delete the following folders?>>"%temp%\tf}1{"
Echo Date Name>>"%temp%\tf}1{"
Echo %%I:%%~fI >>"%temp%\tf}1{"
))
PopD
If Not Exist "%temp%\tf}1{" Echo No Folders Found to delete & Goto _Done
Type "%temp%\tf}1{" | More
Set _rdflag= /q
::If you want a prompt to come out
::_Prompt1
::Set /P _resp=Delete All, None, or Prompt for each (A/N/P)?
::If /I "%_resp:~0,1%"=="N" Goto _Done
::If /I "%_resp:~0,1%"=="A" Goto _Removeold
::If /I NOT "%_resp:~0,1%"=="P" (del
::Echo (A/N/P only please)&Goto _Prompt1
::Set _rdflag=
:_Removeold
For /F "tokens=1* skip=3 Delims=:" %%I In ('type "%temp%\tf}1{"') Do (
If "%_rdflag%"=="" echo Deleting
pause
rd /S /q%_rdflag% "%%J")
:_Done
If Exist "%temp%\tf}1{" del "%temp%\tf}1{"
pause