Peter
Peter

Reputation: 5121

Batch Script inserts un-needed spaces padding special character

I have a very simple script that inputs a user name and password. Some passwords contain special characters such as:!,@,#,$,^,&,*,(,),_. The issue is when they enter one of these characters it pads the character with blank spaces on either side inside the variable.

Here is my code and an example:

echo Enter UserName:
set/p user=
echo Enter Password:
set/p pass=
echo %pass%
net use R: \\serverhere /USER:%user% %pass%

if I entered: Test!Password for my password, it would output: Test ! Password for %pass% in the net use command which causes issues

How can I fix this?

Upvotes: 1

Views: 318

Answers (1)

jeb
jeb

Reputation: 82317

I don't believe that there are problems with injected spaces, but you get problems with special characters at all.

So the best way is to use delayed expansion, as it doesn't care about special characters.

setlocal EnableDelayedExpansion
echo Enter UserName:
set/p user=
echo Enter Password:
set/p pass=
echo !pass!
net use R: \\serverhere /USER:!user! !pass!

Upvotes: 2

Related Questions