批处理脚本以在多个文件中搜索多个字符串 [英] Batch script to search multiple string in multiple files

查看:463
本文介绍了批处理脚本以在多个文件中搜索多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个批处理脚本来完成以下任务

I tried to write a batch script to do the below task

我在名为empids.txt的文本文件中列出了1000个唯一的Employee ID(数字ID,位数可能有所不同).还有一个名为MasterIDs的文件夹,其中包含多个文本文件,每个文本文件都有10万个ID

I have list of 1000 unique Employee IDs (numerical IDs, no. of digits may differ) in a text file called empids.txt. And also I have folder called MasterIDs which contains multiple text files and each text file has 100 thousand IDs

我尝试了一个批处理脚本来搜索empids.txt中列出的1000个ID,并显示在MasterIDs文件夹中的多个文本文件中是否存在任何列出的ID.预期结果如下所示

I tried a batch script to search the 1000 IDs listed in the empids.txt and show the whether any listed ids are present in the multiple textfiles in the MasterIDs folder. The expected result is like the below

预期输出

  • 12345678在* .txt中找到
  • 未找到1145897

下面是我尝试的批处理脚本,但没有得到预期的输出.它只是搜索并给出包含搜索字符串的整行.

below is the batch script i tried and I am not getting the expected output. It just searches and give the whole line which contains the searched string.

set manifest_folder=\\vfiler-padhu\padhu\*.txt
set file_list=\\vfiler-padhu\padh\File_list.txt
set tmpFile=\\vfiler-padhu\padh\tmpFile.txt
for /f "delims=" %%f in (%file_list%) do (
findstr /L  %%f %manifest_folder% >> %tmpFile%
) 
pause

我才刚刚开始学习批处理脚本.请帮助完成此任务.

I am just started learning batch script. Kindly help to do this task.

推荐答案

@echo off
setlocal enableextensions disabledelayedexpansion

set "manifest_folder=\\vfiler-padhu\padhu\*.txt"
set "file_list=\\vfiler-padhu\padh\File_list.txt"
set "tmpFile=\\vfiler-padhu\padh\tmpFile.txt"

    (for /f "usebackq delims=" %%a in ("%file_list%") do (
        set "found="
        for /f "delims=" %%b in ('findstr /l /m /c:"%%a" "%manifest_folder%"') do (
            echo %%a is found in %%~nxb
            set "found=1"
        )
        if not defined found (
            echo %%a is not found
        )
    )) > "%outputFile%"

这将读取输入文件,并针对清单行中的每个行/id进行搜索,询问在其中找到id的文件的名称.

This will read input file and for each line/id a search in the manifest folder is executed, asking for the name of the files where the id is found.

这篇关于批处理脚本以在多个文件中搜索多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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