Search code examples
windowsbatch-filefor-loopfindstr

Executing a pipe line command with findstr


I am trying to get the name of a file containing a particular string. The following command does that.

dir /a:-D /b | findstr /i "fileName.jar"

I'm trying to run it through a bat file using the following code.

for /F "delims=" %%a in ('dir /a:-D /b | findstr /i "fileName.jar"') do set "batToolDir=%%a"

But I am getting the below error.

| was unexpected at this time.

I need to get the name of the file containing a certain string. How can I achieve this? Any help would be much appreciated.


Solution

  • "below error" ??

    What you need is to escape the | to tell cmd that the pipe is part of the command to be executed, not of the for. You need to do this with all redirectors.

    The escape character is ^, so substitute ^| for | within the parentheses. ANy such awkwardness, try inserting ^ before the character causing the problem...