3

I would like to I would like to continue running the batch only if a specific string exists in a txt file.

Example:

setlocal ENABLEDELAYEDEXPANSION

if....
Findstr "check_ok" "C:\tmp\test.txt"
( 
     ...continue...
) ELSE (
exit
)

but I don't want to create a file.

I can't find the correct way to do it...

1 Answer 1

4

Hard to tell exactly what you're asking, because you're already running the batch file so how do you not run the batch file to find out if you should run the batch file?

Regardless, I'm going to see if this helps you:

@echo off
setlocal enabledelayedexpansion

:: Check for "check_ok" and exit the batch file if it is NOT found
Findstr -m /S /C:"check_ok" "C:\tmp\test.txt" || goto :eof

:: If we get here, then check_ok was found
:: Continue with the rest of your batch file
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the quick reply! sorry for the description of the problem, I edited the request. is it better now?
Yes, the question is better. I believe my answer will help you.
As a side note, to the above, I'd just like to add that check_ok does not contain any metacharacters, so there's no need to use the /C option. I would suggest we determine the actual intent and adjust the options accordingly, for instance, FindStr "\<check_ok\>" "C:\tmp\test.txt">NUL||GoTo :EOF would be more specific, as may FindStr /L "check_ok" "C:\tmp\test.txt">NUL||GoTo :EOF. You could even use Find.exe instead, Find "check_ok"<"C:\tmp\test.txt">NUL||GoTo :EOF or Find " check_ok "<"C:\tmp\test.txt">NUL||GoTo :EOF

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.