I need to be able to pass parameters to a windows batch file BY NAME (and NOT by order). My purpose here is to give end user the flexibility to pass parameters in any order, and the batch file should still be able to process them.
An example to make my question clearer:
in the command line, user does the following:
somebatchfile.bat originalFile.txt newFile.txt
Inside somebatchfile.bat there is a simple statement to copy the contents of original file (first parameter %1%) to the new file (second parameter %2%). It could be as simple as the following statement:
copy %1% %2%
Now, if user passes the above parameters in reverse order, the result will be far from desirable (very WRONG in fact).
So, is there a way for user to pass parameters by name: e.g. somebatchfile.bat "SOURC=originalFile.txt" "TARGET=newFile.txt" and for script to recognize them and use’em in correct places e.g. copy %SOURCE% %TARGET%?
Thanks,
Yeah you could do something like that though I don’t think you can use “=” as a token delimiter. You could use say a colon “:”,
somebatchfile.bat "SOURC:originalFile.txt" "TARGET:newFile.txt". Here is an example of how you might split the tokens:Running this would produce this:
[Edit]
Ok, maybe it was too close to bed time for me last night but looking again this morning, you can do this:
Which would produce this (note that I reversed the source and target on the command line to show they are set and retrieved correctly):
Note that %1 and %2 are evaluated before the
setso these do get set as environment variables. They must however be quoted on the command line.