Link to home
Start Free TrialLog in
Avatar of KFFrench
KFFrench

asked on

Delete files within directory which are older than or equal to 7 days from System Date and which do not have "DONOTDELETE" in filename.

I have attempted to modify SteveGTR's script (https://www.experts-exchange.com/questions/20947290/Delete-All-Contents-of-Folder-Older-than-7-Days.html) but to no avail. As an example, only the following files annotated as "<<DELETE>>" would be deleted from the directory.

Directory of C:\Test
System Date = 03/09/2005 11:14 AM

03/01/2005  11:06 AM          Log1.xls                               <<DELETE>>
03/02/2005  11:06 AM          Log2.xls                               <<DELETE>>
03/02/2005  11:15 AM          DONOTDELETE_log.xls
03/02/2005  11:22 AM          DONOTDELETE_log.xls
03/03/2005  11:07 AM          Log3.xls                              
03/04/2005  11:07 AM          Log4.xls                              
03/05/2005  11:08 AM          Log5.xls                              
03/06/2005  11:07 AM          Log6.xls
03/07/2005  11:07 AM          Log7.xls
03/08/2005  11:06 AM          Log8.xls

This solution should run as a scheduled batch file in Windows XP or Windows 2000 environments



ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KFFrench
KFFrench

ASKER

Steve,

I just have one other question regarding this script. Currently the script looks at the Date Modified of the file to determine if it should be deleted. Can this be changed to the Date Created? If so, how?


It can, but it would require a lot more work. The dir command allows for a /TC switch that will display the date created. My processing makes use of the /b, bare, dir switch and the enhanced for substituion %%~it for the modified time. The processing would have to be changed to use the standard output year/time/size/name.
Steve,

Ok. Hopefully it won't be necessary. Thanks for all your help!
You're welcomed. Let me know if you need it and we can add it to this thread.
Steve -

I changed the date interval -7 to -2 and the ran the script against several archived .xls and .txt files with a range of dates from last year through today. The script successfully identified and did not delete the files annotated with "DONOTERASE" in the file name or the .txt files with a date of less than -2 e.g. 1/29/2006 and 1/30/2006. However, it incorrectly deleted all the .xls files including the files with a date of less than -2 days from today's date . I was trying to figure out what went wrong. Can you help?
Post the code you used.
@echo off

setlocal

set fileDir=%~1

if "%fileDir%"=="" set fileDir=c:\test

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 - 2
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%

pushd.

cd /d "%fileDir%"

for /F "delims=" %%i in ('dir /b /a-d ^| findstr /V /I DNE') do call :CHECK "%%i" %%~ti

popd

goto :EOF

:CHECK

set _ChkDate=%2
set /A _Year=1%_ChkDate:~-2% - 100

if /I %_Year% LSS 40 (
  set /A _Year+=2000
) else (
  set /A _Year+=1900
)

set _ChkDate=%_Year%/%_ChkDate:~0,2%/%_ChkDate:~3,2%

if %_ChkDate% GTR %_CutOff% goto :EOF

del /f %1
The line:

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

should be:

set _CutOff=%yyyy%/%mm%/%dd%

My mistake.
This may help..

Directory Listing BEFORE running script

Directory of C:\Test

01/30/2006  10:21 PM    <DIR>          .
01/30/2006  10:21 PM    <DIR>          ..
01/30/2006  10:21 PM                 0 dir.txt
01/27/2006  11:45 PM            10,752 DNE_Log.xls
01/28/2006  08:35 PM            10,752 Log3.xls
01/14/2006  08:35 PM            10,752 Log4.xls
01/27/2006  11:45 PM            10,752 Log5.xls
01/28/2006  08:35 PM            10,752 Log6.xls
01/13/2006  02:58 PM               407 Weekly (1).bat
11/18/2005  05:36 PM               674 Weekly (10).bat
11/17/2005  04:40 PM               672 Weekly (11).bat
11/17/2005  05:16 PM               262 Weekly (12).bat
11/17/2005  05:16 PM               263 Weekly (13).bat
01/28/2006  12:06 AM               256 Weekly (2).bat
01/13/2006  11:47 PM               388 Weekly (3).bat
01/14/2006  11:32 AM               539 Weekly (4).bat
11/21/2005  06:16 PM               222 Weekly (5).bat
11/18/2005  09:33 AM               229 Weekly (6).bat
11/18/2005  09:34 AM               231 Weekly (7).bat
11/18/2005  05:34 PM               225 Weekly (8).bat
11/22/2005  10:22 AM               248 Weekly (9).bat
01/30/2006  09:56 PM             2,293 Weekly.bat

Directory Listing AFTER running script:

Directory of C:\Test

01/30/2006  10:24 PM    <DIR>          .
01/30/2006  10:24 PM    <DIR>          ..
01/30/2006  10:24 PM                 0 dir.txt

It wacked everything on this try and threw a bunch of unregcognized internal/external command errors.
Did you try the change I suggested before your last post?
..Running some tests...
I made the change. Here are the results w/ X = -2 from 01/30/206

BEFORE RUN:

01/28/2006  08:35 PM            10,752 DNE_Log.xls         <<Should not be deleted>>
01/27/2006  11:45 PM            10,752 Log (1).xls
01/14/2006  08:35 PM            10,752 Log.xls
01/30/2006  10:32 PM             2,293 Weekly (13).bat     <<Should not be deleted>>
11/18/2005  09:33 AM               229 Weekly (14).bat
11/18/2005  09:34 AM               231 Weekly (15).bat
11/18/2005  05:34 PM               225 Weekly (16).bat
11/22/2005  10:22 AM               248 Weekly (17).bat
11/18/2005  05:36 PM               674 Weekly (18).bat
11/17/2005  04:40 PM               672 Weekly (19).bat
11/17/2005  05:16 PM               262 Weekly (20).bat
11/17/2005  05:16 PM               263 Weekly (21).bat
11/21/2005  06:16 PM               222 Weekly (22).bat
01/13/2006  02:58 PM               407 Weekly (23).bat
01/28/2006  12:06 AM               256 Weekly (24).bat      <<Should not be deleted>>
01/14/2006  11:32 AM               539 Weekly (25).bat
01/13/2006  11:47 PM               388 Weekly (26).bat

AFTER:

01/30/2006  10:32 PM             2,293 Weekly (13).bat

I think I see it now. There should be some sort of wildcard for the "DNE" find parameter. I made a mistake on "Weekly (25).bat", it's not less than -2 days since the system time is just now 11:02 PM where I'm at.
Looks like you'll want to change this line:

if %_ChkDate% GTR %_CutOff% goto :EOF

To:

if %_ChkDate% GEQ %_CutOff% goto :EOF

As for why the DNE file is not being excluded, I can't say. Does it show up when you issue this command from the DOS prompt:

dir /b /a-d | findstr /V /I DNE

It didn't on my machine.

As you've noticed it should be filtered out with this command:

for /F "delims=" %%i in ('dir /b /a-d ^| findstr /V /I DNE') do call :CHECK "%%i" %%~ti
No wildcards necessary. The findstr switches are returning lines that don't match using case insensitivity. Answer the above questions and we'll move on from there.
That's it! Thanks a bunch! Can I award you more points for your addtional help?
Not necessary :) Glad to have help.