Go Back   Tech Support Forum > Microsoft Support > Windows XP Support

User Tag List

Need batch to delete folder by age

This is a discussion on Need batch to delete folder by age within the Windows XP Support forums, part of the Tech Support Forum category. Need: Hello, I am looking for a "simple" batch file that will delete any folder in the root of the


Closed Thread
 
Thread Tools Search this Thread
Old 04-24-2009, 01:32 PM   #1
Guest
 
Join Date: Apr 2008
Posts: 3
OS:



Need:
Hello, I am looking for a "simple" batch file that will delete any folder in the root of the drive, that is older than "x" days - but not the subfolders.

Setting:
My client insists on saving her data onto a large USB drive. I schedule a batch file to run similar to this:

md "G:\BACKUP\%DATE%"
robocopy /mir C:\Data "G:\BACKUP\%DATE%\data"

When run once a week it puts a copy of "data" into a folder under G:\ named after the date it runs.
i.e. "G:\BACKUP\Fri 04-24-2009\data"

In 12 weeks the drive is full. I need a script that will delete any folder - in the root - created more than 90 days ago; this way my drive won't overfill. I do not want to delete sub folders older than 90 days cause most of the data in the sub folders are older that 90 days.

This allows the client to have 3 months worth of backups at one week increments. Please no VB Scripts, I've seen them and they are to complicated for me

BTW to get the %DATE% variable to look like the above, you need to change the date separater in control panel to a hyhpen from a slash.

TIA
shromani is offline  
Sponsored Links
Advertisement
 
Old 04-25-2009, 03:00 AM   #2
Microsoft MVP
 
TheOutcaste's Avatar

Microsoft Most Valuable Professional
 
Join Date: Mar 2009
Location: Portland, OR
Posts: 3,339
OS: MS-Dos 6.22 - Win7



This would be really simple if you first convert the date to a YYYYMMDD format (or YYYY-MM-DD) and use that to name the folders.
That way they would always sort in chronological order, and all you'd have to do is a simple For loop that does a Dir with newest first, skip the first 12 folders, and delete the rest.

As you've created the folders with the date in the name we can check that date, so the date of any of the Data won't matter.

This will do what you want. It will search all the folders in G:\Backup, check the date in the folder name, then present a list.
You can delete all, none, or be prompted for each folder.
If you just want it to run and delete all, that part can be commented out.
Always best to test with sample data first though.

The file contains a tab character, and some long lines. The tab can't be copied off the forum, and the long lines sometimes get split, so I've attached the file as a Zip.
Modify the blue lines to change the path or number of days to keep.
Code:
@Echo Off
   :: Set the next variable equal to tab followed by a space
:: If you copy this code, you will have to manually edit this line
Set _TabSpace=
Setlocal EnableDelayedExpansion
:: User Variables
:: Set this to the number of days you want to keep
   Set _DaysKept=90
:: Set this to the folder that contains the folders to check and delete
   Set _Path=G:\Backup
:: 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=
Echo Please wait, searching for folders more than %_DaysKept% day%_s% old
For /F "Tokens=1-4 Delims=- " %%I In ('dir "%_Path%" /AD /B /ON') Do (
  Call :JDate %%L %%J %%K
  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{"
	)))
:_allFound
If Not Exist "%temp%\tf}1{" Echo No Folders Found to delete&Goto _Done
Type "%temp%\tf}1{" | More
Set _rdflag= /q
:_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{"
Set _TabSpace=
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
::https://www.robvanderwoude.com
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
:: (https://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
:: https://www.robvanderwoude.com
::
If NOT [%1]==[] Set Date=%1
If "%date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
:: Delims= is a TAB followed by a space in the next two lines
For /F "SKIP=3 TOKENS=2* DELIMS=%_TabSpace%" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "SKIP=3 TOKENS=2* DELIMS=%_TabSpace%" %%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
HTH

Jerry
Attached Files
File Type: zip DelbyDate.zip (1.8 KB, 158 views)
TheOutcaste is offline  
Old 05-08-2009, 11:05 AM   #3
Guest
 
Join Date: May 2009
Posts: 1
OS:



I used this code and its finding the folder to delete but its not deleting it, help
atugaoen is offline  
Sponsored Links
Advertisement
 
Old 05-08-2009, 11:13 AM   #4
Troubled
 
Join Date: Mar 2009
Location: 530
Posts: 3,544
OS: 7 x64, XP x32



Would a vb program do? I could convert to .exe then convert to.bat, lol.

Just a simple directory.deletedir(dim gen as inputbox("")).
deleted122510 is offline  
Old 05-08-2009, 08:56 PM   #5
Microsoft MVP
 
TheOutcaste's Avatar

Microsoft Most Valuable Professional
 
Join Date: Mar 2009
Location: Portland, OR
Posts: 3,339
OS: MS-Dos 6.22 - Win7



So you get this:
Do you wish to delete the following folders?
(list of found folders)
Delete All, None, or Prompt for each (A/N/P)?


But when you press A, none get deleted?
Do you get any errors displayed?
If you press P, does it show this for each folder?
<folder name>, Are you sure (Y/N)?
Again, do you get any errors displayed?

The folders must include the day of the week in front of the date, and must have a space after it, not a hyphen. And can't have anything after the date
Example:
Fri 05-08-2009 will be found and deleted.
Fri-05-08-2009 will be found, but will not be deleted
Fri 05-08-2009-backup will be found, but will not be deleted.

If you are using a slightly different folder name format, just post a sample folder name and the code can be modified to account for it.

Jerry
TheOutcaste is offline  
Old 05-09-2009, 05:50 PM   #6
Guest
 
Join Date: Apr 2008
Posts: 3
OS:



Great, thanks for the suggestions so far.
shromani is offline  
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is on
Smilies are on
[IMG] code is on
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Post a Question


» Site Navigation
 > FAQ
  > 10.0.0.2
Powered by vBadvanced CMPS v3.2.3


All times are GMT -7. The time now is 06:15 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
vBulletin Security provided by vBSecurity v2.2.2 (Pro) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.1.0 (Pro) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
Copyright 2001 - 2018, Tech Support Forum

Windows 10 - Windows 7 - Windows XP - Windows Vista - Trojan Removal - Spyware Removal - Virus Removal - Networking - Security - Top Web Hosts