Question on adding Input to line of pre-built text
Moderator: DosItHelp
Question on adding Input to line of pre-built text
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!
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!
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Question on adding Input to line of pre-built text
Is that your entire script? Are you running it from the command prompt or double clicking it? Exactly what are you entering as input?
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Question on adding Input to line of pre-built text
Also, you're using !UserInput! instead of %UserInput% but I'm not seeing a setlocal enabledelayedexpansion anywhere.
Re: Question on adding Input to line of pre-built text
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.
Steffen
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
Re: Question on adding Input to line of pre-built text
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)
Re: Question on adding Input to line of pre-built text
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.
Re: Question on adding Input to line of pre-built text
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.
Re: Question on adding Input to line of pre-built text
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.
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.
Re: Question on adding Input to line of pre-built text
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.output:
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.output:
But delayed expansion has another advantage that may come in handy in your case (because users enter funny things ). 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:
Steffen
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
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
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 ). 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