Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress FORFILES "no files found" error

Tags:

batch-file

I'm working with a batch file to delete archived documents older than 14 days, and I'm calling the file from an automation process (Lansa Composer) that reads the return code of the script to see if there was a problem. Here's the script:

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

The issue is that the script returns an error code and prints "ERROR: No files found with the specified search criteria" if it doesn't find any files to delete, when I really only want it to return an error if there is a problem accessing the directory or running the del command, etc. Is there some way I can get this script to suppress the "no files found" error, but allow others to pass through?

After some Googling I tried the solutions on this page, but they won't work for what I want, since in the first case it suppresses ALL errors, and in the second the text of the error message is passed, but the actual return code is still suppressed (which is what the automation process reads).

like image 498
Malcolm Avatar asked May 29 '13 17:05

Malcolm


People also ask

How do I delete forfiles?

You can free up space and keep things organized by only deleting files that are older than a certain number of days in any folder — here's how to do it. To delete files older than 30 days on Windows 10, use the “ForFiles” command. The command is: ForFiles /p “C:\path\to\folder” /s /d -30 /c “cmd /c del /q @file”.

Where is forfiles EXE located?

The forfiles command was first introduced as an optional component of Windows NT. Beginning with Windows Vista, it was included in the standard Windows operating system. It's also available as part of Windows 7, Windows 8, Windows 10, and Windows 11. Its executable file is installed at %WINDIR%/System32/forfiles.exe.

What is the forfiles command?

The forfiles command lets you run a command on or pass arguments to multiple files. For example, you could run the type command on all files in a tree with the . txt file name extension.


2 Answers

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.

like image 139
foxidrive Avatar answered Oct 19 '22 19:10

foxidrive


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!

like image 21
Andrey Lopatin Avatar answered Oct 19 '22 18:10

Andrey Lopatin