Home > database >  Batch Delete excluding files under specific folder (or file names with specific pattern /wildcard e.
Batch Delete excluding files under specific folder (or file names with specific pattern /wildcard e.

Time:12-07

FORFILES /P "C:\Temp\" /D -3 /S /C "cmd /c if @isdir==FALSE del /F /Q @path"

above script is working fine and delete all files under Temp and its subdirectory older than 3 days.

I want to exclude all files from specific folder say all files from folder XYZ or full path-> C:\Temp\ABC\XYZ

Note : all files under XYZ folders are having pattern say Test*.*.csv

CodePudding user response:

forfiles does not have an exclude option. You have to use something like findstr /V to exclude the results, but that will not form part of the forfiles command in itself. We simply incorporate a for loop and ecxlude using findstr /V, then delete:

@echo off
for /f "delims=" %%i in ('FORFILES /P "%temp%" /D -3 /S /C "cmd /c if @isdir==FALSE echo @path"^| findstr /VI "C:\Temp\ABC\XYZ"') do del /F /Q "%%~i"
  • Related