Home > front end >  Find multiple string matches from the original string batch script
Find multiple string matches from the original string batch script

Time:04-27

I currently have a list of files in a folder that i need to iterate through and exclude the non production files that matches the pattern. how do i search for multiple strings in a batch command?

for example i have files as

  • QA_test_1.txt
  • QA_809_test.txt
  • dev_93439_sd.txt
  • stg_abc_sldkf_df.txt
  • prod_4845_sdfd.txt
  • prod_998_sdfd.txt
  • live_lskd_sd3434.txt
  • xyz_sdfdf_s3rer.txt

from the list above i need to exclude every file that has the word "dev", QA "Stg" and generate a list of files without them - which is

  • prod_4845_sdfd.txt
  • prod_998_sdfd.txt
  • live_lskd_sd3434.txt
  • xyz_sdfdf_s3rer.txt

The problem is - i am not able to find any batch command that can do a grep equivalent for multiple strings. FindStr does it only for one string and hence i am not able to get that working. here is code that i have

set i=0
for /F "delims=" %%a in ('dir /B /A /S Path *.txt') do (
   set /A i =1
rem put the file names in array
   set list[!i!]=%%~na
rem try to find the file names 
echo %%~na|find "dev" or "qa" or "stg" >nul
if errorlevel 1 (echo notfound) else (echo found %%~na )
  
)

CodePudding user response:

Thank you for all the response. I was able to do it below way

echo %%~na|findstr /i /v "dev QA Stg" >nul
if errorlevel 1 (echo not found %%~na) else (echo %%~na>>newfile.txt)
  • Related