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%
)
Batch - 忽略FORFILES “no files found” error的更多相关文章
- Spring Boot整合Spring Batch
引言 Spring Batch是处理大量数据操作的一个框架,主要用来读取大量数据,然后进行一定的处理后输出指定的形式.比如我们可以将csv文件中的数据(数据量几百万甚至几千万都是没问题的)批处理插入保 ...
- Data storage on the batch layer
4.1 Storage requirements for the master dataset To determine the requirements for data storage, you ...
- 新部署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 ...
- 手动创建binary log files和手动编辑binary log index file会有什么影响
基本环境:官方社区版MySQL 5.7.19 一.了解Binary Log结构 1.1.High-Level Binary Log Structure and Contents • Binlog包括b ...
- MySQL Error Code文档手册---摘自MySQL官方网站
This chapter lists the errors that may appear when you call MySQL from any host language. The first ...
- mysql: Error Codes and Messages
Appendix B. Error Codes and MessagesTable of Contents B.1. Server Error Codes and MessagesB.2. Clien ...
- Read Large Files in Python
I have a large file ( ~4G) to process in Python. I wonder whether it is OK to "read" such ...
- bat programming is easy and powerful
用linux的角度来思考windows,习惯了linux的shell后, 再来看windows的bat编程,就简单多了,简直就是理所当然 实际上windows的cmd命令行和linux的shell命令 ...
- Linux中shell文件操作大全
1.创建文件夹#!/bin/shmkdir -m 777 "%%1" 2.创建文件#!/bin/shtouch "%%1" 3.删除文件#!/bin/shrm ...
随机推荐
- KiCAD原理图导出PDF方法
KiCAD原理图导出为PDF 1.文件->绘制 2.按照下图选择保存目录和输出格式后,选择绘制当前页或者所有页
- 如何在MySQL中删除表中指定列的唯一键?
语法结构如下: alter table table_name drop index column_name;
- 分分钟教你学会 ToolBar 的使用(转)
转自:http://blog.csdn.net/itguangit/article/details/52042203 1.和平常一样,新建一个Moudle 在xml布局文件中使用 Toolbar 控件 ...
- jQuery随机抽取数字号代码
html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...
- 1、cmd中检测远程的ip和端口是否处于监听状态
一.使用 ping 命令测试远程的ip是否可连通 cmd (右键 管理员角色) --- ping IP 二.使用 telnet 测试远程某一个ip的端口是否开放 1.为了安全起见,window ...
- docker安装cboard
参考链接: https://hub.docker.com/r/chenlmdocker/docker-cboard/dockerfile https://www.cnblogs.com/zimo-ji ...
- Mina---系统学习
1.为何使用Mina? java提供的BIO.NIO使用的复杂性等原因,导致Mina框架的诞生: 2.什么时候使用Mina? 易于使用 高并发的用户量 被证明的系统: Mina已被全球数以万计的应用使 ...
- jQuery冒泡事件阻止
JQuery 提供了两种方式来阻止事件冒泡. 方式一:event.stopPropagation(); $("#div1").mousedown(function(event){ ...
- java中EL表达式怎么获取网站的根目录
${pageContext.request.contextPath} <a href="${pageContext.request.contextPath}/login.jsp&quo ...
- Redis Cluster集群详介绍和伪集群搭建
1 什么是Redis-Cluster 为何要搭建Redis集群.Redis是在内存中保存数据的,而我们的电脑一般内存都不大,这也就意味着Redis不适合存储大数据,适合存储大数据的是Hadoop生态系 ...