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. 【JS学习】慕课网7-23编程练习 有关字符串数组

    要求:1.显示打印的日期. 格式为类似“2014年03月21日 星期三” 的当前的时间.2.计算出该班级的平均分(保留整数).同学成绩数据如下:"小明:87; 小花:81; 小红:97; 小 ...

  2. 并查集(Disjoint Set Union,DSU)

    定义: 并查集是一种用来管理元素分组情况的数据结构. 作用: 查询元素a和元素b是否属于同一组 合并元素a和元素b所在的组 优化方法: 1.路径压缩 2.添加高度属性 拓展延伸: 分组并查集 带权并查 ...

  3. springboot集成使用rabbitmq笔记(1.rabbitmq安装)

    使用rabbitmq笔记一 使用rabbitmq笔记二 使用rabbitmq笔记三 1.选择适配的版本,参考---https://www.rabbitmq.com/which-erlang.html ...

  4. 在delphi中执行javascript代码

    有时做项目难免用到代码交叉调用,delphi中执行js就是一种,两种方法可用: 一.使用webbrower,比较麻烦 二.使用ScriptControl,简单方便: 1.首先 uses ComObj; ...

  5. AcWing 157. 树形地铁系统 (hash判断树同构)打卡

    一些主要城市拥有树形的地铁系统,即在任何一对车站之间,有且只有一种方式可以乘坐地铁. 此外,这些城市大多数都有一个中央车站. 想象一下,你是一名在拥有树形地铁系统的城市游玩的游客,你想探索该城市完整的 ...

  6. jq鼠标移入和移出事件

    前几天帮朋友做了一个单页面,其中有个效果就是鼠标移动到头像上变换头像样式,当鼠标移出时恢复头像样式.当时没多想,脑子就蹦出了mouseover,mouseout两个方法. 但是在编写页面的过程中,无论 ...

  7. 可持化永久树 的 STL ( rope )

    rope 的基本操作 #include <ext/rope> using namespace __gnu_cxx; ]; rope<int> x; rope<int> ...

  8. C++一些不常见的库及函数

    pbds库 平衡树:one , two #include <bits/extc++.h> using namespace std; using namespace __gnu_pbds; ...

  9. [参考]C# JSON字符串序列化与反序列化

    C#将对象序列化成JSON字符串 public string GetJsonString() { List<Product> products = new List<Product& ...

  10. CentOS 搭建dns服务器 解析任意域名

    DNS服务器IP地址:192.168.1.219 服务器版本:centos6.6一:软件安装 [root@localhost ~]# yum -y install bind* 二:修改主配置文件 [r ...