zoukankan      html  css  js  c++  java
  • Batch

    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:WindowsSystem32
    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.comgmEUunitycredit_devACETraderMetricsSybaseTakaraBookData older than 7 days"
    
    SET _CmdResult=NONE
    FOR /F "tokens=*" %%a IN ('FORFILES /P "\global.comgmEUunitycredit_devACETraderMetricsSybaseTakaraBookData" /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.comgmEUunitycredit_devACETraderMetricsSybaseTakaraBookData" &&(
        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.comgmEUunitycredit_devACETraderMetricsSybaseTakaraBookData" &&(
        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%
    )
  • 相关阅读:
    es6笔记6^_^generator
    es6笔记5^_^set、map、iterator
    es6笔记4^_^function
    es6笔记3^_^object
    关于eslint的使用与配置,以及prettier的使用
    关于查看本机ssh公钥以及生成公钥
    Github上传图片图床
    力扣剑指Offer:39. 数组中出现次数超过一半的数字
    计蒜客:求平均年龄Python方法
    力扣:面试题59. 滑动窗口的最大值Python题解
  • 原文地址:https://www.cnblogs.com/frankcui/p/11599547.html
Copyright © 2011-2022 走看看