Error with conditional command use

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

Error with conditional command use

#1 Post by Rileyh » 17 Dec 2011 04:55

I am trying to make a command called "stop /message=(something)" and it is supposed to do the same thing as this:

Code: Select all

pause >nul
echo (something)

where it would appear as though instead of "press any key to continue" it has said the (something).

But in my code:

Code: Select all

:break
REM Stop:
REM Stop /message=
set "$file=test.txt"
set "$match=stop /message="
for /f "tokens=1-26" %%a in ('findstr /b /i "!$match!" "!$file!"') do (
  set "b=%%a"
  set "rest=!b:*$match=!"
  set "pause=pause"
  !pause! >nul
  set "message=!rest!"
  !message!
  goto :break
  )
 
  :break


it says, "stop is not recoognised as a internal or external command, operation or batch file."
Could someone show me how to get this to work?

Regards,
Rileyh

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Error with conditional command use

#2 Post by aGerman » 17 Dec 2011 08:22

Try something like that:

Code: Select all

@echo off &setlocal enableExtensions disableDelayedExpansion
::::::::: create macro "$stop" :::::::::
set LF=^


set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"&rem TWO EMPTY LINES ABOVE REQUIRED!
for /f %%a in ('"prompt $H$S &echo on &for %%b in (1) do rem"') do set "BS=%%a"
set $stop=for /L %%n in (1 1 2) do if %%n==2 (%\n%
  ^>nul pause^|set /p "=%BS%!msg:~1!"^&echo(%\n%
  endlocal%\n%
) else setlocal enableDelayedExpansion^&set msg=

::::::::: execute macro "$stop" :::::::::
%$stop% your message

EDIT: You could also work with a variable name to have a better support for special characters.

Code: Select all

@echo off &setlocal enableExtensions disableDelayedExpansion
::::::::: create macro "$stop" :::::::::
set LF=^


set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"&rem TWO EMPTY LINES ABOVE REQUIRED!
for /f %%a in ('"prompt $H$S &echo on &for %%b in (1) do rem"') do set "BS=%%a"
set $stop=for /L %%n in (1 1 2) do if %%n==2 (%\n%
  for /f "tokens=*" %%i in ("!$_msg!") do set "$_msg=!%%i!"%\n%
  ^>nul pause^|set /p "=%BS%!$_msg!"^&echo(%\n%
  endlocal%\n%
) else setlocal enableDelayedExpansion^&set $_msg=

::::::::: execute macro "$stop" :::::::::
set "out=your message!"
%$stop% out

set "out= another message with ^<>|&() "and" prepended & appended spaces "
%$stop% out


Regards
aGerman

Post Reply