just questions

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
benneburger
Posts: 6
Joined: 26 Nov 2011 11:05

just questions

#1 Post by benneburger » 26 Nov 2011 20:51

I'm trying to make a game using DOS code, notepad, and a .bat extension.

How do I prevent the program from closing when the the user types in a space?

How do I set a single variable to echo something on multiple lines? Like this...

variable=(
echo blah blah
echo yadda yadda
)

And how do I use the goto command to go to a certain line?

Cat
Posts: 32
Joined: 11 Nov 2011 12:04

Re: just questions

#2 Post by Cat » 26 Nov 2011 22:38

some examples:

Code: Select all

set cat=13
set dog=7
echo %dog%
echo %cat%

would output:

Code: Select all

13
7

to use GOTO, use labels.

Code: Select all

:loop
echo Hello world
pause
goto loop

would output:

Code: Select all

Hello World
Press any key to continue...

benneburger
Posts: 6
Joined: 26 Nov 2011 11:05

Re: just questions

#3 Post by benneburger » 27 Nov 2011 12:07

Thank you. But whenever I use "set/p input" and type in a space, the window closes. why?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: just questions

#4 Post by Ed Dyreen » 27 Nov 2011 12:15

'

Code: Select all

@echo off

set /p "$input=input :"
echo.$input=%$input%_
pause

exit /b 0

benneburger
Posts: 6
Joined: 26 Nov 2011 11:05

Re: just questions

#5 Post by benneburger » 27 Nov 2011 13:54

Okay. Now I'm trying to set "choice1" to "look around" (no quotes.)

I keep getting an error and then it closes.

benneburger
Posts: 6
Joined: 26 Nov 2011 11:05

Re: just questions

#6 Post by benneburger » 27 Nov 2011 14:12

actually, nm.

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: just questions

#7 Post by Rileyh » 27 Nov 2011 18:43

QUESTION 1:PART 1: I can see that you are a beginner, so I will phrase this simply:
VARIABLES WITH PAUSING=

Code: Select all

set /p (random character that you want to represent your variable)=(something the user will see)

So if you want the user to type in something, you can use set /p to get them to type in something without the file closing suddenly.
For example:

Code: Select all

set /p a=Enter a number:

It will freeze until the user types in something, and cmd will set what the user types to the value of the variable.
PART 2: You could use the "pause" command to stop it closing until the user presses any key:

Code: Select all

pause

Or if you don't want it to show up as "press any key to continue...":

Code: Select all

pause >nul


QUESTION 2: You could do this:

Code: Select all

set "a=(text. But make sure that whatever goes here is on the amount of lines that you want.)"
REM Such as set "a= echo hello (
REM) echo hello world (
REM ) echo hello world again"
%a%


QUESTION 3: You need to set tag/s at certain point/s in a batch file. For example:

Code: Select all

REM place a ":"  before a letter or word to indicate that it is a tag.
:a
echo Hi
pause >nul
goto :a
REM The program will jump to :a. This simple batch file is called a loop.


This should answer your questions.

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

Re: just questions

#8 Post by orange_batch » 29 Nov 2011 03:55

benneburger wrote:How do I prevent the program from closing when the the user types in a space?

You need to watch the handling of special characters in batch. Characters within quotation marks are "deactivated", and your script needs to prepare for blank variables (or those containing only a space like what you're trying). & | % ! to name a few can all cause problems.

The problem you are most likely having:

Code: Select all

set /p "var=Enter Number: "
if %var%==1 (echo:Var is 1.) else echo:Var is 2.

If the user enters a space, it reads

Code: Select all

if  ==1

That's going to cause a syntax error. Run your scripts from an open command line so you can see the errors it outputs. Solution, as I said:

Code: Select all

set /p "var=Enter Number: "
if "%var%"=="1" (echo:Var is 1.) else echo:Var is 2.

benneburger wrote:How do I set a single variable to echo something on multiple lines?

Code: Select all

set "var=echo:Hello&echo:World&echo:Orange"
:: Or...
set var=echo:Hello^&echo:World^&echo:Orange
%var%

^ (caret) is the escape character in batch. & joins multiple commands on the same line. If it's not within quotation marks, it will cause an error without an escape caret because command prompt will interpret it as-is.

Code: Select all

set var=echo:Hello&echo:World&echo:Orange

Will read as...

Code: Select all

set var=echo:Hello
echo:World
echo:Orange

benneburger wrote:And how do I use the goto command to go to a certain line?

Make a label where you want to go, then write goto followed by that label's name.

Code: Select all

:start
echo:Hello
goto start

If you mean a specific line number, that's complicated and beyond your skill level, and also just a terrible and pointless idea anyway, because as soon as you add a new line between somewhere, every reference would be broken.

benneburger
Posts: 6
Joined: 26 Nov 2011 11:05

Re: just questions (SOLVED)

#9 Post by benneburger » 29 Nov 2011 18:50

Through thorough experimentation that stretched my 15 yr old mind to its limits, I have solved my problem using this code:

set /p "$input= "
set $input="%$input%"

With this, the user can enter as many words and spaces as they desire with no issue.

I'm so proud of myself, I can almost jump for joy!

benneburger
Posts: 6
Joined: 26 Nov 2011 11:05

Re: just questions

#10 Post by benneburger » 29 Nov 2011 18:51

Thank you all. I hereby declare this thread done.

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

Re: just questions

#11 Post by orange_batch » 01 Dec 2011 00:56

Yes, that's one way of being able to use your %$input% variable without continually having to place it between quotation marks. Just watch your handling because now those quotation marks are part of that variable, and you'll need to write %$input:~1,-1% to remove them without modifying the variable. For longer scripts you should keep note of such things. It's a little complicated for novices. :o

Post Reply