Skip navigation

Manipulating Command-Line Parameters

Downloads
46132.zip

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:\scripts\test\testscript.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:\scripts\test\. 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.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish