从原始字符串批处理脚本查找多个字符串匹配

发布于 2025-01-22 12:57:13 字数 822 浏览 2 评论 0原文

我目前在文件夹中有一个文件列表,我需要迭代并排除与模式匹配的非生产文件。如何在批处理命令中搜索多个字符串?

例如,我将文件作为

  • 每个
  • 具有
  • ude
  • dev
  • QA_TEST_1.TXT QA_809_TEST.TXT DEV_93439_SD.TXT STG_ABC_ABC_SLDKF_TXT
  • qa

”一词的文件, STG”并生成一个没有它们的文件列表 - 这是

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

,问题是 - 我无法找到任何批量的pressert pressivalents equialtings。 Findstr仅适用于一个字符串,因此我无法实现该工作。这是我有的代码

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 )
  
)

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 )
  
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱没勇气 2025-01-29 12:57:13

谢谢您的所有回应。
我能够在下面做

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文