Batch Script - Find if String is missing in a file, show output

こ雲淡風輕ζ 提交于 2019-12-11 19:15:49

问题


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.


回答1:


This will output all files that don't have the string into the file Missing.txt.

for %%a in (*.*) do (
find "$O123456.MIN%" %%a
if %errorlevel%==1 echo %%a >Missing.txt
)



回答2:


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

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

2 - Your text files all have .txt extensions

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:

  • Change the file mask
  • Make the search case insensitive
  • Recursively process all folders within the root folder
  • Process a list of specific folders
  • Restrict which characters and/or how many characters can be used in the random file (your 0123456)


来源:https://stackoverflow.com/questions/14381077/batch-script-find-if-string-is-missing-in-a-file-show-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!