1.

Solve : Handle ampersand "&" inputted by user in batch file.?

Answer»

I have a batch file that takes user input and places it in a variable. The problem is that sometimes the user may enter an "&" symbol which then causes the script to TRY to process the string after the & as a command.  I need the script to take the ampersand and place it variable string and then convert it to url encoding. For example:

User is prompted: What is your company NAME?
User inputs: You & Me
variable1= You & Me
variable2= You%26Me

Is this possible?

Code: [Select]echo off &setlocal enabledelayedexpansion
set /p "variable1=Enter Input:"

set "variable2=!variable1:&=%%26!"
echo %variable2%output
Code: [Select]C:\Batch\Ampersand>Ampersand.bat
Enter Input:You & Me
You %26 MeI see you posted an almost similar question in the Computer Programming category.  The code is basically the same as what Salmon Trout had posted for you for the spaces. Not sure why you didn't attempt to modify his code for the Ampersand replacement.To the OP, if it's URL encoded then the spaces also need encoding in your example.

Code: [Select]echo off &setlocal enabledelayedexpansion
set /p "variable1=Enter Input:"
set "variable2=!variable1:&=%%26!"
set "variable2=!variable2: =%%20!"
echo "%variable2%" Quote from: Squashman on January 06, 2015, 08:36:26 PM

I see you posted an almost similar question in the Computer Programming category.  The code is basically the same as what Salmon Trout had posted for you for the spaces. Not sure why you didn't attempt to modify his code for the Ampersand replacement.

I actually did, but with the code:
Code: [Select]echo off & setlocal enabledelayedexpansion
set /p "variable1=Enter Input:"
set "variable2=!variable1:&=%%26!"
set "variable3=!variable2: =%%20!"
set "variable4=!variable3:,=%%2c!"
echo %variable1%
echo %variable2%
echo %variable3%
echo %variable4%
pause

I get the output:
Code: [Select]Enter Input:you, me, & them
you, me,
'them' is not recognized as an internal or external command,
operable program or batch file.
you, me, %26 them
you,%20me,%20%26%20them
you%2c%20me%2c%20%26%20them
Press any key to continue . . .
Because the input has an Ampersand you either need to enclose it in quotes or use Delayed EXPANSION to ECHO the contents.
Code: [Select]echo off & setlocal enabledelayedexpansion
set /p "variable1=Enter Input:"
set "variable2=!variable1:&=%%26!"
set "variable3=!variable2: =%%20!"
set "variable4=!variable3:,=%%2c!"
echo %variable1%
echo "%variable1%"
echo !variable1!
echo %variable2%
echo %variable3%
echo %variable4%
pauseoutput
Code: [Select]Enter Input:you, me, & them
you, me,
'them' is not recognized as an internal or external command,
operable program or batch file.
"you, me, & them"
you, me, & them
you, me, %26 them
you,%20me,%20%26%20them
you%2c%20me%2c%20%26%20them
Press any key to continue . . .And there is also the SET /P trick, which doesn't require the use of Delayed Expansion.
Code: [Select]echo off & setlocal enabledelayedexpansion
set /p "variable1=Enter Input:"
set "variable2=!variable1:&=%%26!"
set "variable3=!variable2: =%%20!"
set "variable4=!variable3:,=%%2c!"
set /p ".=%variable1%"<nul
echo/
echo %variable2%
echo %variable3%
echo %variable4%
pauseoutput
Code: [Select]Enter Input:you, me, & them
you, me, & them
you, me, %26 them
you,%20me,%20%26%20them
you%2c%20me%2c%20%26%20them
Press any key to continue . . .


Discussion

No Comment Found