Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Delete All Contents of Folder Older than 7 Days

Posted on 2004-04-07
2
3,487 Views
Last Modified: 2007-12-19
To All,

My problem is, I use a batch file to copy all contents of the Z drive and put them in C:\htmlbackup\%date%
IE
C:\htmlbackup\2004-04-01
C:\htmlbackup\2004-04-02
C:\htmlbackup\2004-04-03
C:\htmlbackup\2004-04-04
And so on.

So I want to create a batch file that will delete all folders older than 7 days of the current date.
IE
Current date = 2004-04-10
I want to delete all folders before 2004-04-03

I will use Scheduled Tasks to run the batch file every 7 days.  I am using Windows Server 2003.

Thanks
0
Comment
Question by:chigs20
2 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 10780684
Try this:

@echo off
setlocal

set yyyy=

set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
 for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
    set %%x=%%u
    set %%y=%%v
    set %%z=%%w
    set $d1=
    set $tok=))

if "%yyyy%"=="" set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100

REM Substract your 7 days here
set /A dd=1%dd% - 100 - 7
set /A mm=1%mm% - 100 + 0

if /I %dd% GTR 0 goto DONE

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yyyy=%yyyy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31

echo ERROR: unexpected month:
echo dd=%dd% mm=%mm%, yyyy=%yyyy%
goto :EOF

:SET31

set /A dd=31 + %dd%

goto DONE

:SET30

set /A dd=30 + %dd%

goto DONE

:LEAPCHK

set /A tt=%yyyy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yyyy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yyyy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto DONE

:SET29

set /A dd=29 + %dd%

:DONE

if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%

set _CutOff=%yyyy%-%mm%-%dd%

set mm=
set yyyy=
set dd=
set tt=

REM ** Here is where you specify what you want to scan

for /F "delims=" %%i in ('dir /b /ad c:\htmlbackup') do if /I %%i LSS %_CutOff% echo rd "c:\htmlbackup\%%i" /s /q

set _CutOff=

Remove the echo statement to activate the rd command.

Good Luck,
Steve
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10819720
If you don't mind installing the free and powerfull Perl from (for instance)
http://www.activestate.com/Products/ActivePerl/?_x=1

I made the script below
1. safe: it will first display the dirs it keeps or remove (not deleting anything), please test
2. safe: it tries to change dir to where your backups are or leave if it cannot
3. safe: it works only on directories with name like year-month-day
4. after testing it, when you are ok, just uncomment the line "# rmtree($_);" => "rmtree($_);" to do the actual deletion

run the script (make sure perl is reachable):  perl  delbkp.pl
(note that file path are / based, not \ :)

--------script please call it  'delbkp.pl'------------------
#!perl

$NUMBER_OF_DAYS = 7;

$home = "C:/htmlbackup";

use File::Path;
use Time::Local;

chdir ($home) or die "Cannot change dir to $home";

$now = time();

for (<[12][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]>)
{
  if ( -d $_ )
  {
    split(/-/,$_);
    $before = timelocal(0,0,0,$_[2],$_[1]-1,$_[0]-1900);
    $delete = (($now - $before)/86400 > $NUMBER_OF_DAYS);
    print "$_ ", ($delete ? "remove":"keep"), "\n";
    #
    # uncomment (remove the #) the next line when you are ok!

    # rmtree($_) if ($delete);
  }
}
-------------------------------------------------------------
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Robocopy - Looking for help with syntax 7 162
Backup solution 14 79
Batch File - do a backup of file that is the last business day of the month 9 89
network copying issue 4 67
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question