Echoing a variable in Batch is not working? - batch-file

I am currently trying to echo the text for a text based adventure game in Batch. My code looks like this:
...
echo.
echo Hello Adventurer.
echo.
echo Please tell us your name:
set /p pass =
echo.
...
echo You will be searching for the Golden Egg %pass% . You are the des...
It's not working, can anyone help me out here?

Remove the space around the =, i.e.
set /p pass=
(Currently your variable is actually called %pass %)

Related

Why does batch script keep exiting after set /P user prompt?

#echo off
cls
echo Hello, there! Today is
date /t
echo and the current time is
time /t
echo I'm so glad to meet you. My name is Hal 10000.
set /p name=What's your name?
cls
echo Well hello, %name%
echo Hey, %name%, & set /p "age"="how old are you in years?"
echo Well that's just great! I'm actually 115 today.
echo I look pretty good, huh!?
echo Well, gotta go, by!
exit
Looks like you need a pause command. Your code is executing correctly but there is nothing stopping the code from exiting before you see your results.
#Echo off
Echo This message will not be displayed.
cls
Echo This message will be displayed.
pause
exit
Adding a pause before the exit will show your results until a button is pressed.

Batch-File Stops at first Line or Ends up in Loop

I have a little problem I don't understand. First off I am a Rookie and never done Batch-Scripting before. I made two scripts for my company including the command nslookup
First script: in this script the nslookup command shall print me out the results of a specific ip range, but after "echo NSLOOKUP" nothing is displayed" The for-loop doesn't execute. But it worked last week with the same code:
#echo off
for /L %%x IN (33, 1, 60) DO (
echo _______________________
echo NS LOOKUP
nslookup 192.168.178.%%x
)
pause
Second script: Here it's a bit different. I can enter the name for the domain, but after that it doesnt execute the nslookup-command aswell
#echo off
set input=y
:while
if %input%==n (
exit
)
echo _______________
echo Name for nslookup
set /p name=
nslookup %name%.domain.domain.domain.de
echo _______________
echo Search another one? (y/n)
set /p input=
goto while
Also whenever I make a new batch-file and typing something like:
#echo off
echo tell me your name
set /p name=
echo tell me your age
set /p age=
echo %name%
echo %age%
pause
It just end in a loop with an input of name and age.
I am confused. Please help me. I'd appreciate it. Thank you in advance.

Randomize batch variables

I am trying to make a Yahtzee game and I need to randomize number 1-6 in order to make it echo the specific dice face.
:gameplay
cls
pause
SET /A dice=%RANDOM% %%6+1
if %dice%==1
echo %dice1a%
echo %dice1b%
echo %dice1c%
echo %dice1d%
echo %dice1e%
echo %dice1g%
pause
This is the code I developed. Its not completed yet, this was just a test of the randomizer. Every time I run this, it gets to the first pause, then it quits the program. If you guys know what the problem is, can you tell me, and also I was wondering if it was possible to store multiline variable so I don't have to constantly echo multiple variables. If you guys know anything please tell me. Thanks
EDIT:
I tried what you suggested and did this:
:gameplay
cls
pause
SET /A dice=%RANDOM% %%6+1
if %dice%=1
echo Hi
pause
echo %dice1a%
echo %dice1b%
echo %dice1c%
echo %dice1d%
echo %dice1e%
echo %dice1g%
pause
It didn't follow through to the 2nd pause so I think the variable isn't set properly. I barely noticed this time that it says invalid command syntax right before it quits. Thanks for the suggestion though.
Thanks guys I figured it out, I just was missing the double and quadruple spaces as well as the parenthesis in the if then statement.
SET /A dice=%RANDOM% %%6 +1
if %dice%==1 (
echo %dice1a%
echo %dice1b%
echo %dice1c%
echo %dice1d%
echo %dice1e%
echo %dice1f%
echo %dice1g%
)
I really appreciate everyone who attempted to help me, even if it was just me being stupid, and I really appreciate yacc for helping me the whole way. If you want my game once its finished, I will copy it on to the about me section.

Spaces In Text Document Name Through Batch User Input

I am trying to make an "AI". It functions by making a file and naming it after the user input, then asking what say when the input is put in again. But, when you add a space, It names it just after the first word, and doesn't allow it to be called up next time!
Here Is the part that learns from user input:
:learn
cls
echo I don't know anything about "%cat%". Can you tell me what you know.
echo.
set /p catinfo=Tell me what you know:
echo %catinfo% >> brain\%cat%.txt
set txt=That's cool, now I know.
(
echo %name%
) > vf2.txt
set txt=Cool now I know about %cat%.
cls
goto startup
And this is what reads the text document:
:learned
(
set /p catinfo=
) < brain\%cat%.txt
set txt=%catinfo%
color e
echo %catinfo%
%speech% "%catinfo%"
color a
PING 1.1.1.1 -n 1 -w 3000>nul
cls
goto startup
I just wanted the user to be able to use spaces to talk to the computer. Sorry about this probably pretty simple question, but I would like it to be more streamline than using underscores.
Thanks!
-Simon

Setting User account "Full Name" and "Description" from within a batch using variables

I am attempting to write a batch script that adds new users to a machine using user prompts to get the information. I am at the point where the user gets created correctly, but the "Full Name" and "Description" fields don't populate, no matter what I try. I can't seem to get it to pass the %fullname% variable into either the net user command or the wmic command. If I just type quoted text into either spot it adds the information properly. Also if there's a better way to do echos like that it would be appreciated, takes up too many lines IMO. :)
:User
echo.
echo Please Input Username...
echo.
set /p username=Username:
echo.
echo Please Confirm Username...
echo.
set /p confirm=Username:
if '%username%' == '%confirm%' (
echo.
echo Please Input User Full Name...
echo.
set /p fullname=Full Name:
echo.
echo Please Input User Description...
echo.
set /p description=Description:
echo Creating %username%...
net user %username% * /add /active:yes /fullname:%fullname% /comment:%description% /passwordchg:no /expires:never
wmic useraccount where "Name='%username%'" set PasswordExpires=FALSE
wmic useraccount where "Name='%username%'" set FullName=%fullname%
This script creates the user just fine, but the fullname and comment fields are blank.
Problem solved. Apparently the parentheses for my if statement were messing everything up. The fullname and description variables were blank for whatever reason. I guess you can't do a set inside an if statement?
No matter, changing to this has solved the problem and it works perfectly now.
:User
echo.
echo Please Input Username...
echo.
set /p username=Username:
echo.
echo Please Confirm Username...
echo.
set /p confirm=Username:
if '%username%' == '%confirm%' goto User1
goto User
:User1
echo.
echo Please Input User Full Name...
echo.
set /p fullname=Full Name:
echo.
echo Please Input User Description...
echo.
set /p description=Description:
pause
echo Creating %username%...
net user %username% * /add /active:yes /fullname:"%fullname%" /comment:"%description%" /passwordchg:no /expires:never
wmic useraccount where "Name='%username%'" set PasswordExpires=FALSE

Resources