Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: checking a user input is empty  (Read 3479 times)

0 Members and 1 Guest are viewing this topic.

mohanprasadgutta

    Topic Starter


    Greenhorn

    checking a user input is empty
    « on: November 05, 2008, 11:15:11 PM »
    Hello,
    I have written this small batch file to ask the username
    It should not allow him to go further if he dont provide username.
    I have written this to do the same.
    but its not working.
    could you please let me know where i made the mistake.

    @echo off
    :uname
    SET /P username=Please enter your username:

    IF %username% == "" GOTO uname
    echo entered username

    Dias de verano

    • Guest
    Re: checking a user input is empty
    « Reply #1 on: November 06, 2008, 12:18:46 AM »
    You made 2 mistakes

    1. in 3rd line there should be quotes around %username%
    2. in 4th  line there should be percent signs around username

    Like this

    @echo off
    :uname
    SET /P username=Please enter your username:
    IF "%username%"=="" GOTO uname
    echo entered %username%

    mohanprasadgutta

      Topic Starter


      Greenhorn

      Re: checking a user input is empty
      « Reply #2 on: November 06, 2008, 01:08:43 AM »
      I have modified the bat file to correct the two mistakes.
      Still it is not prompting me if i dont provide any input. i am just typing enter button.
      Then bat file execution is getting completed. But it should prompt me to enter username.
      I am using Windows 2000.
      <code>
      @echo off
      :uname
      SET /P username=Please enter your username:

      IF "%username%" == "" GOTO uname
      echo entered %username%
      </code>
      You made 2 mistakes

      1. in 3rd line there should be quotes around %username%
      2. in 4th  line there should be percent signs around username

      Like this

      @echo off
      :uname
      SET /P username=Please enter your username:
      IF "%username%"=="" GOTO uname
      echo entered %username%


      fireballs



        Apprentice

      • Code:Terminal
      • Thanked: 3
        Re: checking a user input is empty
        « Reply #3 on: November 06, 2008, 01:22:15 AM »
        you need to reset the %username% vaule otherwise it'll remember what you entered last time:

        Code: [Select]
        set username=
        set /p username=Please enter username:

        FB
        Next time google it.

        mohanprasadgutta

          Topic Starter


          Greenhorn

          Re: checking a user input is empty
          « Reply #4 on: November 06, 2008, 01:26:55 AM »
          Hello,
          Still same problem.
          Have you checked it in your machine.
          thanks in advance.

          you need to reset the %username% vaule otherwise it'll remember what you entered last time:

          Code: [Select]
          set username=
          set /p username=Please enter username:

          FB

          fireballs



            Apprentice

          • Code:Terminal
          • Thanked: 3
            Re: checking a user input is empty
            « Reply #5 on: November 06, 2008, 01:29:37 AM »
            run your script without the
            Code: [Select]
            @echo off at the beginning and the problem will most likely be obvious.

            FB
            Next time google it.

            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: checking a user input is empty
            « Reply #6 on: November 06, 2008, 04:18:22 AM »
            The username variable is defined by the system and is unlikely ever to be blank.

            Try using another variable name:

            Code: [Select]
            @echo off
            setlocal
            :uname
            SET /P uname=Please enter your username:

            IF .%uname%==. GOTO uname
            echo entered %uname%
            endlocal

             8)
            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            mohanprasadgutta

              Topic Starter


              Greenhorn

              Re: checking a user input is empty
              « Reply #7 on: November 06, 2008, 04:31:00 AM »
              thank you very much for your response.
              Its working perfectly.
              But could you please tell what does the . used in if condition, setlocal convays?
              i am beginner to DOS.
              thanks in advance.

              The username variable is defined by the system and is unlikely ever to be blank.

              Try using another variable name:

              Code: [Select]
              @echo off
              setlocal
              :uname
              SET /P uname=Please enter your username:

              IF .%uname%==. GOTO uname
              echo entered %uname%
              endlocal

               8)

              Sidewinder



                Guru

                Thanked: 139
              • Experience: Familiar
              • OS: Windows 10
              Re: checking a user input is empty
              « Reply #8 on: November 06, 2008, 05:32:52 AM »
              Quote
              But could you please tell what does the . used in if condition

              The dot keeps the if condition balanced. If uname is blank, the if condition compares dot to dot. There is no way to directly compare nulls in batch code. Note: the dot is arbitrary, you can use any character; I prefer dots because they are lower case.

              Quote
              setlocal convays

              setlocal and it's evil twin endlocal, restrict the life of variables. In this case uname starts blank with each execution of your code.

               8)
              The true sign of intelligence is not knowledge but imagination.

              -- Albert Einstein

              Dias de verano

              • Guest
              Re: checking a user input is empty
              « Reply #9 on: November 06, 2008, 10:25:59 AM »
              Quote from: Sidewinder
              I prefer dots because they are lower case.

              Don't understand what you mean. Dots are neither lower or upper case. Only the letters of the alphabet have "case".

              Jacob



                Hopeful

                Thanked: 1
                • Experience: Expert
                • OS: Windows XP
                Re: checking a user input is empty
                « Reply #10 on: November 06, 2008, 11:08:08 AM »
                Possibly he means you do not have to hold shift to get them?
                But, I am also unsure of what he means.

                Dias de verano

                • Guest
                Re: checking a user input is empty
                « Reply #11 on: November 06, 2008, 11:58:31 AM »
                Anyway, there are lots of ways to get around the "comparing nulls" problem.


                quotes are most common I think

                if "%variable1%"=="%variable2%"

                square brackets

                if [%variable1%]==[%variable2%]

                Or a single dot...


                gregory



                  Beginner

                  Thanked: 1
                  • Experience: Experienced
                  • OS: Linux variant
                  Re: checking a user input is empty
                  « Reply #12 on: November 09, 2008, 02:45:41 AM »
                  I think using quotes as a rule makes sense, as it also takes care of the chance a variable might contain spaces.

                  Dias de verano

                  • Guest
                  Re: checking a user input is empty
                  « Reply #13 on: November 09, 2008, 02:51:08 AM »
                  I think using quotes as a rule makes sense, as it also takes care of the chance a variable might contain spaces.

                  Indeed.

                  Code: [Select]
                  S:\>set var=hello world

                  S:\>echo %var%
                  hello world

                  S:\>if %var%==hello world echo Yes
                  world==hello was unexpected at this time.

                  S:\>if {%var%}=={hello world} echo Yes
                  world}=={hello was unexpected at this time.

                  S:\>if "%var%"=="hello world" echo Yes
                  Yes

                  S:\>