Passing more than 9 parameters to a batch file

It is of a common practice to write a batch file to execute the commands using some command line utilities like OSQL. And to provide the required parameters to that command line utility, we just set some variable in the command file and pass them as parameters to the batch file containing the commad to be executed.

Suppose we have a batch file which executes a OSQL command by taking few parameters like ServerName, Database name, Username, password etc. So we just run that batch file probably by providing all the required parameters as:

MyBatchFile.bat computername SQLUser SQLUSERPWD DBName

And in the batch file we make use of these parameters by their order in which they are being passed as:

osql -U%2 -P%3 -S%1 -d%4

This works absolutely fine till we have total only upto 9 (nine) parameters to be passed to the batch file and to be used in that file. But if we pass more than nine parameters and try to use them in the command as %10 oR %11 ..so on, it would not work. %10  will be considered as FirstParameter0 & %11 as SecondParameter1 i.e. if %1 = MYMACHINE, then %10 will be = MYMACHINE0.

So to have more than 9 parameters in the batch file, we make use of SHIFT function as:

SET ONE=%~1
SET TWO=%~2
SET THREE=%~3
SET FOUR=%~4
SET FIVE=%~5
SET SIX=%~6
SET SEVEN=%~7
SET EIGHT=%~8
SET NINE=%~9
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SET TEN=%~1
SET ELEVEN=%~2

:In this way we can move to any index of the parameters list, It will start from the first parameter and then by shifting it to right direction we can reach upto the last parameter. And then in the command we can use them as:

osql -U%TWO% -P%THREE% -S%ONE% -d%FOUR% %TEN%…….

Advertisements
This entry was posted in Others. Bookmark the permalink.

16 Responses to Passing more than 9 parameters to a batch file

  1. Anil says:

    Hi,

    How can i take data from event viwer to txt file in bat pro.

  2. Geetha says:

    Hi,

    Thanks a lot for your article, its been of a lot of help.
    I appreciate it.

    Thanks,
    geeta

  3. Suraj says:

    Thank you for the code. This just solved my last 2 hours headache 🙂
    cheers
    Suraj

  4. Julian says:

    Thanks a lot…

  5. sanjeev panchal says:

    hey
    thx a lot it solved my prob

  6. Dave Comeau says:

    Thank you thank you. Just what I needed!

  7. Nitesh says:

    Thanks a lot for you post. It made my day.

  8. yeshwanth says:

    your solution is good but it is not taking the empty values when I give as argument

    for ex: when I pass the below arguments

    e547003 e547003 Design.SDS91 Author 000874 “A” “000874/A-Testdataset” “DirectModel” “000048.jt” “” “C:\temp”

    it is not taking the empty parameter before C:\temp

  9. Uday says:

    Thanks a lot. It really helped.

  10. Prashant says:

    Thanks Buddy.. That really helped..

  11. Vivek says:

    Why did not i open your blog before.
    Relief from headache.
    Thanks 🙂

  12. vijay says:

    Thank u so much for your Blog

  13. hassene says:

    Thanks a lot. It really helped.

  14. John says:

    Many thanks. It works for me.

  15. franko says:

    4/22/2015 – I had high hopes, but alas, this fails completely in Windows 7 HP. Get message ‘invalid param to shift command’, then a list of all param names with tag ‘no such file or directory’. And of course, the called program fails. (called program, gfortran, works fine from cmd line with many params.)

  16. franko says:

    Well, now I got it to work. But, I had to elminate the ’tilde’ in for example ‘% ~1′. I am passing filenames which exist in the current directory. I think the ’tilde’ gets a pathname. Anyway, it works. Thanks very much!
    franko

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s