Manipulating Command-Line Parameters
Learn how to manipulate variables
May 9, 2005
One of the most common command-line parameters for a script is either a filename or directory path. As I was looking through the For command's Help file, I noticed that I could manipulate the variables within the loop by using modifiers between the percent sign and the variable name (e.g., using %~I to remove surrounding quotes for the value stored in %I). I tried the same modifiers on regular command-line parameters (e.g., %1, %2, %3) and was surprised to find that they also worked there. Table A shows a rundown of some useful pieces of information you can retrieve from command-line parameters that are filenames or directory names.
Simply replace the letter I with the variable you want to manipulate. You can also combine these modifiers. For example, in a Windows shell script, %0 contains the name of the script being executed. If you have a script whose full path is C:scriptstesttestscript.cmd, %0 will contain the string testscript.cmd. However, if you want to find the full path in which testscript.cmd resides, you can use %~dp0, which will now contain the string C:scriptstest. You can use the same technique on user-defined parameters such as %1. To see how modifiers affect the value displayed for these variables, you can run the modifiertest.cmd script that Listing A shows.
About the Author
You May Also Like