Solved

Windows Batch - Find Folder & Delete Files

Posted on 2009-05-20
4
429 Views
Last Modified: 2012-05-07
I need to search a folder structure to check if certain folders exist, if they do then need to delete the files in the folders which are 14 days+ old.

Example of folder structure:

c:\tobe\letters\reject\
c:\bech\letters\printed\
c:\aiki\pollo\
c:\obbo\cricket\
c:\itsa\letters\reject\
c:\lana\letters\printed

Ideally I want to search c:\*.*\letters\ if 'reject' or 'printed' folders exist then delete files older than 14 days in c:\*.*\letter\reject & c:\*.*\letters\printed.

The script structure should be:
1. A FOR statement looping through and searching c:\*.*\letters\
2. IF EXIST 'reject' folder THEN do FORFILES cmd to delete files older than 14 days
3. IF EXIST 'printed' folder THEN do FORFILES cmd to delete files older than 14 days

Some of the code I've been tinkering with, as you can see I'm struggling :\
REM Some code I've been tinkering with
 
for /d %a in ("c:\*") do echo c:\%a\letters\
if exist c:\%a\letters\reject
forfiles /p   c:\*.*\letters\reject\* /s /m*.* /dt-14 /c"cmd /c echo @file"

Open in new window

0
Comment
Question by:Rich55555
  • 3
4 Comments
 
LVL 4

Expert Comment

by:manavsi
ID: 24438035
You might want to consider using a language that is better suited for this task.

My preference would be to use Perl. Perl can do in 1 line of code everything that your current batch file does, excluding the echo statements. The echo statements would be about the same, but Perl can even do that part in fewer lines. Perl can also very easily traverse a directory tree and delete only the desired files.
#!perl -w
 
use strict;
use File::Find;
 
usage() if (! @ARGV || $ARGV[0] =~ /\D/);
 
my $age = $ARGV[0];
my $dir = $ARGV[1] || '.';
 
find(sub { print "$_\n" if -M $_ > $age; }, $dir);
 
sub usage {
print <<'EOF';
USAGE:
DELOLD X [Dir]
Where:
X is the number of days previous to Today.
Dir is the optional directory where files exist. Defaults to current directory.
 
EX: "DELOLD 5" Deletes files older than 5 days.
"DELOLD 120 c:\temp" Deletes files from the c:\temp directory that are older than 120 days.
EOF
}

Open in new window

0
 
LVL 4

Expert Comment

by:manavsi
ID: 24438040
may be calling this perl script with ur batch file can help u i suppose.. :)


HTH
Manavsi
0
 
LVL 4

Accepted Solution

by:
manavsi earned 500 total points
ID: 24438048
if u are very particular about Batch file.. then try considering this.. But, it deletes only at the root directory u have this script and not the sub ones..

HTH
Manavsi
@echo off
SET OLDERTHAN=%1
IF NOT DEFINED OLDERTHAN GOTO SYNTAX
 
set FileDir=%~2
 
if "%FileDir%"=="" set FileDir=.
 
for /f "tokens=2" %%i in ('date /t') do set thedate=%%i
 
set mm=%thedate:~0,2%
set dd=%thedate:~3,2%
set yyyy=%thedate:~6,4%
 
set /A dd=%dd% - %OLDERTHAN%
set /A mm=%mm% + 0
 
:LOOPDATE
 
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
 
:SET31
set /A dd=31 + %dd%
goto LOOPDATE
 
:SET30
set /A dd=30 + %dd%
goto LOOPDATE
 
: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 LOOPDATE
 
: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%
for %%i in (%FileDir%\*.*) do (set FileName=%%i call :PROCESSFILE %%~ti)
 
set mm=
set yyyy=
set dd=
set thedate=
goto EXIT
 
:SYNTAX
ECHO.
ECHO USAGE:
ECHO DELOLD X [Dir]
ECHO Where:
echo X is the number of days previous to Today.
echo Dir is the optional directory where files exist. Defaults to current directory.
ECHO.
ECHO EX: "DELOLD 5" Deletes files older than 5 days.
echo "DELOLD 120 c:\temp Deletes files from the c:\temp directory that are older
echo than 120 days.
GOTO EXIT
 
:PROCESSFILE
set temp=%1
 
set fyyyy=%temp:~6%
 
if /I %fyyyy% LSS 100 set fyyyy=20%fyyyy%
if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%
 
set fmm=%temp:~0,2%
set fdd=%temp:~3,2%
 
REM ::  This is where the files are deleted 
REM ::  Change the ECHO command to DEL to 
REM ::  delete. ECHO is used for test. 
 
 
if /I %yyyy%/%mm%/%dd% GEQ %fyyyy%/%fmm%/%fdd% (echo %FileName%)
 
set temp=
set fyyyy=
set fmm=
set fdd=
 
:EXIT

Open in new window

0
 

Author Comment

by:Rich55555
ID: 24439509
Manavsi, definately wanted to do this in batch file.

Your batch file seems complex, could that not be simplified with the 'forfiles' command?
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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