Use commands conditionally

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Use commands conditionally

#1 Post by Rileyh » 04 Dec 2011 03:47

I have a "for" string and I need to be able to run "pause >nul" if a find is successful. But when I use this code:

Code: Select all

for /f "tokens=1-2*" %%a in ('findstr /b /i "halt" "!$file!"') do (
  pause >nul)


All it does is pause, even when there is no string "halt" in the file "!$file!"
NOTE- Delayed expansion is on, but I only copied the part with an issue.

How can I get it to pause conditionally?

Regards,
Rileyh

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Use commands conditionally

#2 Post by orange_batch » 04 Dec 2011 04:27

? That is strange. Maybe your problem is elsewhere in the script, or maybe you overlooked something in your text file.

Try moving the right parenthesis ) from pause >nul to the next line. Syntax sometimes acts differently than on the command line.

Obviously you're breaking the line into tokens for some reason, but if you're only looking to pause if halt is found you could write:

Code: Select all

findstr /b /i "halt" "!$file!"&&pause >nul

Post Reply