Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

Windows Batch - Find Folder & Delete Files

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
Rich55555
Asked:
Rich55555
  • 3
1 Solution
 
manavsiCommented:
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
 
manavsiCommented:
may be calling this perl script with ur batch file can help u i suppose.. :)


HTH
Manavsi
0
 
manavsiCommented:
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
 
Rich55555Author Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now