Batch Script – Functions - Using the SHIFT Operator - Batch Script

One of the limitations of command line arguments is that it can accept only arguments till %9. Let’s take an example of this limitation.

Example

Output

If the above code is stored in a file called test.bat and the file is run as

Then following will be the output.



As you can see from the above output, the final value which should be shown as ‘j’ is being shown as a0. This is because there is no parameter known as %10.

This limitation can be avoided by using the SHIFT operator. After your batch file handled its first parameter(s) it could SHIFT them (just insert a line with only the command SHIFT), resulting in %1 getting the value B, %2 getting the value C, etcetera, till %9, which now gets the value J. Continue this process until at least %9 is empty.

Let’s look at an example of how to use the SHIFT operator to overcome the limitation of command line arguments.

Example

If the above code is stored in a file called test.bat and the file is run as

Then following will be the output.

Note

Some characters in the command line are ignored by batch files, depending on the DOS version, whether they are "escaped" or not, and often depending on their location in the command line −

  • Commas (",") are replaced by spaces, unless they are part of a string in doublequotes.
  • Semicolons (";") are replaced by spaces, unless they are part of a string in doublequotes.
  • "=" characters are sometimes replaced by spaces, not if they are part of a string in doublequotes.
  • The first forward slash ("/") is replaced by a space only if it immediately follows the command, without a leading space.
  • Multiple spaces are replaced by a single space, unless they are part of a string in doublequotes.
  • Tabs are replaced by a single space.
  • Leading spaces before the first command line argument are ignored.
  • Trailing spaces after the last command line argument are trimmed.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Batch Script Topics