prateeksarda
prateeksarda

Reputation: 947

forfiles to suppress No find files not working

I am using forfiles to display text files older than 300 days. If I don't find any files forfiles gives an error, so I used following code to suppress the error, and return output as:

A:"NONE"
success 

code below:

SETLOCAL ENABLEDELAYEDEXPANSION
SET res=NONE


for /f "tokens=*" %%a in ('forfiles /d -300 /p E:\Webex /m *.txt /c "cmd /c echo @file ^>^>log.txt" ^| findstr error' ) do set res=%%a
ECHO A:"!res!" >>log.txt 
if "!res!" == "ERROR: No files found with the specified search criteria." (
    ECHO B:"!res!" >>log.txt
    SET err_lv=0
) ELSE (
    SET err_lv=1
)

IF "!res!" == "NONE" SET err_lv=0

if !err_lv! equ 1 (
    ECHO failure >>log.txt
) else (
    ECHO success >>log.txt
)


pause

Upvotes: 2

Views: 4289

Answers (2)

D1v3
D1v3

Reputation: 101

Calling FORFILES /p E: /D -1 /C "cmd /c echo @path"2>nul alone still raises the error level and caused my code to fail.

I found that in order to not trigger any try/catch or other error handling I needed to call this command from a jenkinsfile (groovy script) and what worked for me was to add a simple echo statement that would not return an error. It's cludgey but this is the simplest most effective solution for my work.

bat '''
            FORFILES /p E: /D -1 /C "cmd /c echo @path"2>nul
            ECHO "It's okay if we don't find any files to delete."
'''

Upvotes: 0

foxidrive
foxidrive

Reputation: 41234

To eliminate the error message, add a 2>nul to the end of the line
which redirects STDERR stream 2 (standard error) to nul.

forfiles /d -300 /p E:\Webex /m *.txt /c "cmd /c echo @file" 2>nul

Upvotes: 4

Related Questions