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

Author Topic: Parameters at command line question  (Read 3464 times)

0 Members and 1 Guest are viewing this topic.

campione

    Topic Starter


    Greenhorn

    Parameters at command line question
    « on: August 27, 2008, 11:28:46 AM »
    I know I can take parameters at command line like:

    file.bat param1 param2 etc

    and have them read in file.bat like:

    %1 %2 etc

    but is there a way to pass parameters using options, i.e.:

    file.bat -pOne param1 -pTwo -param2 etc

    and have them read in file.bat, i.e. get value of pOne and pTwo?

    Any help appreciated

    fireballs



      Apprentice

    • Code:Terminal
    • Thanked: 3
      Re: Parameters at command line question
      « Reply #1 on: August 27, 2008, 11:44:22 AM »
      if your asking what i think then you can just add

      Code: [Select]
      set Pone=%1% which will then set your first parameter to "Pone".

      on second thoughts that doesn't seem to be what your asking but if it is hope it works. If i get you right this time you want to pass a parameter to the batch file that tells the batch file to get a value?

      this can be done but it depends where this value is in the first place.

      FB
      « Last Edit: August 27, 2008, 12:02:07 PM by fireballs »
      Next time google it.

      Dias de verano

      • Guest
      Re: Parameters at command line question
      « Reply #2 on: August 27, 2008, 01:00:09 PM »
      fireballs, you had one % sign too many. To set Pone to the value of the first parameter you would use

      set Pone=%1

      « Last Edit: August 27, 2008, 03:32:41 PM by Dias de verano »

      campione

        Topic Starter


        Greenhorn

        Re: Parameters at command line question
        « Reply #3 on: August 27, 2008, 01:14:51 PM »

        No, what I'm thinking of is a way of kinda setting variables as they are passed, for example:

        Code: [Select]
        file.bat -user myUserName -password myPassword
        would allow me to access the -user value that was passed, i.e. userName and the -password, i.e. myPassword.

        I suppose the whole point of the exercise is to be able to swap around the options, such as:

        Code: [Select]
        file.bat -password myPassword -user myUserName
        but that this command would still mean the same as the previous command.

        campione

          Topic Starter


          Greenhorn

          Re: Parameters at command line question
          « Reply #4 on: August 27, 2008, 01:25:43 PM »

          Actually, just thinking there, I could check each parameter, e.g.:

          Code: [Select]
          while (input parameters)(
          if %1 == -user
          (
           do something with %2
          )

          if %1 == -password
          (
           do something with %2
          )


          check that %3 is -password if %1 -user
          OR
          check that %3 is -user if %1 -password

          do something with %4
          )

          Excuse the pseudocode.

          Not as clean as I would have liked but still should do the job.

          fireballs



            Apprentice

          • Code:Terminal
          • Thanked: 3
            Re: Parameters at command line question
            « Reply #5 on: August 27, 2008, 01:28:47 PM »
            fireballs, you had on % sign too many. To set Pone to the value of the first parameter you would use

            set Pone=%1

            It works either way for me.

            you could have:

            Code: [Select]
            if %%1=="-password" goto password
            if %%1="-username" goto username
            exit

            :username
            if not %%2=="My Username" (exit) else (
            if not %%4=="My Password" exit
            )
            goto code

            :password
            if not %%2=="My Password" (exit) else (
            if not %%4=="My Username" exit
            )
            goto code

            is that what your looking for?

            EDIT: i think we had the same idea, you posted as i was writing a response.

            FB
            Next time google it.

            campione

              Topic Starter


              Greenhorn

              Re: Parameters at command line question
              « Reply #6 on: August 27, 2008, 01:31:18 PM »
              That's the one, thought there might have been an actual command.

              Thanks.

              Dias de verano

              • Guest
              Re: Parameters at command line question
              « Reply #7 on: August 27, 2008, 01:32:27 PM »
              While I was posting a reply, I saw the red "While you were typing a new reply has been posted" warning.

              If you think about it logically, you only have 2 possibilities for the parameter sequence

              (a) %1 -user %2 username %3 -password %4 password
              (b) %1 -password %2 password %3 -user %4 username

              So if you look at %1 you will find out all you need to know

              Code: [Select]
              if "%1"=="-user" (
                  set username=%2
                  set password=%4
                  ) else (
                  set password=%2
                  set username=%4
                  )


              campione

                Topic Starter


                Greenhorn

                Re: Parameters at command line question
                « Reply #8 on: August 27, 2008, 02:13:34 PM »

                I suppose my thinking was that using %1 - %9 can be limiting, mainly due to the amount of parameters that can be passed.

                Dias de verano

                • Guest
                Re: Parameters at command line question
                « Reply #9 on: August 27, 2008, 02:42:51 PM »
                You can pass any number of parameters, you aren't limited to 9, as you will see if you study the SHIFT command.

                Also, the %* parameter expands to a string made up of ***all*** of the parameters passed.


                campione

                  Topic Starter


                  Greenhorn

                  Re: Parameters at command line question
                  « Reply #10 on: August 28, 2008, 01:48:07 AM »

                  I see, so the SHIFT command will keep shifting through parameters, so I could have a large amount of them.

                  Nice one.