1

I'm making a game in batch, and I'm using call functions so I can color code text. It works fine, although I cannot use the '?' in them. Any workarounds I can use?

:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i

Thats the color block.

call :colorEcho B " 'Hey, can you come over here, %PlayerName%? I need your help with this new chemical we're working on.' "

What happens:

Picture of the program

I want to keep :colorEcho, because I'm already really far into the game and don't want to restart.

FudgeMuffins
  • 321
  • 2
  • 12
  • One alternative: You can use the free tool `echox.exe` instead, from here: [Shell Script Tools](http://www.westmesatech.com/sst.html). – Bill_Stewart Mar 28 '16 at 23:55
  • Possible duplicate of [How to have multiple colors in a Windows batch file?](http://stackoverflow.com/questions/4339649/how-to-have-multiple-colors-in-a-windows-batch-file) – Wes Larson Mar 28 '16 at 23:58
  • 3
    In the future, please don't use images where they're not needed. You can copy and paste text from a command window and post it here in a block quote or code block. Images are not always easy to read, they're blocked by many corporate proxy servers (meaning that users can't see it), they're almost impossible to read on mobile devices, and they cost mobile users their data (and money). – Ken White Mar 29 '16 at 00:02
  • @KenWhite: The answers deal with handling special characters and multiple colors, which is what the question is about. – Wes Larson Mar 29 '16 at 00:24
  • You can "use '?' in call function in batch": `call :myfunc "A question mark here: ? ..."` and in `:myfunc` put `echo %~1`. I suggest you to edit both the question title and the description in order to match your _real_ problem! @KenWhite: the `colorEcho` function is _not_ mentioned in the title nor in the description, but just as an example... – Aacini Mar 29 '16 at 01:37
  • 1
    The root of the problem is that `?` (and `:` and `*` and others) are not allowed in filenames, and your routine attempts to create a filename "%~2" – Magoo Mar 29 '16 at 01:54
  • @Magoo: yes, and this problem have _no relation_ with the topic title nor the question description... – Aacini Mar 29 '16 at 02:52
  • @KenWhite - `echox.exe` has no problem with the `?` character... – Bill_Stewart Mar 29 '16 at 20:59

1 Answers1

1

I'm using call functions so I can color code text. It works fine, although I cannot use the '?' in them.

From @jeb 's answer here: How to have multiple colors in a Windows batch file?
Use this code block to be able to handle special characters, including !<>&| %"*?. Exclamation marks still have to be handled differently, but this shows how.

@echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)
rem Prepare a file "X" with only one dot
<nul > X set /p ".=."

call :colorEcho 0B " 'Hey, can you come over here? I need your help ...' "
echo(
call :colorEcho 0A " 'Yo" &call :colorEcho 0A ^^^^^^^! &call :colorEcho 0A " What's up?' "
echo(
call :colorEcho 0B " 'Oh $#><* that hurts" &call :colorEcho 0A ^^^^^^^!' "
echo(
call :colorEcho 0C "  ^!<>&|%%%%"*?"

del X
goto :eof

:colorEcho
set "param=^%~2" !
set "param=!param:"=\"!"
findstr /p /A:%1 "." "!param!\..\X" nul
<nul set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
exit /b
Community
  • 1
  • 1
Wes Larson
  • 1,042
  • 8
  • 17