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,
You simply substitute the parameter for 1 (e.g., %~f2 for the second parameter's fully qualified path name). The %0 parameter in a batch file holds information about the file when it runs and indicates which command extensions you can use with the file (e.g., %~dp0 gives the batch file's drive and path).
Batch parameters (Command line parameters): In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on. If you require all arguments, then you can simply use %* in a batch script.
There is no practical limit to the number of parameters you can pass to a batch file, but you can only address parameter 0 (%0 - The batch file name) through parameter 9 (%9).
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:
@echo off set foo=%1 echo input: %foo% for /f "tokens=1,2 delims=:" %%a in ("%foo%") do set name=%%a & set val=%%b echo name: %name% echo value: %val%
Running this would produce this:
C:\>test.bat SOURC:originalFile.txt input: SOURC:originalFile.txt name: SOURC value: originalFile.txt
[Edit]
Ok, maybe it was too close to bed time for me last night but looking again this morning, you can do this:
@echo off set %1 set %2 echo source: %SOURCE% echo target: %TARGET%
Which would produce this (note that I reversed the source and target on the command line to show they are set and retrieved correctly):
C:\>test.bat "TARGET=newFile.txt" "SOURCE=originalFile.txt" source: originalFile.txt target: newFile.txt
Note that %1 and %2 are evaluated before the set
so these do get set as environment variables. They must however be quoted on the command line.
Other way I quite liked:
set c=defaultC set s=defaultS set u=defaultU :initial if "%1"=="" goto done echo %1 set aux=%1 if "%aux:~0,1%"=="-" ( set nome=%aux:~1,250% ) else ( set "%nome%=%1" set nome= ) shift goto initial :done echo %c% echo %s% echo %u%
Run the following command:
arguments.bat -c users -u products
Will generate the following output:
users defaultS products
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With