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 ...
随机推荐
- (转)OpenGL学习——立方体贴图
转自:https://learnopengl-cn.readthedocs.io/zh/latest/04%20Advanced%20OpenGL/06%20Cubemaps/ 我们之前一直使用的是2 ...
- Codeforces542E Playing on Graph 思维+DFS+BFS
解法参考https://www.cnblogs.com/BearChild/p/7683114.html这位大佬的,这位大佬讲得很好了. 这道题还是有一定的思维的. 直接贴代码: #include&l ...
- mongoose 常用数据库操作 查询
条件查询 Model.find(conditions, [fields], [options], [callback]) demo1 try.js var User = require(". ...
- CSIC_716_20191225【HTML基础入门】
HTTP协议 超文本传输协议HyperText Transfer Protocol 四大特性: 1.一次请求一次响应 2.基于TCP/IP协议,作用于应用层 3.无状态 4.无连接 数据格式: 1.请 ...
- Python 十大装 B 语法解析
Python 是一种代表简单思想的语言,其语法相对简单,很容易上手.不过,如果就此小视 Python 语法的精妙和深邃,那就大错特错了.本文精心筛选了最能展现 Python 语法之精妙的十个知识点,并 ...
- Delphi 鼠标控制函数GetCursorPos、SetCursorPos
GetCursorPos函数 获取鼠标的位置 var P: TPoint; begin GetCursorPos(P); //获取鼠标位置 end; SetCursorPos函数 设置鼠标的位置 v ...
- 【Web】Spring WebFlux
阅读目录 一.关于WebFlux 二.SpringMVC与SpringWebFlux 三.Reactive Spring Web 四.实现WebFlux示例 SpringWebflux是SpringF ...
- Flex布局(二)
接上一篇,说一下flex布局的实例,转自阮一峰老师的博客
- nginx,php-fpm性能优化
When you running a highload website with PHP-FPM via FastCGI, the following tips may be useful to yo ...
- 用 Flask 来写个轻博客 (1) — 创建项目
目录 目录 前言 扩展阅读 部署开发环境 创建 Github 项目 前言 一步一步的实现一个 Flask 轻博客项目启动,最新的代码会上传到 Github. 扩展阅读 欢迎使用 Flask - vir ...