Windows command file parameter

Windows command file parameter

Starts a new instance of the command interpreter, Cmd.exe. If used without parameters, cmd displays the version and copyright information of the operating system.

Syntax

Parameters

Parameter Description
/c Carries out the command specified by string and then stops.
/k Carries out the command specified by string and continues.
/s Modifies the treatment of string after /c or /k.
/q Turns the echo off.
/d Disables execution of AutoRun commands.
/a Formats internal command output to a pipe or a file as American National Standards Institute (ANSI).
/u Formats internal command output to a pipe or a file as Unicode.
/t: < | > Sets the background (b) and foreground (f) colors.
/e:on Enables command extensions.
/e:off Disables commands extensions.
/f:on Enables file and directory name completion.
/f:off Disables file and directory name completion.
/v:on Enables delayed environment variable expansion.
/v:off Disables delayed environment variable expansion.
Specifies the command you want to carry out.
/? Displays help at the command prompt.

The following table lists valid hexadecimal digits that you can use as the values for and :

Value Color
0 Black
1 Blue
2 Green
3 Aqua
4 Red
5 Purple
6 Yellow
7 White
8 Gray
9 Light blue
a Light green
b Light aqua
c Light red
d Light purple
e Light yellow
f Bright white

Remarks

To use multiple commands for , separate them by the command separator && and enclose them in quotation marks. For example:

If you specify /c or /k, cmd processes, the remainder of string, and the quotation marks are preserved only if all of the following conditions are met:

You don’t also use /s.

You use exactly one set of quotation marks.

You don’t use any special characters within the quotation marks (for example: & ( ) @ ^ | ).

You use one or more white-space characters within the quotation marks.

The string within quotation marks is the name of an executable file.

If the previous conditions aren’t met, string is processed by examining the first character to verify whether it is an opening quotation mark. If the first character is an opening quotation mark, it is stripped along with the closing quotation mark. Any text following the closing quotation marks is preserved.

If you don’t specify /d in string, Cmd.exe looks for the following registry subkeys:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun\REG_SZ

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun\REG_EXPAND_SZ

If one or both registry subkeys are present, they’re executed before all other variables.

Incorrectly editing the registry may severely damage your system. Before making changes to the registry, you should back up any valued data on the computer.

You can disable command extensions for a particular process by using /e:off. You can enable or disable extensions for all cmd command-line options on a computer or user session by setting the following REG_DWORD values:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\EnableExtensions\REG_DWORD

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\EnableExtensions\REG_DWORD

Set the REG_DWORD value to either 0Г—1 (enabled) or 0Г—0 (disabled) in the registry by using Regedit.exe. User-specified settings take precedence over computer settings, and command-line options take precedence over registry settings.

Incorrectly editing the registry may severely damage your system. Before making changes to the registry, you should back up any valued data on the computer.

When you enable command extensions, the following commands are affected:

assoc

call

chdir (cd)

color

del (erase)

endlocal

for

ftype

goto

if

mkdir (md)

popd

prompt

pushd

set

setlocal

shift

start (also includes changes to external command processes)

If you enable delayed environment variable expansion, you can use the exclamation point character to substitute the value of an environment variable at run time.

File and directory name completion is not enabled by default. You can enable or disable file name completion for a particular process of the cmd command with /f:<on | off>. You can enable or disable file and directory name completion for all processes of the cmd command on a computer or for a user logon session by setting the following REG_DWORD values:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\CompletionChar\REG_DWORD

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\PathCompletionChar\REG_DWORD

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\CompletionChar\REG_DWORD

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\PathCompletionChar\REG_DWORD

To set the REG_DWORD value, run Regedit.exe and use the hexadecimal value of a control character for a particular function (for example, 0Г—9 is TAB and 0Г—08 is BACKSPACE). User-specified settings take precedence over computer settings, and command-line options take precedence over registry settings.

Incorrectly editing the registry may severely damage your system. Before making changes to the registry, you should back up any valued data on the computer.

If you enable file and directory name completion by using /f:on, use CTRL+D for directory name completion and CTRL+F for file name completion. To disable a particular completion character in the registry, use the value for white space [0Г—20] because it is not a valid control character.

Pressing CTRL+D or CTRL+F, processes the file and directory name completion. These key combination functions append a wildcard character to string (if one is not present), builds a list of paths that match, and then displays the first matching path.

If none of the paths match, the file and directory name completion function beeps and does not change the display. To move through the list of matching paths, press CTRL+D or CTRL+F repeatedly. To move through the list backwards, press the SHIFT key and CTRL+D or CTRL+F simultaneously. To discard the saved list of matching paths and generate a new list, edit string and press CTRL+D or CTRL+F. If you switch between CTRL+D and CTRL+F, the saved list of matching paths is discarded and a new list is generated. The only difference between the key combinations CTRL+D and CTRL+F is that CTRL+D only matches directory names and CTRL+F matches both file and directory names. If you use file and directory name completion on any of the built-in directory commands (that is, CD, MD, or RD), directory completion is assumed.

File and directory name completion correctly processes file names that contain white space or special characters if you place quotation marks around the matching path.

You must use quotation marks around the following special characters: & [ ] | < >^ = ; ! ‘ + , `

If the information that you supply contains spaces, you must use quotation marks around the text (for example, «Computer Name»).

If you process file and directory name completion from within string, any part of the path to the right of the cursor is discarded (at the point in string where the completion was processed).

Command line parameters

Batch files can only handle parameters %0 to %9

%0 is the program name as it was called,
%1 is the first command line parameter,
%2 is the second command line parameter,
and so on till %9 .

OK, tell me something new.

Since %0 is the program name as it was called, in DOS %0 will be empty for AUTOEXEC.BAT if started at boot time.
This means that, in AUTOEXEC.BAT, you can check if it is being started at boot time or from the command line, for example to prevent loading TSRs twice.

SHIFT

The batch file’s limitation to handle parameters up to %9 only can be overcome by using SHIFT.
Let us assume your batchfile is called with the command line parameters A B C D E F G H I J K .
Now %1 equals A, %2 equals B, etcetera, until %9 , which equals I. However, %10 does not equal J but A0; %10 is interpreted as %1 , immediately followed by a 0.
Does that mean the rest of the parameters is lost? Of course not.
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.
Use a loop to handle any number of command line parameters:

Note: IF «%1″==»» will cause problems if %1 is enclosed in quotes itself.
In that case, use IF [%1]==[] or, in NT 4 (SP6) and later only, IF «%

1″==»» instead.

In Windows NT 4, 2000 and XP you can SHIFT the command line parameters starting from the nth positions using SHIFT ‘s /n switch, where n can be any (integer) number between 0 and 8: SHIFT /4 will leave %0 through %3 untouched, and shift %5 to %4 , %6 to %5 , etcetera.
To use this feature, Command Extensions should be enabled.

An easy work-around in NT 4 and later is:

No need to use SHIFT anymore.

Delimiters

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

I know of several occasions where these seemingly useless «features» proved very handy.
Keep in mind, though, that these «features» may vary with the operating systems used.

More on command line parsing can be found on the PATH and FOR (especially FOR’s interactive examples) pages.

More options in Windows NT 4/2000/XP

Windows NT 4 introduced a set of new features for command line parameters:

%CmdCmdLine% will return the entire command line as passed to CMD.EXE
%* will return the remainder of the command line starting at the first command line argument (in Windows NT 4, %* also includes all leading spaces)
%

dn

will return the drive letter of %n (n can range from 0 to 9) if %n is a valid path or file name (no UNC)
%

pn

will return the directory of %n if %n is a valid path or file name (no UNC)
%

nn

will return the file name only of %n if %n is a valid file name
%

xn

will return the file extension only of %n if %n is a valid file name
%

fn

will return the fully qualified path of %n if %n is a valid file name or directory
Note: %CmdCmdLine% and %* will leave all delimiters intact, except, in Windows 2000 and later, leading spaces before the first argument

Windows 2000 and XP add even more options.
More information can be found at the page explaining NT’s CALL command.

To remove the leading space of %* included by NT 4 use the following commands:

Validate command line arguments using GOTO

Note: Please read the Best Practices section on command line input validation

A tip by Oliver Schneider:

original code, broken in CMD.EXE (Windows NT 4 and later):

Note: This tip dates back to way before the Windows NT era.
In CMD.EXE (Windows NT 4 and later), a batch file exits if an invalid label is specified, unless the next line tests for ERRORLEVEL 1.

improved code for CMD.EXE:

For a limited number of allowed arguments, this is a time saving technique.

Do note, however, that labels are case sensitive, so you may not want to use this technique for «string type» arguments.
Also keep in mind that labels cannot contain delimiters (space, comma, semi-colon, etcetera), they must be unique, and that only the first 8 characters are used (so the first 8 characters must be unique!).

This technique is best used when each valid value for %1 has its own batch code to process it:

Note: Note the extra IF ERRORLEVEL 1 test I added.
It is required to make the code work in Windows 7 and 8 too.

page last uploaded: 2021-01-27, 16:12

Command extensions enable extra features in NT shells.

By default command extensions are enabled. However, to be absolutely sure that they are, either use SETLOCAL ENABLEEXTENSIONS within your NT shell scripts (batch files) or execute those scripts using CMD /X .

Likewise, you may disable command extensions using SETLOCAL DISABLEEXTENSIONS or CMD /Y .

Read my SETLOCAL page if you intend to use SETLOCAL extension switches.

Читайте также:  Template module windows physical disks by zabbix agent
Оцените статью