问题
I tried to write a batch script to do the below task
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
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
expected output
- 12345678 is found in *.txt
- 1145897 is not found
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.
回答1:
@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%"
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.
来源:https://stackoverflow.com/questions/25402636/batch-script-to-search-multiple-string-in-multiple-files