Question on adding Input to line of pre-built text

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ulricken
Posts: 5
Joined: 05 Aug 2017 13:10

Question on adding Input to line of pre-built text

#1 Post by ulricken » 05 Aug 2017 13:16

Hello All,

OS: Windows 7 Ultimate 64-bit

I am trying to create a batch file that will do the following.

@echo off

SET /P UserInput=Please type the Number:

You owe us an amount of !UserInput!
pause

But I get the following as the result
Please type the Number:12
"You owe us an amount of 34.!UserInput!"
Press any key to continue . . .

I feel the answer is simple and easy, but google doesn't want to be a friend right now lol.

Any help(or just the right direction at least) would be great!

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Question on adding Input to line of pre-built text

#2 Post by ShadowThief » 05 Aug 2017 14:40

Is that your entire script? Are you running it from the command prompt or double clicking it? Exactly what are you entering as input?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Question on adding Input to line of pre-built text

#3 Post by ShadowThief » 05 Aug 2017 14:41

Also, you're using !UserInput! instead of %UserInput% but I'm not seeing a setlocal enabledelayedexpansion anywhere.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Question on adding Input to line of pre-built text

#4 Post by aGerman » 05 Aug 2017 14:44

Two things are missing.
1) If you want to output something use the ECHO command.
2) If you want to expand variables enclosed in exclamation marks you need to enable delayed variable expansion.

Code: Select all

@echo off

SET /P UserInput=Please type the Number:

setlocal EnableDelayedExpansion
echo You owe us an amount of !UserInput!
endlocal

pause

Steffen

ulricken
Posts: 5
Joined: 05 Aug 2017 13:10

Re: Question on adding Input to line of pre-built text

#5 Post by ulricken » 05 Aug 2017 14:46

ShadowThief wrote:Is that your entire script? Are you running it from the command prompt or double clicking it? Exactly what are you entering as input?


Yes, it is. I know a beginner lol.

I actually figured out that I should have been using %UserInput% instead of !UserInput!.

It was a numeric value as Input(i.e. 36, 12, 02, etc)

ulricken
Posts: 5
Joined: 05 Aug 2017 13:10

Re: Question on adding Input to line of pre-built text

#6 Post by ulricken » 05 Aug 2017 14:48

ShadowThief wrote:Also, you're using !UserInput! instead of %UserInput% but I'm not seeing a setlocal enabledelayedexpansion anywhere.


I just found out about the %UserInput% prior to seeing this.. I guess I was wording it weirdly with Google on trying to find the answer.

Honestly I'm not sure what the setlocal enabledelayedexpansion does. I've never heard or seen this before.

I'll do some research on this.

ulricken
Posts: 5
Joined: 05 Aug 2017 13:10

Re: Question on adding Input to line of pre-built text

#7 Post by ulricken » 05 Aug 2017 14:50

aGerman wrote:Two things are missing.
1) If you want to output something use the ECHO command.
2) If you want to expand variables enclosed in exclamation marks you need to enable delayed variable expansion.

Code: Select all

@echo off

SET /P UserInput=Please type the Number:

setlocal EnableDelayedExpansion
echo You owe us an amount of !UserInput!
endlocal

pause

Steffen



HAHA, Rookie move on the echo. I had that in script(I found out that I didn't have the echo right after posting this). As I stated for the person in the previous post, I'm not sure what the "setlocal EnableDelayedExpansion" is or does, but I will research it.

ulricken
Posts: 5
Joined: 05 Aug 2017 13:10

Re: Question on adding Input to line of pre-built text

#8 Post by ulricken » 05 Aug 2017 14:51

Thank you all for the information.

I know this is a rookie question/script, but we all got to start somewhere.

I will be doing some research on the setlocal EnableDelayedExpansion.

As for now, I feel like this question is answered.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Question on adding Input to line of pre-built text

#9 Post by aGerman » 05 Aug 2017 16:35

Normally variables will be expanded using percent signs. This will work for you as well. Just write %UserInput% instead of !UserInput!

Delayed variable expansion is needed if you want to expand a variable that was changed in the same command line or an (in parentheses enclosed) block of command lines. E.g.

Code: Select all

@echo off
set "n=0"
echo before: %n%
for /l %%i in (1 1 5) do (
  set "n=%%i"
  echo %n%
)
echo after: %n%
pause
output:

Code: Select all

before: 0
0
0
0
0
0
after: 5

As you can see the variable was changed but inside of the block it was not expanded to the new value. The reason is that variables are already expanded to their values before the command line (or block) is executed. To avoid this "early" expansion you need to enable the delayed variable expansion and you have to enclose the variable name into exclamation marks.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "n=0"
echo before: %n%
for /l %%i in (1 1 5) do (
  set "n=%%i"
  echo !n!
)
echo after: %n%
pause
output:

Code: Select all

before: 0
1
2
3
4
5
after: 5


But delayed expansion has another advantage that may come in handy in your case (because users enter funny things :wink:). That is, if the content of a variable contains characters with a special meaning in batch, it can be savely outputted as literal expression.
Test:

Code: Select all

@echo off
set "var=a&b"

echo %var%
echo ~~~~~~~~~~

setlocal EnableDelayedExpansion
echo !var!
echo ~~~~~~~~~~

pause

Steffen

Post Reply