You can pipe the FORFILES result to FINDSTR with the /V and /G:file options to filter out the files you want to ignore. You can embed the files to ignore directly in your batch script. The full path of each file should be used, with enclosing quotes to match the FORFILES output.
I use the /L option to force the search to use literal strings, the /X option to make sure the filter uses an exact match, and the /I option to make it case insensitive.
Note that @path represents the full path, including the file name. So @file is not needed.
Also, FORFILES will list folders, so you should exclude them using @ISDIR
@echo off
forfiles /p n:\ /m * /s /c "cmd /c if @isdir==FALSE echo @path" /d -3 | findstr /vixlg:"%~f0" > c:\temp\output.txt
exit /b
"n:\somePath1\someFileToIgnore1.ext"
"n:\somePath2\someFileToIgnore2.ext"
etc.
If you want to exclude all files within a specific folder, then you will need to modify the above script to use regular expressions, using \R instead of \L. You can construct a regular expression to specify a specific file, all files within a specific folder, or all files within a folder tree.
Backslash literals must be escaped as \\
, and period literals escaped as \.
. Excluding files within a folder uses [^\\]*
to represent the files - it matches any string of characters except except backslash. Excluding a folder tree uses .*
to match any string of characters to match both folders and files.
@echo off
forfiles /p n:\ /m * /s /c "cmd /c if @isdir==FALSE echo @path" /d -3 | findstr /vixrg:"%~f0" > c:\temp\output.txt
exit /b
"n:\\somePath1\\someFileToIgnore1\.ext"
"n:\\somePath2\\ignoreFilesInThisFolder\\[^\\]*"
"n:\\somePath3\\ignoreFilesInThisFolderTree\\.*"
etc.