Welcome to TSG!
This will delete the previous months folders. It will exit without deleting if you run it before the date specified in the
Set /A _FirstDay= line.
(If allowed to run on the first of the month before the backup for the 1st is made, you would end up deleting all of the previous month's backups. If something happens before the backup for the first is made, you would have no backups at all).
It will list all the folders first, and you have the option of specifying to delete all, none, or be prompted for each.
Once you are happy with the way it works, you can eliminate the display of the list and the prompt by changing the
Set _Prompt= line to
False, and just have it delete all the folders automatically.
Code:
@Echo Off
:: Delete previous month's folders
:: Date determined by date contained in folder name
:: Written by TheOutcaste for http://forums.techguy.org
Setlocal EnableDelayedExpansion
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: User Variables
:: Set this next line to False to automatically delete all folders found
Set _Prompt=True
:: Set this to the earliest day of the month this is allowed to run
:: Do not include a leading zero
Set /A _FirstDay=10
:: Set this to the folder that contains the folders to check and delete
Set _Path=C:\Batch\ARCHIVE\FINAL
:: Set this to the base name of the folders to find. If this string is not in the folder name, the folder will be skipped
:: Set this to nothing to find all folders
Set _BaseName=\Back*
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
If %_FirstDay% LSS 2 Goto :EOF
If %_FirstDay% GTR 28 Goto :EOF
If %_FirstDay% LSS 10 Set _FirstDay=0%_FirstDay%
:: Get todays date
Call :GetDate
Set _yy=%_fDate:~,4%
Set _mm=%_fDate:~4,2%
Set _dd=%_fDate:~6,2%
If %_dd% LSS %_FirstDay% Echo Not allowed to run until %_mm%/%_FirstDay%/%_yy%&Goto :EOF
:: Convert first of months date to Julian
Call :JDate %_yy% %_mm% 01
Set _JToday=%_JDate%
:: Set delete date
Set /a _DelDate=_JToday-1
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PushD %_Path%
If /I %_Prompt%==True Echo Please wait, searching for folders from the previous month
For /F "Tokens=1-4 Delims=_" %%I In ('dir "%_Path%%_BaseName%" /AD /B /ON') Do (
Call :JDate %%J %%K %%L
If !_JDate! LEQ %_DelDate% (
If Exist "%temp%\tf}1{" (
Echo %%I_%%J_%%K_%%L>>"%temp%\tf}1{"
) Else (
Echo.>"%temp%\tf}1{"
Echo Do you wish to delete the following folders?>>"%temp%\tf}1{"
Echo Name>>"%temp%\tf}1{"
Echo %%I_%%J_%%K_%%L>>"%temp%\tf}1{"
)) Else (
Goto :_allFound
))
:_allFound
If Not Exist "%temp%\tf}1{" Echo No Folders Found to delete&Goto _Done
Set _rdflag= /q
If /I %_Prompt%==False Goto _Removeold
Type "%temp%\tf}1{" | More
:_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" (Echo (A/N/P only please)&Goto _Prompt1
Set _rdflag=
:_Removeold
For /F "skip=3 Delims=" %%I In ('type "%temp%\tf}1{"') Do (
If "%_rdflag%"=="" Echo Deleting
rd /s%_rdflag% "%%I")
:_Done
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PopD
Goto:EOF
::===================================::
:: ::
:: - S u b r o u t i n e s - ::
:: ::
::===================================::
:JDate
:: Convert date to Julian
:: Arguments : YYYY MM DD
:: Returns : Julian date in variable _JDate
:: Usage
::Call :JDate %__GYear% %_GMonth% %_GDay%
:: First strip leading zeroes; a logical error in this
:: routine was corrected with help from Alexander Shapiro
::Code taken from datediff.bat written by Rob van der Woude
::http://www.robvanderwoude.com
:: Modified to handle months and days witout leading zeros
:: By TheOutcaste http://forums.techguy.org
Set _JMM=%2
Set _JDD=%3
IF 1%_JMM% LSS 110 Set _JMM=%_JMM:~-1%
IF 1%_JDD% LSS 110 Set _JDD=%_JDD:~-1%
::
:: Algorithm based on Fliegel-Van Flandern
:: algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum
:: (http://mathforum.org/library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.
Set /A _JMonth1 = ( %_JMM% - 14 ) / 12
Set /A _JYear1 = %1 + 4800
Set /A _JDate = 1461 * ( %_JYear1% + %_JMonth1% ) / 4 + 367 * ( %_JMM% - 2 -12 * %_JMonth1% ) / 12 - ( 3 * ( ( %_JYear1% + %_JMonth1% + 100 ) /100 ) ) / 4 + %_JDD% - 32075
For %%A In (_JMonth1 _JYear1) Do Set %%A=
Goto:EOF
:GetDate
:: This subroutine will always display the same results,
:: for the date independent of "International" settings.
:: This batch file uses REG.EXE from the NT Resource Kit
:: (already installed with WinXP and Vista)
:: to read the "International" settings from the registry.
:: Date is returned as yyyymmdd in variable _fdate
:: Modified from SortDate Written by Rob van der Woude
:: http://www.robvanderwoude.com
::
If NOT [%1]==[] Set Date=%1
If "%date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
:: Default Delimiter of TAB and Space are used
For /F "SKIP=3 TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "SKIP=3 TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%B%%C
IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%C%%B
IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%B%%C%%D
Goto:EOF
Another option is to delete folders that are more than XX days old, and schedule it to run every couple of days. This way you always have XX days of backups saved
This will delete all folders more than 10 days old as set, so if ran on 08/12, folders dated 08/02 and newer will not be deleted.
It will list all the folders first, and you have the option of specifying to delete all, none, or be prompted for each.
Once you are happy with the way it works, you can eliminate the display of the list and the prompt by changing the
Set _Prompt= line to
False, and just have it delete all the folders automatically.
Code:
@Echo Off
:: Delete folders by date contained in folder name
:: Written by TheOutcaste for http://forums.techguy.org
Setlocal EnableDelayedExpansion
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: User Variables
:: Set this next line to False to automatically delete all folders found
Set _Prompt=True
:: Set this to the number of days you want to keep
Set _DaysKept=10
:: Set this to the folder that contains the folders to check and delete
Set _Path=C:\Batch\ARCHIVE\FINAL
:: Set this to the base name of the folders to find. If this string is not in the folder name, the folder will be skipped
:: Set this to nothing to find all folders
Set _BaseName=\Back*
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Get todays date
Call :GetDate
Set _yy=%_fDate:~,4%
Set _mm=%_fDate:~4,2%
Set _dd=%_fDate:~6,2%
:: Convert todays date to Julian
Call :JDate %_yy% %_mm% %_dd%
Set _JToday=%_JDate%
:: Set delete date
Set /a _DelDate=_JToday-%_DaysKept%-1
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PushD %_Path%
Set _s=s
If %_DaysKept%==1 set _s=
If /I %_Prompt%==True Echo Please wait, searching for folders more than %_DaysKept% day%_s% old
For /F "Tokens=1-4 Delims=_" %%I In ('dir "%_Path%%_BaseName%" /AD /B /ON') Do (
Call :JDate %%J %%K %%L
If !_JDate! LEQ %_DelDate% (
If Exist "%temp%\tf}1{" (
Echo %%I_%%J_%%K_%%L>>"%temp%\tf}1{"
) Else (
Echo.>"%temp%\tf}1{"
Echo Do you wish to delete the following folders?>>"%temp%\tf}1{"
Echo Name>>"%temp%\tf}1{"
Echo %%I_%%J_%%K_%%L>>"%temp%\tf}1{"
)) Else (
Goto :_allFound
))
:_allFound
If Not Exist "%temp%\tf}1{" Echo No Folders Found to delete&Goto _Done
Set _rdflag= /q
If /I %_Prompt%==False Goto _Removeold
Type "%temp%\tf}1{" | More
:_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" (Echo (A/N/P only please)&Goto _Prompt1
Set _rdflag=
:_Removeold
For /F "skip=3 Delims=" %%I In ('type "%temp%\tf}1{"') Do (
If "%_rdflag%"=="" Echo Deleting
rd /s%_rdflag% "%%I")
:_Done
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PopD
Goto:EOF
::===================================::
:: ::
:: - S u b r o u t i n e s - ::
:: ::
::===================================::
:JDate
:: Convert date to Julian
:: Arguments : YYYY MM DD
:: Returns : Julian date in variable _JDate
:: Usage
::Call :JDate %__GYear% %_GMonth% %_GDay%
:: First strip leading zeroes; a logical error in this
:: routine was corrected with help from Alexander Shapiro
::Code taken from datediff.bat written by Rob van der Woude
::http://www.robvanderwoude.com
:: Modified to handle months and days witout leading zeros
:: By TheOutcaste http://forums.techguy.org
Set _JMM=%2
Set _JDD=%3
IF 1%_JMM% LSS 110 Set _JMM=%_JMM:~-1%
IF 1%_JDD% LSS 110 Set _JDD=%_JDD:~-1%
::
:: Algorithm based on Fliegel-Van Flandern
:: algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum
:: (http://mathforum.org/library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.
Set /A _JMonth1 = ( %_JMM% - 14 ) / 12
Set /A _JYear1 = %1 + 4800
Set /A _JDate = 1461 * ( %_JYear1% + %_JMonth1% ) / 4 + 367 * ( %_JMM% - 2 -12 * %_JMonth1% ) / 12 - ( 3 * ( ( %_JYear1% + %_JMonth1% + 100 ) /100 ) ) / 4 + %_JDD% - 32075
For %%A In (_JMonth1 _JYear1) Do Set %%A=
Goto:EOF
:GetDate
:: This subroutine will always display the same results,
:: for the date independent of "International" settings.
:: This batch file uses REG.EXE from the NT Resource Kit
:: (already installed with WinXP and Vista)
:: to read the "International" settings from the registry.
:: Date is returned as yyyymmdd in variable _fdate
:: Modified from SortDate Written by Rob van der Woude
:: http://www.robvanderwoude.com
::
If NOT [%1]==[] Set Date=%1
If "%date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
:: Default Delimiter of TAB and Space are used
For /F "SKIP=3 TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "SKIP=3 TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%B%%C
IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%C%%B
IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%B%%C%%D
Goto:EOF