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 crash desktop dns driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop mac malware memory monitor motherboard network not working printer problem ram registry repair router slow software sound 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 >
Solved: Deleting Folders older than 30 days from date.

Reply  
Thread Tools
scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
10-Jan-2010, 01:24 AM #1
Solved: Deleting Folders older than 30 days from date.
I would like to try to write this on my own however need a push in the correct direction.

I have TB drive where I save the data for my clients for 30 days. I am currently manually going there and anything over 30 days I am removing the directory.

I know how to look at the directory names with a FOR statement. How would I match their dates with the current date?

The OS is 2003 Small Business Server if that matters.
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets
Resources LLC: Water Treatment Coagulation, Water Recycling
Freemorrison's Avatar
Computer Specs
Senior Member with 571 posts.
 
Join Date: Nov 2009
Location: South Beach Florida
Experience: Intermediate
10-Jan-2010, 01:52 AM #2
So basically you would like to have an app that will sweep out any - and all files older than 30 days on your TB- is that correct?
scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
10-Jan-2010, 01:55 AM #3
Yes.

However I would like to write this in Batch. I don't mind using an application in the meantime however my goal is to sharpen my BAT skills so I am attempting to write most of the stuff I do in Batch for the time being until such time as I go onto the next language.

It would be the directories and subdirectories I would be deleting however.

RD ?? /S /Q
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets
Resources LLC: Water Treatment Coagulation, Water Recycling
Freemorrison's Avatar
Computer Specs
Senior Member with 571 posts.
 
Join Date: Nov 2009
Location: South Beach Florida
Experience: Intermediate
10-Jan-2010, 01:56 AM #4
scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
10-Jan-2010, 02:03 AM #5
Not quite what i am looking for but does help push me in the right direction.

I now now that I have to get todays date probably from date. Just have to figure out how to compare the created date of the folder against todays date.
scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
10-Jan-2010, 03:17 AM #6
Okay,

I have a portion of this written

Code:
FOR /F "tokens=1-4 delims=/ " %%I IN ('DATE /t') DO SET /A mydate=%%J%%K%%L
 
FOR /F tokens=1 %%A IN ('DIR /t:C') DO CALL :_removedirs "%%A"
:_removedirs
if [%1]< GOTO :EOF
GOTO :EOF
Am I heading in the right direction or am I way off base? I am running into errors with the /A without the delims=/ because it keeps wanting to divide. I was thinking of coming up with an alogrithm that turned the date to number and then subtracted 30 or something but I figure there has to be an easier way, right?
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets
Resources LLC: Water Treatment Coagulation, Water Recycling
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
10-Jan-2010, 03:19 AM #7
Here's an outline:
  • Determine system's regional settings to determine date format.
  • Get today's date
  • Convert to Julian so you can do date math easily
  • Subtract the number of days you want to keep to get a Julian date to compare to.
  • Get list of files or folders, convert the flle/folder dates to Julian
  • Compare with the target date and delete file/remove folder if older.

Some things to look out for.
A folder's modified date doesn't always get updated when a file in the folder gets modified, it can take up to an hour for the changed date to be written to disk and appear in explorer or the command prompt.
If this is an Archive where once the folder is created, files in the folders won't be updated, added, or changed it won't matter.
If files can be updated, added, or changed, there's a 1 hour window where a file could have been changed, but the folder date won't show it, and you could delete the entire folder by mistake if you look only at folder dates.
In that case you'd want to check the dates of each file, remove if they meet the criteria, then if the folder is empty, remove the folder.

The %~t modifier shows the modified date, not the created date. If you need to check the created date, you can't use a bare Dir listing. You'd need to parse the full dir output to extract the date.

Dir /OD will sort by date, you can jump out of the loop when the dates get new enough.
Dir /OD /S sorts by date within each folder, so you can't jump out.

If you are only looking at one level of folders it's pretty easy, if you need to search through a tree, it gets complex.

You can walk the tree and check files in each folder, or might work to output date and full name for each file in the tree to a file (Echo Juliandate:fullpath), use sort to sort by date, then read the file, check date, and delete until the dates reach the taget date.

I think you probably have the date routines to get the date and convert to julian. I updated the getdate routine a couple weeks ago when Squashman pointed out it doesn't work if the system uses a two digit year setting. The new version is in Post 8 in this thread:
http://forums.techguy.org/software-d...ch-script.html
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
10-Jan-2010, 03:25 AM #8
Actually I did not know about the Julian Date. I have utilized dates in the past but not to compare, only to write log files. This one is new for me.

I am reading over your post now.

I wrote some FOR statements above. The time one I actualy copied from somewhere however it was almost what I had with the exception of the delims=/
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets
Resources LLC: Water Treatment Coagulation, Water Recycling
scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
10-Jan-2010, 03:35 AM #9
Okay I have found a subroutine which teaches me how and actually has examples on how to convert to a Julian Date.
http://www.robvanderwoude.com/datetimentmath.php
scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
10-Jan-2010, 03:35 AM #10
Thanks, I will look into this tomorrow. I am getting extremely tired. Need some sleep. 2:34am here.
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
10-Jan-2010, 03:42 AM #11
Yeah we hit send at about the same time.
You don't need the /A with the Set statement, you're not doing math in that particular line, just getting the date into variable without the nonnumeric delimiters (/)
You can also just extract substrings from %date%, but it only works if the format is the same.
if you figure on MM/DD/YYYY and it's YYYY/MM/DD or DD/MM/YYYY you can have "issues"
If it's US format (MM/DD/YYYY) this will work, doesn't car if the day of the week is included:
%Date:~-4,4% will give the year
%Date:~-7,2% will give the day
%Date:~-10,2% will give the month
If you run this on the first of the month, you can just compare the months with a little adjustment and delete only those items with a month older than last month
If month <3 add 12 and subtract 1 from year
combine the new numbers as YYYYMM, so Jan2009 would end up 200813. Now subtract 1 to get 200812
Get year and month for each file, if <200812 delete.
Feb would be 200813
March would be 200902
Not exactly 30, deletes items over 1 month (28-31 days) old.
If you need it to be exactly 30 days no matter when you run it, best to convert to Julian.
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

Freemorrison's Avatar
Computer Specs
Senior Member with 571 posts.
 
Join Date: Nov 2009
Location: South Beach Florida
Experience: Intermediate
10-Jan-2010, 09:54 AM #12
scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
10-Jan-2010, 11:29 AM #13
FreeMorrison,

That is actually a great looking tool. I will see if it works on Server 2003 software.

I am still going to write the Bat file however. The goal here is to attempt to sharpen my Batch writing skills. In the meantime, I will utilize that software if it runs.

Thanks,

Jerry,

Thanks for the heads up. I don't need it to be exact. Just 1 month is all. We tell clients that we keep their data for 1 month before it gets deleted. I am going to attempt to write a script with what you have provided.
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets
Resources LLC: Water Treatment Coagulation, Water Recycling
scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
10-Jan-2010, 01:03 PM #14
Okay,

That page I posted above. Nevermind about that. It doesn't work. It was written for NT nd is not compatible with XP, Vista, etc. However I did simply rewrite my own based upon the same algorithm that the original write wrote his.

However my question is this:
How do I know what the correct Julian date really is?
Let me explain:

According to this military website the Julian date is one date: http://aa.usno.navy.mil/data/docs/JulianDate.php

However according to the original site above:
http://www.robvanderwoude.com/datetimentmath.php

As well as this website
http://www.fourmilab.ch/documents/calendar/

I am presuming that this means that the military website is wrong??

Here is my code:
Code:
echo off
FOR /F "tokens=1-4 delims=/ " %%I IN ('DATE /t:C') DO (
SET _mth=%%J
SET _day=%%K
SET _yr=%%L
)
Echo Converting Real Date to Julian Date
SET /A _month=((%_mth%-14)/12)
SET /A _year=(%_yr%+4800)
Echo _month= %_month%
Echo _year = %_year%
SET /A _jdate=((1461*(%_year%+%_month%)/4)+((367*((%_mth%-2)-(12*%_month%)))/12))-((3*((%_year%+%_month%+100)/100))/4)+(%_day%-32075)
Echo %_jdate%
GOTO:EOF
Here is the new julian date algorithm that will allow calculating between two dates.

Given credit where credit is due.

Date: 05/29/2003 at 08:39:20
From: Doctor Fenton
Subject: Re: Fliegel-Van Flandern algorithm

The algorithm I utilized to convert this is from
Given Y,M,D, the year, month, and date, define (using integer
arithmetic)

M1 = (M-14)/12
Y1 = Y + 4800 .

Then the Julian date J is

J = 1461*(Y1+M1)/4 + 367*(M-2-12*M1)/12 - (3*((Y1+M1+100)/100))/4
+ D - 32075 .

For example, for Y = 2001, M = 2, and D = 24,

M1 = -1
Y1 = 6801 ,

and

J = 2483700 + 367 - 51 + 24 -32075

= 2451965 .

These algorithms are available in the Explanatory Supplement to the
Astronomical Almanac, 2nd edition, P. Seidelmann (ed.), or in
Fliegel and Van Flandern's original paper,

Fliegel, H. F., and Van Flandern, T. C., "A Machine Algorithm for
Processing Calendar Dates," Communications of the Association of
Computing Machines, vol. 11 (1968), p. 657.

If you have any questions, please write back and I will try to
explain further.

- Doctor Fenton, The Math Forum
http://mathforum.org/dr.math/


Okay, so now I can convert over to a Julian Date. That was actually pretty simple. I thought that was going to be a lot harder.

Tell me if I am heading in the correct direction.

Next step.
Get the CREATED date from each one of the directories in a FOR statement as described above.

DIR /t:C

Something to the below other than I have to point it to what directory and figure out the math.

Code:
FOR /F tokens=1 %%A IN ('DIR /t:C') DO CALL :_removedirs "%%A"
 
:_removedirs
:: SET /A _createdate= (or should this be in the FOR statement, probably)
:: if [%1]< GOTO :EOF
:: GOTO :EOF
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets
Resources LLC: Water Treatment Coagulation, Water Recycling
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
10-Jan-2010, 05:23 PM #15
This sounds like a good time to use forfiles.
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 03:02 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.