Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
DOS/Other
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router security slow software sound toshiba trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/Other >
Delete previous Month Folder-Batch File

Reply  
Thread Tools
hrshelke's Avatar
Junior Member with 1 posts.
 
Join Date: Aug 2009
12-Aug-2009, 03:53 PM #1
Smile Delete previous Month Folder-Batch File
Hi,

I have backup folders which creates on daily basis like at C:\Batch\ARCHIVE\FINAL

BACK_2009_07_15
BACK_2009_07_16
BACK_2009_08_12

I am looking for batch code which can delete all folders in previous month like

BACK_2009_07_15
BACK_2009_07_16

not
BACK_2009_08_12

One.

Please let me know the proper code for this
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
12-Aug-2009, 09:33 PM #2
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
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

ghostdog74's Avatar
Member with 146 posts.
 
Join Date: Dec 2005
14-Aug-2009, 01:06 AM #3
Quote:
Originally Posted by hrshelke View Post
Hi,

I have backup folders which creates on daily basis like at C:\Batch\ARCHIVE\FINAL

BACK_2009_07_15
BACK_2009_07_16
BACK_2009_08_12

I am looking for batch code which can delete all folders in previous month like

BACK_2009_07_15
BACK_2009_07_16

not
BACK_2009_08_12

One.

Please let me know the proper code for this
if you can download stuff, use the GNU win32 findutils and coreutils
Code:
c:\myfolder > find . -type d -name "BACK_*" -mtime +30 -print0 | xargs rm -rf
the above find directories more than 30 days old and remove them. put the above in a bat file, and schedule to run every last day of the month.
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:49 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.