Computing.Net > Forums > Programming > Batch File to clean folders

Batch File to clean folders

Reply to Message Icon

Original Message
Name: samcneal
Date: December 20, 2004 at 06:11:22 Pacific
Subject: Batch File to clean folders
OS: Windows 2003
CPU/Ram: Quad 4, 1 gig
Comment:

Anyone knows how to create a batch file to clean up a folder based on a certain number of days? I want to delete files older than 7 days but I don't know the syntax for date. I know how to delete files using the batch utility.


- Sonya


Report Offensive Message For Removal


Response Number 1
Name: dtech10
Date: December 21, 2004 at 15:29:39 Pacific
Reply: (edit)

Hi
I wrote a batch for WinXP to delete files older than a certain number of days, as I'm english I guess than I would have to change the date variables in the batch file.
ie this command typed at the dos prompt on my system "date /t" produces "21/12/2004".
Your would produce "12/21/2004" is that correct if not what format doe's it produce.
I'm not familar with Windows 2003 so I think it might work ok on your system.



Report Offensive Follow Up For Removal

Response Number 2
Name: FishMonger
Date: December 21, 2004 at 21:55:28 Pacific
Reply: (edit)

Calculating past/future dates and differences between them is not an easy task for a batch file.

If you're interested, here's a Perl script that would be cleaner than the batch file. This uses the parent directory as its starting point and does a recursive desent into each subdir deleting any file that was modified 7 or more days ago.

#!perl -w

use strict;
use File::Find;

my $dir = 'path/to/parent/dir';

finddepth(\&file_be_gone, $dir);

sub file_be_gone {
    return if /^\.\.?/;
    if ((_M $_) >= 7) { unlink $File::Find::name; }
}


Report Offensive Follow Up For Removal

Response Number 3
Name: FishMonger
Date: December 21, 2004 at 22:05:41 Pacific
Reply: (edit)

Oops, I made a typo.

change:
   if ((_M $_)

to:
   if ((-M $_)


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch File to clean folders

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge