Solved

Windows Batch - Find Folder & Delete Files

Posted on 2009-05-20
4
438 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
[X]
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
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

630 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