Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


is there a way to make a batch script display the variable name instea

- - - - - batch variables

  • Please log in to reply
4 replies to this topic


  • Member
  • 2 posts
  • OS:Vista Business x64
  • Country: Country Flag
Hello, i am making a batch script that will install another batch script. It looks like this:
@echo off
echo @echo off > example.bat
echo echo Hi >> example.bat
echo echo 1.) Option1 >> example.bat
echo echo 2.) Option2 >> example.bat
echo echo 3.) Option3 >> example.bat
echo if %selection%==1 goto opt1 >> example.bat
echo if %selection%==2 goto opt2 >> example.bat
echo if %selection%==3 goto opt3 >> example.bat

So it displays all this text and saves what is displayed to example.bat. But it will display variables as their values; instead of making this:

if %selection%==1 goto opt1

it will do this:

if ==1 goto opt1

because theese variables have no value. So, is there a way to tell this script to display variable names instead of values?

also, a pause message is "Press any key to continue..." . Can i change that?

How to remove advertisement from MSFN



    The Finder

  • Developer
  • 14,078 posts
  • OS:none specified
  • Country: Country Flag

Well, what you want to do is to "escape" the percent sign.


Since you are redirecting output to a file, you can "group" lines.

It is not (IMHO) a good idea to use the .bat extension, unless of course you are working on DOS/Win9x/Me the extension for batch files under any NT based system is .cmd.

All in all, I would rewrite your small batch as:

@echo off
echo @echo off
echo echo Hi
echo echo 1.^) Option1
echo echo 2.^) Option2
echo echo 3.^) Option3
echo if %%selection%%==1 goto :opt1
echo if %%selection%%==2 goto :opt2
echo if %%selection%%==3 goto :opt3
)> example.cmd

as it is "cleaner".

Please note that since I enclosed the whole set of ECHO commands in brackets, I had to escape the brackets in the numbered list by using a caret.


You can redirect the output of PAUSE command to nul and ECHO instead something else, like:

ECHO This is a "normal" PAUSE
ECHO This is a "custom" PAUSE
ECHO Please press key "\" to go on






    Update Packrat

  • MSFN Sponsor
  • 1,145 posts
  • OS:XP Pro x64
  • Country: Country Flag


Double each % to escape it.
echo if %%selection%%==1 goto opt1 >> example.bat
echo if %%selection%%==2 goto opt2 >> example.bat
echo if %%selection%%==3 goto opt3 >> example.bat
Hide the PAUSE message with >NUL and ECHO your own above it:
ECHO Your new pause message...
If you're planning to send the PAUSE command to example.bat, you'll need to escape the redirector with a ^.
ECHO ECHO Your new pause message... >> example.bat
ECHO PAUSE ^>NUL >> example.bat

Edited by 5eraph, Yesterday, 04:25 AM.



    K-Mart-ian Legend

  • Super Moderator
  • 9,716 posts
  • OS:Server 2012
  • Country: Country Flag


Topics merged.
MSFN RULES | GimageX HTA for PE 3.x | lol probloms



    Just simple

  • Member
  • PipPipPipPipPipPipPip
  • 1,654 posts
  • OS:Windows 7 x64
  • Country: Country Flag

I like to experiment. A cmd script is just a plain text file so it can be read from. So I considered using FindStr to read the cmd file for lines beginning with a marker ::>. Seems to work ok and avoids using escapes on special characters. The :fileSave label does the reading and echos the matched lines to the new file. The ::> marker should not foul up anything as :: is usually treated as a comment line when it is at the start of the line. Hopefully I added enough comments in the script to help with understanding it. Tested on Windows 7.

@echo off
:: call label :fileSave. 1st parameter is file to read. 2nd parameter is file to save to
call :fileSave %0 example.cmd
goto :eof

:: findstr pattern is:
:: ^ = Match at start of line
:: ::> = Then this literal match

 :: if file exists, then do not continue this label code
 if exist "%~2" (
  echo Error. File already exists 1>&2
  endlocal & goto :eof
 for /f "tokens=*" %%a in ('findstr "^::>" "%~1"') do (
  :: save to a simple variable so string handling can be done on it
  set line=%%a
  :: trim first 3 letters from the line
  set line=!line:~3!
  :: if not defined echo a empty line, else echo the line as is
  if not defined line ( echo.>> "%~2") else echo !line!>> "%~2"
goto :eof

:: Conditions of use of lines read by findstr
:: ------------------------------------------
:: Lines need to start with ::> to be matched by findstr.
:: Empty lines can just start with ::> on the line with no other text needed.
:: Add lines between the REM markers to keep them grouped together.
:: Do not start lines in other parts of the script with ::> else they will be matched by findstr.

REM *** Start of text read by findstr ***
::>:: Script by Pijano
::>@echo off
::>set selection=0
::>echo id Selection
::>echo -- ---------
::>echo 0. Quit
::>echo 1. Option1
::>echo 2. Option2
::>echo 3. Option3
::>set /p "selection=Prompt : "
::>echo You chose %selection%. This outputs to stdout.
::>if %selection% equ 0 goto :eof
::>if %selection% equ 1 goto :opt1
::>if %selection% equ 2 goto :opt2
::>if %selection% equ 3 goto :opt3
::>echo Error. You chose %selection%. This outputs to stdErr. 1>&2
::>call :promptEndOfScript
::>goto :eof
::> echo label :opt1 commands
::> call :promptEndOfScript
::>goto :eof
::> echo label :opt2 commands
::> call :promptEndOfScript
::>goto :eof
::> echo label :opt3 commands
::> call :promptEndOfScript
::>goto :eof
::> echo Script complete. Press any key to continue.
::> pause>nul
::>goto :eof
REM *** End of text read by findstr ***

If you are wondering why I choose to redirect to con with some of the echoes, it prevents what is echoed going to stdout or stderr. So you can redirect the example.cmd to a log file and the menu and other echos redirected to con will not be echoed to file, but echoed to the screen.


Edit: Removed the redirection to con as I do not see it as a benefit for common use. If you redirect the script for errors then you would just do stderr i.e. example.cmd 2> log.txt. Redirecting vital stdout to con can hinder applications or scripts that may want to interact with it by capturing stout and stderr.


Also note that I have comments in the for loop. Sometimes putting comments in between the braces of a for loop or other braced commands do not like comments in there as well. If you have issues, then you can remove those comments. Testing seems fine so it maybe just rem comments that cause issue, not sure which at present.

Edited by MHz, Yesterday, 08:20 PM.

Also tagged with one or more of these keywords: batch, variables

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

How to remove advertisement from MSFN