0

I have created a batch file as below

@echo off
echo  Type Below Requirements:
echo.
:username
set /p usr= Type Username:
if %usr%==[%1]==[] goto username
echo Your username is: %usr%
pause

This is working perfectly when I am typing any text, but if I type " batch is Exit Automatically.

Bas van Dijk
  • 9,933
  • 10
  • 55
  • 91
Adhil 1989
  • 25
  • 4

2 Answers2

2

Here is another way:

@echo off
echo  Type Below Requirements:
echo.
:username
set "usr="
set /p "usr= Type Username: "
if not defined usr goto :username
echo Your username is: %usr%
pause
foxidrive
  • 40,353
  • 10
  • 53
  • 68
0

Firstly, the comparison expression syntax if %usr%==[%1]==[] is wrong, it should read if [%usr%]==[] instead.

Secondly, to handle double-quotes correctly, you will need to enable delayed expansion.

The following should work:

@echo off
setlocal EnableDelayedExpansion
echo  Type Below Requirements:
echo.
:username
set usr=
set /p usr= Type Username:
if [!usr!]==[] goto username
echo Your username is: !usr!
pause
Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Is it Possible to restrict by typing space and symbols, ex: "test username" and "test@username" should not allowed... – Adhil 1989 Sep 05 '15 at 05:34
  • In above batch file,Is it Possible to give a alert, ex: You Input username in invalid if type "space or symbol", and for the password it should match password Policy... – Adhil 1989 Sep 05 '15 at 07:06
  • Yes: a good starting point for that is string replacement expansion like `%usr:@=%` (see `set /?` for help); compare it with `%usr%` using `if`; if the strings do not equal, one or more `@` are present... – aschipfl Sep 06 '15 at 08:16
  • If you can Please Show me a sample code, I did not get what you explained to me... Thanks, – Adhil 1989 Sep 06 '15 at 15:51
  • For example, you want to check whether the value of `%usr%` contains any spaces, you could do this: `if "%usr: =%"=="%usr%" echo No spaces encountered.`; the first expansion uses string replacement (see `set /?` for details) where spaces are replaced by nothing, so if there are spaces, the comparison fails; – aschipfl Sep 09 '15 at 22:28