Jump to content

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


Recommended Posts

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?

p.s.

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

Link to comment
Share on other sites


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

http://www.robvanderwoude.com/escapechars.php

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 offecho echo Hiecho echo 1.^) Option1echo echo 2.^) Option2echo echo 3.^) Option3echo if %%selection%%==1 goto :opt1echo if %%selection%%==2 goto :opt2echo 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 OFFECHO This is a "normal" PAUSEPAUSEECHO This is a "custom" PAUSEECHO.ECHO Please press key "\" to go onPAUSE>NUL

http://www.robvanderwoude.com/redirection.php

 

jaclaz

Link to comment
Share on other sites

Double each % to escape it.

echo if %%selection%%==1 goto opt1 >> example.batecho if %%selection%%==2 goto opt2 >> example.batecho if %%selection%%==3 goto opt3 >> example.bat
Hide the PAUSE message with >NUL and ECHO your own above it:

ECHO Your new pause message...PAUSE >NUL
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.batECHO PAUSE ^>NUL >> example.bat
Edited by 5eraph
Link to comment
Share on other sites

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 tocall :fileSave %0 example.cmdgoto :eof:: findstr pattern is::: ^ = Match at start of line:: ::> = Then this literal match:fileSave setlocal ENABLEDELAYEDEXPANSION :: 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" ) endlocalgoto :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::>cls::>::>set selection=0::>echo id Selection::>echo -- ---------::>echo 0. Quit::>echo 1. Option1::>echo 2. Option2::>echo 3. Option3::>echo.::>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::>::>:opt1::> echo label :opt1 commands::> call :promptEndOfScript::>goto :eof::>::>:opt2::> echo label :opt2 commands::> call :promptEndOfScript::>goto :eof::>::>:opt3::> echo label :opt3 commands::> call :promptEndOfScript::>goto :eof::>::>:promptEndOfScript::> echo Script complete. Press any key to continue.::> pause>nul::>goto :eofREM *** 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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...