0

I am trying to locate a file using dir and findstr to be able to specify the extension and a pattern in the name.

For this, I use the following command:

for /f %%a in ('dir /b /s *.pm  | findstr /i "MyPattern"') do (set "name=%%a")

The question has been mostly answered in here, here and in here.

But none of them applied to my case.

double-beep
  • 5,031
  • 17
  • 33
  • 41
user2536125
  • 245
  • 1
  • 3
  • 17
  • 2
    None of your linked question (or their answers) handles your problem. You need to escape some special chars inside the `for /f %%a in ('critical part') do ...`. Those chars are `|<>&,=)` (I hope, I didn't forget some): `for /f %%a in ('dir /b /s *.pm ^| findstr /i "MyPattern"') do ...` – Stephan Feb 01 '19 at 08:51
  • Works perfectly – user2536125 Feb 01 '19 at 09:54
  • [duplicate](https://stackoverflow.com/questions/20841622/error-was-unexpected-at-this-time-batch-script) – Stephan Feb 01 '19 at 13:01
  • @Stephan duplicate of [Batch character escaping](//stackoverflow.com/q/6828751) – double-beep Feb 11 '19 at 19:12

1 Answers1

1

You need to escape the pipe (|) because it breaks the for loop just because it is always executed with higher prio. Modify your code like the following:

for /f %%a in ('dir /b /s *.pm  ^| findstr /i "MyPattern"') do (set "name=%%a")

and it should work.

double-beep
  • 5,031
  • 17
  • 33
  • 41