Batch - 忽略FORFILES “no files found” error

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%
)

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

时间: 2024-08-01 18:55:52

Batch - 忽略FORFILES “no files found” error的相关文章

Windows Batch 1-4

Guide to Windows Batch Scripting http://steve-jansen.github.io/guides/windows-batch-scripting/index.html Overview batch可以帮助配置 DevOps , 提高每天的工作效率. Part 1 – Getting Started Launching the Command Prompt keyboard shortcut Windows Logo Key + R 输入cmd.exe E

MySQL Error Codes MYSQL的错误代码

OS error code 1: Operation not permitted OS error code 2: No such file or directory OS error code 3: No such process OS error code 4: Interrupted system call OS error code 5: Input/output error OS error code 6: No such device or address OS error code

Core data的batch update

关于Core data最大的抱怨是缺乏批处理操作.比如RSS阅读器这种需要在众多entities上操作的应用,没有批处理操作就必须将所有东西装入内存,然后手动设置必要的attributes. 通常你会这么操作: NSFetchRequest *fetchAllRSSItems = [NSFetchRequest fetchRequestWithEntityName:[RSSItem entityName]]; NSError *fetchError; NSArray *results = [se

MySQL Error Code文档手册---摘自MySQL官方网站

This chapter lists the errors that may appear when you call MySQL from any host language. The first list displays server error messages. The second list displays client program messages. Server error information comes from the following files: The Er

新部署tomcat,An error occurred at line: [1] index_jsp.java

环境: centos6.5 32位 oracle jdk 1.8 tomcat 7 问题: yum install tomcat后,返回如下错误: [[email protected]]~# curl -v http://localhost:8080 * About to connect() to localhost port 8080 (#0) * Trying 127.0.0.1... connected * Connected to localhost (127.0.0.1) port 8

Data storage on the batch layer

4.1 Storage requirements for the master dataset To determine the requirements for data storage, you must consider how your data will be written and how it will be read. The role of the batch layer within the Lambda Architecture affects both values. I

Read Large Files in Python

I have a large file ( ~4G) to process in Python. I wonder whether it is OK to "read" such a large file. So I tried in the following several ways: The original large file to deal with is not "./CentOS-6.5-i386.iso", I just take this fil

手动创建binary log files和手动编辑binary log index file会有什么影响

一.了解Binary Log结构 1.1.High-Level Binary Log Structure and Contents • Binlog包括binary log files和index file• 每个binary log文件的前4字节是Magic Number,紧接着是一组描述数据修改的Events • The magic number bytes are 0xfe 0x62 0x69 0x6e = 0xfe 'b''i''n' • 每个Event包括header bytes和da

loadrunner自学以及问题解答1

QQ群:2423597857 ============================== 1.LR 脚本为空的解决方法: 去掉ie设置中的第三方支持取消掉 在系统属性-高级-性能-数据执行保护中,添加loadrunner安装目录中的vugen.exe文件. 有可能是由于录制的URL地址采用的是localhost的问题,改成分配的IP地址或127.0.0.1试试. 2.插入文本检查点步骤时,使用web_reg_find,通常TextPfx和TextSfx中会包含双引号,需要进行转义(用斜杠),例