批处理脚本-查找文件中是否缺少字符串,显示输出 [英] Batch Script - Find if String is missing in a file, show output

查看:162
本文介绍了批处理脚本-查找文件中是否缺少字符串,显示输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有10个文件夹的群集,每个文件夹包含1000个程序文件.我需要在这些文本文件中搜索"MISSING"字符串.所有文件必须以$ O123456.MIN%开头(123456是随机文件名).我知道如何查找字符串是否存在,但是如何识别字符串是否不存在?

I have cluster of 10 folders, each with 1000 program files. I need to search these text files for a MISSING string. All files must start with $O123456.MIN% (123456 being random file names). I know how to find if the string exists, but how do I identify if the string does not exist?

一旦确定了哪个文件缺少字符串,我希望将报告或该文件的副本移到另一个文件夹中.

Once it is identified, what file is missing the string, I would like eather a report or a copy of that file, moved to another folder.

推荐答案

您的要求不清楚.我假设以下情况:

Your requirements are not clear. I am assuming the following:

1-您的10个文件夹的群集"由某个根文件夹中的所有文件夹组成.

1 - Your "cluster" of 10 folders consists of all folders within a certain root folder.

2-您的文本文件均具有.txt扩展名

2 - Your text files all have .txt extensions

3-您要报告指定文件夹中的所有文件,其中第一行不是以$O*.MIN%开头,其中*表示任意1个或多个字符,并且OMIN区分大小写.

3 - You want to report all files within the specified folders where the first line does not start with $O*.MIN%, where * represents any 1 or more characters, and O and MIN are case sensitive.

@echo off
setlocal
set "rootFolder=c:\yourRootPath"
set "fileMask=*.txt"
set "outFile=missing.txt"

>"%outFile%" (
  for /d %%D in ("%rootFolder%") for %%F in ("%%D\%fileMask%") do (
    findstr /nbr "$O..*\.MIN%%" "%%F" | findstr /bl "1:" >nul || echo %%F
  )
)

如果您的实际要求不同,则可能不需要太多代码更改.例如,执行以下任何更改都是微不足道的:

If your actual requirements are different, it probably won't take much code change. For example, any of the following changes would be trivial to implement:

  • 更改文件掩码
  • 使搜索大小写不敏感
  • 递归处理根文件夹中的所有文件夹
  • 处理特定文件夹的列表
  • 限制随机文件中可以使用哪些字符和/或多少个字符(您的0123456)

这篇关于批处理脚本-查找文件中是否缺少字符串,显示输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆