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的更多相关文章

  1. Spring Boot整合Spring Batch

    引言 Spring Batch是处理大量数据操作的一个框架,主要用来读取大量数据,然后进行一定的处理后输出指定的形式.比如我们可以将csv文件中的数据(数据量几百万甚至几千万都是没问题的)批处理插入保 ...

  2. Data storage on the batch layer

    4.1 Storage requirements for the master dataset To determine the requirements for data storage, you ...

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

    环境: centos6.5 32位 oracle jdk 1.8 tomcat 7 问题: yum install tomcat后,返回如下错误: [root@centos]~# curl -v ht ...

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

    基本环境:官方社区版MySQL 5.7.19 一.了解Binary Log结构 1.1.High-Level Binary Log Structure and Contents • Binlog包括b ...

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

    This chapter lists the errors that may appear when you call MySQL from any host language. The first ...

  6. mysql: Error Codes and Messages

    Appendix B. Error Codes and MessagesTable of Contents B.1. Server Error Codes and MessagesB.2. Clien ...

  7. Read Large Files in Python

    I have a large file ( ~4G) to process in Python. I wonder whether it is OK to "read" such ...

  8. bat programming is easy and powerful

    用linux的角度来思考windows,习惯了linux的shell后, 再来看windows的bat编程,就简单多了,简直就是理所当然 实际上windows的cmd命令行和linux的shell命令 ...

  9. Linux中shell文件操作大全

    1.创建文件夹#!/bin/shmkdir -m 777 "%%1" 2.创建文件#!/bin/shtouch "%%1" 3.删除文件#!/bin/shrm ...

随机推荐

  1. 负载均衡实现故障vip自动漂移

    环境说明演示vip自动漂移 192.168.237.50 192.168.237.51 vip: 192.168.237.5 keepalived开源软件实现 keepalived可以实现当vip挂的 ...

  2. 第九章 Service

    2019-09-23 今天距离2020年刚好有一百天,希望在未来的百日里能不负期待 不忘初心,方得始终, 初心易得,始终难守. 一.Service 的概念 Kubernetes Service定义了这 ...

  3. Github Statistics 一个基于 React 的 GitHub 数据统计工具

    V 站曾经有个热帖说为何我的开源项目只有 Fork 没有 Star,楼下有个热评说开源项目关注的不应该是 Commit 数据吗?先不论 Star.Fork 和 Commit,issue .pr 也应是 ...

  4. zabbix之自动注册,编写触发器:一定时间内超过某个负载值的时间

    zabbix中添加主机很多时可以考虑自动注册来自动添加,下面按照图片顺序来看,zabbix版本:3.0.28 超过负载30%的持续时间 创建触发器:

  5. 生产者消费者模式-->线程

    #_author:来童星#date:2019/12/17#生产者消费者模式-->线程from queue import Queueimport random,time,threading#生产者 ...

  6. PHP的开源产品discuz

    首先就是discuz,用起来真的是特别的好用,搭建的网站真的非常美观 尤其是用起来之后,我发现功能真的是太强大了,不用到处编写代码,调试什么 只需要把精力放在做产品上就可以了,我很好奇为什么会这么强大 ...

  7. [bzoj2287]消失之物 题解(背包dp)

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1138  Solved: 654[Submit][ ...

  8. iOS 7 认识 TextKit

    本文由 伯乐在线 - 和谐老约翰 翻译自 Max Seelemann.欢迎加入技术翻译小组.转载请参见文章末尾处的要求. iOS7 的发布给开发者的案头带来了很多新工具.其中一个就是 TextKit( ...

  9. php中正则表达式总结(不容错过)

    php中正则表达式总结(不容错过) 一.总结 一句话总结: 无论js,php,java,python里面中的正则都是差不多一样的,所以用点脑子 用到正则的地方很多,比如 nginx的配置文件 1.ph ...

  10. C++的ofstream与ifstream使用

    基本理解: ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间; 在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的 ...