9

Trying to test a string to see if it contains a substring in a Windows batch file.

This is what I have so far:

echo %1 | find "message"
if %errorlevel% == 0 echo contains string

The command line output for this is (the contents of %1 was "messages\Message.js"):

messages\Message.js
contains string

The issue I am having is that the only way I can get this to work is with the exact line: echo %1 | find "js".

How can I do this without echoing the file path each time? When I remove the echo, the operating system attempts to open the file. I would love to save the file path to a variable, but nothing I tried worked, I always ended up with an empty variable.

1 Answer 1

7
    echo %1 | find "message" > NUL
    if %errorlevel% == 0 echo contains string
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.