Rich55555
asked on
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 :\
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"
may be calling this perl script with ur batch file can help u i suppose.. :)
HTH
Manavsi
HTH
Manavsi
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Manavsi, definately wanted to do this in batch file.
Your batch file seems complex, could that not be simplified with the 'forfiles' command?
Your batch file seems complex, could that not be simplified with the 'forfiles' command?
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.
Open in new window