码迷,mamicode.com
首页 > 其他好文 > 详细

Batch - 忽略FORFILES “no files found” error

时间:2019-09-27 19:05:45      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:ase   cape   out   one   token   forfile   pat   man   file   

ref:https://stackoverflow.com/questions/16820681/suppress-forfiles-no-files-found-error

 

Solution:

The solution is to capture the output of the FORFILES command in a FOR loop, search it for strings starting with ERROR, and store the result in a variable. From there, you can use IF/ELSE directives to set the errorlevel accordingly. Here‘s the code (minus some logging and comments):

cd /d C:\Windows\System32
SET _CmdResult=NONE
FOR /F "tokens=*" %%a IN (‘FORFILES /P "[file path...]\IDOC_ARCHIVE" /M *.* /D -14 /C "cmd /c DEL @file" 2^>^&1 ^| FINDSTR ERROR‘) DO SET _CmdResult=%%a
IF "%_CmdResult%" == "ERROR: No files found with the specified search criteria." ( 
    SET errorlevel=0 
 ) ELSE ( 
    SET errorlevel=1
 )
IF "%_CmdResult%" == "NONE" SET errorlevel=0

 

Just make sure to escape any characters such as >&| in the FOR loop.

 

Test Code:

ECHO "Remove old takara book dump files from network directory \\global.com\gm\EU\unitycredit_dev\ACE\TraderMetrics\Sybase\TakaraBookData\ older than 7 days"

SET _CmdResult=NONE
FOR /F "tokens=*" %%a IN (‘FORFILES /P "\\global.com\gm\EU\unitycredit_dev\ACE\TraderMetrics\Sybase\TakaraBookData\" /M *.* /D -7 /C "cmd /c echo @path" 2^>^&1 ^| FINDSTR ERROR‘) DO SET _CmdResult=%%a
IF "%_CmdResult%" == "ERROR: No files found with the specified search criteria." (
    SET errorlevel=0
 ) ELSE (
    SET errorlevel=1
 )
IF "%_CmdResult%" == "NONE" SET errorlevel=0

ECHO "Find all 7 days older files in certain path, PRINT its FILE PATH..."
PushD "\\global.com\gm\EU\unitycredit_dev\ACE\TraderMetrics\Sybase\TakaraBookData\" &&(
    forfiles -s -m *.* /D -7 /C "cmd /c echo @path"
     ) & PopD

ECHO "Find all 7 days older files in certain path, DELETE it..."
PushD "\\global.com\gm\EU\unitycredit_dev\ACE\TraderMetrics\Sybase\TakaraBookData\" &&(
    forfiles -s -m *.* /D -7 /C "cmd /c del @path"
     ) & PopD

IF "%ERRORLEVEL%"=="0" (
    set ERRORLEVEL=0
    exit /B 0

) ELSE (
    ECHO "ERRORLEVEL=%ERRORLEVEL%"
    exit /B %ERRORLEVEL%
)

 

Batch - 忽略FORFILES “no files found” error

标签:ase   cape   out   one   token   forfile   pat   man   file   

原文地址:https://www.cnblogs.com/frankcui/p/11599547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!