Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: Multiple Commands On One Line[Next]: Parameter Replacement
\->Introduction->Parameters

Parameters

Parameters are configuration values passed to a program to tell it what to do, for example in the command to backup your evernote database:

"C:\Program Files (x86)\Evernote\Evernote\ENScript.exe" exportNotes /q any: /f "C:\Dropbox\$Backups$\EVERNOTE BACKUP.enex"

The command is "C:\Program Files (x86)\Evernote\Evernote\ENScript.exe" and its parameters are:

  1. exportNotes

  2. /q

  3. any:

  4. /f

  5. "C:\Dropbox\$Backups$\EVERNOTE BACKUP.enex"

    If a parameter contains spaces then it must be surrounded by double quotes as per the last parameter above otherwise it would be considered as two parameters "C:\Dropbox\$Backups$\EVERNOTE" & "BACKUP.enex" and the command will most likely fail (and definately not do what you intended).

In a batch file you can directly access (read) parameters 1-9 with "%1", 2 with "%2" etc.

As well as "%1" to "%9" you can also use:

You can also use a "~" character after the "%" and that means remove any double quotes first, for example if you had this batch file:

@echo off
echo Parameter #1 = %1
echo Parameter #1 = %~1

And you invoked it as follows (you will see the results):

C:\TMP> TestP1.cmd "A String"
Parameter #1 = "A String"
Parameter #1 = A String

See "parameter replacement" for more information.

To handle an unknown number of parameters you can use the "SHIFT" command.

Parameters are sometimes also referred to as switches or options. More complex programs will not require a strict number of parameters in particular positions but will use parameters as commands (switches) or data (options for the switch). Switches may typically be added in any order but not always.

A switch almost always begins with either "-" or "/". In the above evernote backup command "/q" and "/f" are the switches and "any:" and "C:\Dropbox\$Backups$\EVERNOTE BACKUP.enex" are the options.

How to Work Out the Valid Parameters On a Command Line?

First off many programs will document their command line so look for readme files or documentation on the developer's website, forums etc. A quick Google will typically find what you want, particularly for popular tools.

The types of parameters and how you supply them is up to the developer and there are weird implementations and sometimes the simplest things can stump you or prevent you using a poorly written tool.

Well written programs will provide a way to "ask" it to at least display a summary of the parameters, you should try:

If the above didn't display a GUI program or dialog, but also didn't complete, try typing, if you can do this then its taking input from STDIN, to terminate this press CTRL+Z keys together, otherwise try CTRL+C or CTRL+BREAK.

Most programs will not explain how to handle double quotes (that you are not using to quote a string), hence the section that follows...

Handling Double Quotes etc...

Some particularly idiotic programs may not support spaces in parameters and so won't support double quoting strings or filenames containing spaces! A workaround may be to not invoke that command directly but use a batch file to manipulate the current directory or filenames etc to allow the command to run.

Of the programs that support the quoting some slightly less idiotic programs won't provide a mechanism to "escape" problem characters like the double quote.

Some programs may not be particular about where double quotes are placed, for example /FILE:"A FILE NAME.TXT" may be the same as "/FILE:A FILE NAME.TXT".

To escape a double quote:


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: Multiple Commands On One Line[Next]: Parameter Replacement


ScriptingTipsAndTricks© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday September 07 2014 at 12:50pm
Visit ScriptingTipsAndTricks's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.