Suppress FORFILES "no files found" error

This should solve that issue:

@echo off
Echo Deleting files older than 14 days...
cd /d C:\Windows\System32
copy /b forfiles.exe "[file path...]\IDOC_ARCHIVE" >nul
FORFILES /P "[file path...]\IDOC_ARCHIVE" /M *.* /D -14 /C "cmd /c del @file"

What it does is provide a file older than 14 days - so it will always be deleted and the 'no files found' message won't appear. I chose the forfiles.exe to copy but you can use any file older than 14 days. All other error messages will appear as normal.


Adding 2>nul did the trick. Thanks!

forfiles /p d:\todayfiles /d +0 /c "cmd /c echo @path" 2>nul | find ":" /c

This basically suppresses the Error Stream of this command. So even if other errors appeared while doing this operation, they won't appear!


To reliably affect the ERRORLEVEL you need to run a subsequent command which is guaranteed to set ERRORLEVEL to 0. I use the command interpreter itself (cmd.exe) to do this as in the following snippet:

FORFILES  /M:somefiles /D -14 2>nul | cmd /c ""

Hope this helps someone.

Tags:

Batch File