Link to home
Start Free TrialLog in
Avatar of Michelle Aycoth
Michelle Aycoth

asked on

Batch File script to delete files based on match

I am new to writing windows batch code. I am trying to remove files from a folder that are older than 3 days old but are checked before deleting to make sure it deletes all matching files. For instance...
123456.html
123456.txt
123456.xxx
987654.html
987654.txt


Given the list of files, I would want the batch code to locate the .xxx file and find any matching files with the same file prefix  and delete all three when all three are older than 3 days old.

I found the following on the internet:
@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\Users\ad9f2p\TEST CLEANUP"
FOR /f "delims=" %%a IN (
 'dir /b /a-d "%sourcedir%\*" '
) DO (
 SET "filename=%%a"
 SET "filename=!filename:*.XXX=!"
 IF "!filename!"=="!filename:_=!" ECHO DEL "%sourcedir%\%%a"
)
Pause
GOTO :EOF

I pulled this code from the web and started to modify it, but I am struggling since I am uncertain what some of the commands/variables are doing.   In its current state, it echoed everything in the directory to delete. It should have left the 987654 files alone.

Any help would be greatly appreciated.  Thanks, Mojeaux
Avatar of NVIT
NVIT
Flag of United States of America image

This does what you want.
Note: Currently, it ECHOs the DEL command for your verification. When you are satisfied that's what you want, remove the ECHO

@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\Users\ad9f2p\TEST CLEANUP"
pushd %sourcedir%
FOR /f "tokens=*" %%a IN (
 'dir /b "%sourcedir%\*.xxx"'
) DO (
 SET "filename=%%a"
 forfiles /m "%%~na.*" /d -3 /c "cmd /c ECHO del @path"
)
popd

Open in new window

Avatar of Michelle Aycoth
Michelle Aycoth

ASKER

Thank you for the quick reply but when I run the suggested script I get the following error:  "ERROR:  No files found with the specified search criteria."   I double checked my directory to make sure I did have files with this suffix.  I copied and pasted as is... should I have changed anything?   Thx, Mojeaux
>  I get the following error:  "ERROR:  No files found with the specified search criteria."

Ignore it. It just means no qualified files were found, i.e. 3 day old files.
I failed to mention... The code search the specified folder only. It does not search sub-folders. Are sub-folders a requirement?
I don't think the above script works as desired. Can you confirm this:
1. you want to look for .xxx files
2. then find all files with the same base name (but different extension)
3. and only delete if all files found are older than 3 days.
> I don't think the above script works as desired

The OP's or mine?
I tested mine.
Neither one. Please review the question, in particular
Given the list of files, I would want the batch code to locate the .xxx file and find any matching files with the same file prefix  and delete all three when all three are older than 3 days old.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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
Similar solution in PowerShell, less elegant than I wanted it to be.
$basedir = 'C:\Users\ad9f2p\TEST CLEANUP'

$old = (get-date).AddDays(-3)
get-childitem $basedir\*.xxx |
  ? { $_.LastWriteTime -le $old } |
  %  {
    $filemask = $_.DirectoryName+'\'+$_.BaseName+'.*'
    if ( ! (get-childitem $filemask | ? { $_.LastWriteTime -gt $old } ))
    {
       get-childitem $base | remove-item
    }
  }

Open in new window

Thank you.  It works beautifully.   I'm going through to learn exactly how everything works now.   Truly appreciate the assist.
Appreciate everyone's contribution and quick response on the question.   It was a huge help.   Many Thanks!  Mojeaux