{
	"id": "b9302bb1-1cca-4fbb-96c7-098a7671db26",
	"created_at": "2026-04-06T01:29:59.475949Z",
	"updated_at": "2026-04-10T03:20:33.529552Z",
	"deleted_at": null,
	"sha1_hash": "7d1279bf8dd28b5a734e3f9febd1a781b04b3969",
	"title": "The Windows NT Command Shell",
	"llm_title": "",
	"authors": "",
	"file_creation_date": "0001-01-01T00:00:00Z",
	"file_modification_date": "0001-01-01T00:00:00Z",
	"file_size": 236133,
	"plain_text": "The Windows NT Command Shell\r\nBy About the Author\r\nArchived: 2026-04-06 00:52:59 UTC\r\nArchived content. No warranty is made as to technical accuracy. Content may contain URLs that were valid when\r\noriginally published, but now link to sites or pages that no longer exist.\r\nBy Tim Hill\r\nChapter 2 from Windows NT Shell Scripting, published by MacMillan Technical Publishing\r\nCommand Shell Basics The command shell is introduced, along with the basic command syntax. The\r\ndifference between CMD.EXE and COMMAND.COM is explained.\r\nStarting a Command Shell Learn how to start, stop and nest command shells. Command line switches\r\naccepted by the command shell are also detailed.\r\nCommand Line Editing Find out about the various command text-editing features, including less well-known features such as command completion and the command history.\r\nDOSKEY and Command Macros The features available through the DOSKEY command are described\r\nhere, including command macros.\r\nLaunching Applications from the Shell This section provides complete details of how the shell launches\r\napplications, including how they are located and how file associations are used. The role of the PATH and\r\nPATHEXT variables is explored, as well as the use of the START command.\r\nControlling Script Output Various commands allow control over script output, such as ECHO and\r\nTITLE.\r\nCommand Redirection Find out how to capture command output, redirect command input, and send the\r\noutput of one command to the input of another.\r\nRunning Multiple Commands Complex command lines execute multiple commands; learn about multi-line commands and the use of parentheses to group commands together.\r\nUsing Command Filters The filter commands, such as MORE and SORT, are described here.\r\nThe Windows NT Command Scheduler This section describes the Windows NT command scheduler, the\r\nSchedule Service and the AT command.\r\nCommand Shell Basics\r\nStarting a Command Shell\r\nCommand Line Editing\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 1 of 43\n\nDOSKEY and Command Macros\r\nLaunching Applications from the Shell\r\nControlling Script Output\r\nCommand Redirection\r\nRunning Multiple Commands\r\nUsing Command Filters\r\nThe Windows NT Command Scheduler\r\nThe previous chapter introduced the Windows NT console window. By far the most common use for a console\r\nwindow is to execute the Windows NT command shell. The command shell defines the Windows NT scripting\r\nlanguage and is responsible for processing scripts, as well as commands typed at the keyboard.\r\nThe command shell is a console application. As explained in chapter 1, console applications are Windows NT\r\napplications that interact via a console window, rather than via GUI elements such as windows and dialog boxes.\r\nWhen you start a command shell, Windows NT creates a console window for that shell. All commands that are run\r\nfrom within a shell (including other command shells) share the same console window for output. The only\r\nexception to this is the START command, which can be used to create additional console windows.\r\nTo start a default command shell in a console window, click the Start button, select the Programs item, and then\r\nselect the Command Prompt command. The section \"Starting a Command Shell\" details additional ways to start a\r\ncommand shell.\r\nBy default, command shells run in Interactive mode. In this mode, the shell displays a prompt and then waits for\r\nkeyboard input. When a command line is entered, it is immediately interpreted and then executed. After execution\r\ncompletes, the shell displays another prompt, and the whole sequence begins again. This continues until the EXIT\r\ncommand ends the command shell session. Figure 2.1 shows this basic command execution sequence.\r\nIf a command entered is the name of a script file, the command shell switches to Script mode, and begins reading,\r\ninterpreting, and executing commands from the specified script file. Figure 2.2 shows the command execution\r\nsequence while the shell is in script mode. Execution of the script ends when the shell reaches the end of the script\r\nfile. At this point, the shell returns to interactive mode, displays another command prompt, and waits for keyboard\r\ninput.\r\nFigure 2.1: Interactive mode shell execution sequence\r\nFigure 2.1: Interactive mode shell execution sequence\r\nThe Command Shell Prompt\r\nThe command shell displays the shell prompt when it is ready to accept a command in interactive mode. The\r\ndefault shell prompt is the current drive and path name, followed by a \u003e character. When the command shell is in\r\nscript mode, prompts are only displayed if command echo is enabled. The ECHO command, described in the\r\nsection \"Controlling Script Output,\" controls command echo.\r\nThe PROMPT command changes the command shell prompt. Follow the PROMPT command with the text of the\r\nnew shell prompt. For example:\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 2 of 43\n\n1. C:\\\u003eprompt ???\r\n2. ???\r\nFigure 2.2: Script mode shell execution sequence\r\nFigure 2.2: Script mode shell execution sequence\r\nThis example changes the command prompt to three question marks. Notice that the shell uses this prompt on line\r\n2 in the example. To revert to the default prompt, enter a PROMPT command without any prompt text. For\r\nexample:\r\n1. ???prompt\r\n2. C:\\\u003e\r\nThe prompt is restored to the default.\r\nThe command prompt text can contain special characters used as placeholders for additional information. For\r\nexample, $T in the command text is replaced with the current time when the prompt is displayed, and $G is\r\nreplaced with the \u003e character. The following example shows this:\r\n1. C:\\\u003eprompt $T$G\r\n2. 14:23:50.35\u003e\r\nUsing $G to represent the \u003e character might seem unnecessary, as this character can simply be typed. However,\r\nthis does not work, because the command shell reserves certain characters for special purposes. These reserved\r\nshell characters all have $c equivalents, so that they can be used in command prompts. For example, $A is used\r\nfor an ampersand, $L for a \u003c character, and so on. Table 2.1 shows the complete set of special characters\r\nrecognized in a command shell prompt.\r\nTable 2.1 Special Characters in a Shell Prompt\r\nCharacter Description\r\n$A Ampersand character.\r\n$B Pipe (|) character.\r\n$C Left parenthesis.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 3 of 43\n\nCharacter Description\r\n$D Current date.\r\n$E Escape code (ASCII 27).\r\n$F Right parenthesis.\r\n$G Greater than character.\r\n$H Backspace character.\r\n$L Less than character.\r\n$N Current drive letter.\r\n$P Current drive letter and directory path.\r\n$Q Equal sign.\r\n$S Space.\r\n$T Current time.\r\n$V Windows NT version number.\r\n$_ New line.\r\n$$ Dollar sign.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 4 of 43\n\nCharacter Description\r\n$+\r\nA series of \"+\" signs, corresponding to the number of pushed directories on the PUSHD stack.\r\nSee the PUSHD command in Part III.\r\n$M The remote name (UNC name) for the current drive.\r\nThe $+ special character works in conjunction with the PUSHD and POPD commands (see Part III). These\r\ncommands manage a push-down stack of directories and drives, and the $+ special character displays a sequence\r\nof + characters in the command prompt, one for each level in the push-down stack.\r\nIf the current drive is a network drive, the $M special character displays the remote UNC name of this drive in the\r\nform \\\\server\\share. If the current drive is a local drive, then $M does not display anything.\r\nThe current prompt text is stored in the PROMPT environment variable. Changing the prompt changes the value\r\nof this variable and vice versa. Thus, these two commands have the same effect:\r\n1. C:\\\u003eprompt [$p]\r\n2. C:\\\u003eset PROMPT=[$p]\r\nSimple Command Syntax\r\nAs described in chapter 1, simple shell commands consist of a command name followed by any required\r\narguments. The command name and arguments (if any) are separated by a space. A command is always entered in\r\nresponse to a shell prompt. Figure 2.3 shows a simple shell command.\r\nFigure 2.3: Simple command syntax\r\nFigure 2.3: Simple command syntax\r\nIn Figure 2.3, the command name describes the action to be performed, while the arguments provide additional\r\ninformation needed to carry out this action. The syntax of the command arguments is specific to each command.\r\nHowever, there are a number of well-established conventions for command argument syntax. These are only\r\nconventions, however, and each individual command is free to interpret the supplied arguments however it\r\nchooses:\r\nFirst, multiple arguments are normally separated from one another by spaces. In Figure 2.3, the command\r\nhas three arguments, c:\\*.bak, e:\\backup, and /s. Occasionally, other characters are used as argument\r\nseparators. For example, the COPY command can use + characters to separate multiple filenames.\r\nSecond, any argument that contains spaces or begins or ends with spaces must be enclosed in double\r\nquotes. This is particularly important when using long file and directory names, which frequently contain\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 5 of 43\n\none or more spaces. If a double-quoted argument itself contains a double quote character, the double quote\r\nmust be doubled. For example, enter \"Quoted\" Argument as \"\"\"Quoted\"\" Argument\".\r\nThird, command switches always begin with a slash / character. A switch is an argument that modifies the\r\noperation of the command in some way. Occasionally, switches begin with a + or - character. Some\r\nswitches are global, and affect the command regardless of their position in the argument list. Other\r\nswitches are local, and affect specific arguments (such as the one immediately preceding the switch).\r\nFourth, all reserved shell characters not in double quotes must be escaped. These characters have special\r\nmeaning to the Windows NT command shell. The reserved shell characters are:\r\n\u0026 | ( ) \u003c \u003e ^\r\nTo pass reserved shell characters as part of an argument for a command, either the entire argument must be\r\nenclosed in double quotes, or the reserved character must be escaped. Prefix a reserved character with a\r\ncarat (^) character to escape it. For example, the following command example will not work as expected,\r\nbecause \u003c and \u003e are reserved shell characters:\r\n1. C:\u0026gt;echo \u003cdir\u003e\r\n2. The syntax of the command is incorrect.\r\nInstead, escape the two reserved characters, as follows:\r\n3. C:\u0026gt;echo ^\u003cdir^\u003e\r\n4. \u003cdir\u003e\r\nTypically, the reserved shell characters are not used in commands, so collisions that require the use of\r\nescapes are rare. They do occur, however. For example, the popular PKZIP program supports a -\u0026 switch\r\nto enable disk spanning. To use this switch correctly under Windows NT, -^\u0026 must be typed.\r\nTip The carat character is itself a reserved shell character. Thus, to type a carat character as part of a\r\ncommand argument, type two carats instead. Escaping is necessary only when the normal shell\r\ninterpretation of reserved characters must be bypassed.\r\nFinally, the maximum allowed length of a shell command appears to be undocumented by Microsoft.\r\nSimple testing shows that the Windows NT command shell allows very long commands—in excess of\r\n4,000 characters. Practically speaking, there is no significant upper limit to the length of a command.\r\nBe aware that a command shell is not an MS-DOS command prompt, even though it shares the same icon. The\r\nWindows NT command shell is a full 32-bit Windows NT console application that resides in the CMD.EXE\r\nexecutable file. The MS-DOS command prompt is a 16-bit DOS application that resides in the COMMAND.COM\r\nexecutable file. Because COMMAND.COM is a 16-bit DOS executable, Windows NT executes this shell within a\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 6 of 43\n\nWindows NT virtual DOS machine (VDM). COMMAND.COM is supplied primarily for compatibility with MS-DOS.\r\nSurprisingly, however, both the Windows NT and the MS-DOS shells have almost identical features. Here is a\r\nsample IF command entered into a Windows NT command shell, followed by the command output:\r\n1. C:\\\u003eif /i a==A echo MATCH\r\n2. MATCH\r\nThe IF command compares the letter \"a\" to the letter \"A\" and echoes MATCH if they compare. The /I switch\r\ncompares the two letters without regard to letter case. Therefore, not surprisingly, the command echoes MATCH.\r\nHere is the same IF command entered into an MS-DOS 16-bit COMMAND.COM shell (running on Windows NT\r\non the same computer):\r\n1. C:\\\u003eif /i a==A echo MATCH\r\n2. MATCH\r\nThe output of both commands is identical. This is surprising, as the /I switch is a new feature of the Windows NT\r\ncommand shell (CMD.EXE) which is not understood by COMMAND.COM when running under actual MS-DOS.\r\nThis behavior reveals a quite subtle feature of Windows NT that is very important. The 16-bit MS-DOS shell\r\n(COMMAND.COM) that ships with Windows NT is specially designed for Windows NT. When a command is\r\nentered for execution by this shell, it does not actually execute it. Instead, it packages the command text and sends\r\nit to a 32-bit CMD.EXE command shell for execution. Because all commands are actually executed by CMD.EXE\r\n(the Windows NT command shell), the 16-bit shell inherits all the features and facilities of the full Windows NT\r\nshell.\r\nYou can see COMMAND.COM automatically execute a 32-bit CMD.EXE shell by using the Windows NT Task\r\nManager application. Follow this procedure:\r\n1. Right-click in an empty area in the taskbar. From the popup window select the Task Manager command to\r\nstart Task Manager.\r\n2. Click the Processes tab to display the list of running Windows NT processes.\r\n3. Click the Start button in the taskbar and select the Run command.\r\n4. In the Open box, type COMMAND. Then click OK to start a 16-bit command prompt.\r\n5. Now examine the list of processes in the Task Manager window. You should see at least one NTVDM\r\nprocess. This is the Windows NT VDM, which NT starts to allow COMMAND.COM to execute.\r\n6. In the COMMAND.COM window, enter any command which generates lengthy output (DIR /S is a good\r\nchoice). When the output begins scrolling, press Ctrl+S to pause the command.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 7 of 43\n\n7. Switch to Task Manager and examine the list of processes again. Notice that a new CMD.EXE (command\r\nshell) is running. Figure 2.4 shows a typical Task Manager window.\r\n8. Switch to the COMMAND.COM window and press Ctrl+S again. Wait for the command to complete.\r\n9. Switch to Task Manager again. Notice that the CMD.EXE shell has disappeared from the Processes list.\r\n10. To close the COMMAND.COM window enter an EXIT command.\r\nThis simple experiment shows that every command entered for execution, regardless of the shell used, is\r\nultimately executed by CMD.EXE, the Windows NT command shell.\r\nCc723564.shell04(en-us,TechNet.10).gif\r\nFigure 2.4: Task Manager showing automatic CMD.EXE execution\r\nYou can start a Windows NT command shell in a number of different ways:\r\nSelect the Programs item in the Start menu, and then select the Command Prompt command.\r\nSelect the Run command in the Start menu, enter CMD in the Open box and click OK.\r\nEnter a CMD command in an existing command shell.\r\nEnter a START command in an existing command shell, and specify CMD as the command to execute.\r\nDouble-click a .BAT or .CMD script file in an Explorer window.\r\nAll these methods except the last start a command shell in Interactive mode. The last method starts the command\r\nshell in Script mode.\r\nLike most other commands, the command shell CMD.EXE accepts several switches that control various shell\r\noptions. Table 2.2 shows these switches, which are described in detail in the \"Command Reference\" in Part III.\r\nTable 2.2 CMD.EXE Switches\r\nSwitch Description\r\n/X Enables command extensions (default).\r\n/Y Disables command extensions.\r\n/A Command output to files or pipes will be ANSI (default).\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 8 of 43\n\nSwitch Description\r\n/U Command output to files or pipes will be Unicode.\r\n/T Sets foreground and background window colors.\r\n/C Executes command specified and then terminate shell.\r\n/K Executes command specified and then prompts for additional commands.\r\nTo specify shell switches, place them after the CMD command on the command line. It is not possible to directly\r\nenter shell switches if the shell is started from the Start menu Command Prompt command, or by double-clicking\r\na .BAT or .CMD script file (the first and last methods in the preceding list).\r\nWithout any switches, a command shell starts in Interactive mode with command extensions enabled. Command\r\nextensions are certain features added to the command shell since Windows NT was first released. In general, these\r\nextensions are backward compatible, but they can occasionally cause older Windows NT and MS-DOS scripts to\r\nfail. In this case, the /Y switch disables the extensions and, in effect, runs an MS-DOS compatible command shell.\r\nThe /C and /K switches directly execute a command. The command to execute is specified following the switch.\r\nFor example:\r\n1. C:\\\u003ecmd /c echo Run this...\r\n2. Run this...\r\nAll command line arguments following the /C or /K switch describe the command to execute. The /C switch\r\nexecutes the specified command, and the command shell then terminates. The /K switch executes the specified\r\ncommand, and the shell then enters Interactive mode. The /K switch is particularly useful to set up a command\r\nshell to a predetermined state before the first command prompt is displayed.\r\nThe /C and /K switches both accept any valid shell command, including the name of a script file to execute. In this\r\ncase, the shell enters script mode and executes the specified script. This is the method the Windows NT Explorer\r\nuses to start a script file when it is opened. For example, if you double-click on the file SCRIPT.BAT, Explorer\r\nactually executes this command:\r\ncmd /c script.bat\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 9 of 43\n\nIn Interactive mode, the EXIT command terminates a command shell. If a console window was created for the\r\ncommand shell, the console window closes. If the command shell was invoked from within another program\r\n(including another command shell), that program regains control of the console window.\r\nTip Closing the console window also terminates a command shell. This method is valid only for the 32-bit\r\nCMD.EXE shell. The 16-bit COMMAND.COM shell should not be terminated in this manner.\r\nThe EXIT command can also be used in script mode. The command should be used with care, however, as the\r\nshell terminates immediately, effectively aborting script execution. Scripts should normally end by executing to\r\nthe end of the script file, or by the GOTO :EOF command (see Chapter 4).\r\nIf a script file is running in a shell that was started using the /C switch, the command shell terminates when the\r\nscript reaches the end of the file.\r\nA command shell can be started from within another command shell, commonly referred to as nesting a shell. This\r\ncan be done either with the START command or by entering a CMD command directly at the command prompt.\r\nNesting shells is useful if a command or script must be executed using different options to the current shell. For\r\nexample, command extensions can be disabled in the nested command shell, or the command redirection mode\r\nchanged. For example:\r\n1. c:\\\u003eprompt [$p]\r\n2.\r\n3. [c:\\]cmd /y\r\n4. Microsoft(R) Windows NT(TM)\r\n5. (C) Copyright 1985-1996 Microsoft Corp.\r\n6.\r\n7. [c:\\]prompt\r\n8. c:\\\u003eexit\r\n9. [c:\\]\r\nThe first PROMPT command changes the prompt. A new command shell is then invoked with the /Y switch (to\r\ndisable command extensions). Notice that the new command shell inherits the prompt from the previous shell. The\r\nsecond PROMPT command returns the prompt to the default, as can be seen in the subsequent shell prompt\r\n(C:\\\u003e). The EXIT command exits the nested command shell, reverting to the previous (original) shell. Notice that\r\nthe prompt reverts to the modified form, showing that the second command shell has indeed terminated.\r\nThe inheritance of the current command prompt by nested command shells is not a special feature of the shell. It\r\noccurs because the current command prompt is stored in an environment variable, and all commands executed\r\nfrom a shell inherit the current shell environment. The environment and inheritance is described fully in Chapter\r\n3.\r\nNested command shells are useful when used with the /C switch. Suppose the script SXT.BAT was designed to\r\nrun correctly only if command extensions are enabled. The following command executes this script correctly\r\nregardless of the state of command extensions for the current shell:\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 10 of 43\n\nC:\\\u003ecmd /x /c sxt.bat\r\nAnother use of nested command shells—capturing all script output—is described in the upcoming section,\r\n\"Controlling Script Output.\"\r\nWhen in interactive mode, the command shell provides a rich set of tools to assist in command entry and editing.\r\nMost of these tools are applicable to all console applications that use line by line input. The command-line editing\r\ntools include:\r\nBasic character editing, such as Backspace and Delete.\r\nTemplate editing, which operates with a copy of the previously entered command.\r\nCommand history editing, which allows quick recall of previously entered commands.\r\nCommand completion, which automatically completes partially entered file and directory names.\r\nCommand macros, which are described in \"DOSKEY and Command Macros.\"\r\nThe \"Command Line Editing\" section in the \"Command Reference\" describes the full set of command-line editing\r\ncommands.\r\nBasic character editing includes familiar commands such as Backspace, which erases the character to the left of\r\nthe cursor, and Delete, which erases the character to the right of the cursor. Use the Left Arrow or Right Arrow\r\nkeys to move the cursor left or right one character. Use these keys with Ctrl to move the cursor left or right one\r\nword. Use Home to move the cursor to the start of the command line, and End to move to the end of the\r\ncommand. Press the Esc key to delete the entire command, and press Enter to execute the command. The cursor\r\ndoes not have to be at the end of the line when Enter is pressed.\r\nBy default, the character editor operates in Overwrite mode. New characters typed overwrite characters at the\r\ncurrent cursor location. Pressing the Insert key switches to Insert mode. In Insert mode, characters to the right of\r\nthe cursor are shifted to the right as new characters are entered at the cursor location. Repeatedly pressing Insert\r\ntoggles back and forth between the two modes. The cursor changes shape to indicate the mode—Insert mode is\r\nindicated by a larger, block shaped cursor. The mode resets when the Enter key is pressed.\r\nThe default Insert/Overwrite mode can be changed from Overwrite to Insert either by the DOSKEY command or\r\nvia the Console Window property sheet's Options tab, as described in Chapter 1.\r\nThe second editing method, template editing, works in conjunction with a hidden command buffer, the template,\r\nwhich contains a copy of the most recently entered command. Template editing is present for compatibility with\r\nMS-DOS, and the newer command history editing commands generally make template editing obsolete.\r\nThe template F4 command is useful to quickly delete blocks of characters in a command line. Position the cursor\r\nat the first character to delete and press F4. A popup prompt appears. Enter a single character, and all characters in\r\nthe command line from the cursor up to (but not including) the first instance of the specified character are deleted.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 11 of 43\n\nCommand history editing provides quick access to a list of recently executed commands, also known as the\r\nhistory buffer. Commands in this buffer can be re-executed, or recalled to the command line and edited as desired.\r\nEach time a shell command executes, the command text is added to the command history buffer, and is then\r\navailable for later recall. The maximum number of stored commands is set by the DOSKEY command or via the\r\nConsole Window property sheet's Options tab, as described in Chapter 1.\r\nThe F7 key displays the command history buffer. Press F7, and a popup window appears containing the contents\r\nof the history buffer, with the oldest commands listed first.\r\nFigure 2.5 shows a console window with a command history popup displayed. Each command is numbered,\r\nstarting with 1 for the oldest command. The Up Arrow and Down Arrow keys move up and down through the list\r\nof commands, while the Page Up or Page Down keys move to the start or end of the command list. The Esc key\r\ncloses the history buffer popup window without executing a command.\r\nCc723564.shell05(en-us,TechNet.10).gif\r\nFigure 2.5: The command history buffer popup window\r\nTo recall a command from the history buffer and execute it:\r\n1. Press F7 to display the command history popup window.\r\n2. Use the up arrow and down arrow keys in the popup window to highlight the command to execute.\r\n3. Press Enter to execute the command.\r\nWhen editing a command, use the up and down arrow keys to directly recall commands from the history buffer for\r\nediting. This differs from using these keys within the command history popup window. Press the keys repeatedly\r\nto cycle through all commands in the command buffer. The Up Arrow key moves back through the command\r\nhistory (starting with the most recently entered command) while the Down Arrow moves forward through the\r\ncommand history. The Page Up key moves to the oldest command in the history buffer, and the Page Down key\r\nmoves to the newest command in the buffer. Once a command is recalled to the command line, it can be edited as\r\nneeded and then executed by pressing the Enter key.\r\nThe F8 key searches the history buffer. Begin by typing the first few characters of the command that is required\r\ninto the command line. Press the F8 key, and the first (most recent) command in the history buffer that begins with\r\nthe characters entered is recalled to the command line. Repeatedly pressing F8 continues to search for additional\r\nmatching commands in the history buffer.\r\nThe F9 key recalls commands from the history buffer by number. Command numbers are displayed in the\r\ncommand history popup, accessible by pressing F7. Press F9, and a popup window appears asking for the\r\ncommand number. Enter the command number, and the specified command is recalled to the command line ready\r\nfor editing or execution. The F9 key can also be used while the command history buffer popup window is\r\ndisplayed.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 12 of 43\n\nWindows NT supports command history editing for any program which reads keyboard input on a line-by-line\r\nbasis. Each program maintains its own command history, so multiple command shells each have an independent\r\ncommand history buffer.\r\nCommand completion automatically completes the typing of long file or directory names. Before this feature can\r\nbe used, it must be enabled by changing a setting in the Windows NT registry. The value to alter is:\r\nHKEY_CURRENT_USER \\Software \\Microsoft \\Command Processor\\CompletionChar\r\nThe default value for this is 0x0, which disables command completion. To enable command completion, set this\r\nvalue to the ASCII code for the key to use as the command completion key. The Tab key, value 0x9, is a good\r\nchoice since it is not normally used in commands. The value is located in the per-user portion of the registry, and\r\nis therefore enabled on a per-user basis.\r\nOnce enabled, command completion operates as follows:\r\n1. When entering a file or directory name, type the first few characters of the name and press the chosen\r\ncommand completion key (e.g. the Tab key).\r\n2. The shell searches the current directory for a file or directory name which begins with the specified\r\ncharacters, and replaces the typed characters with the complete file or directory name.\r\n3. Pressing the command completion key again cycles through all additional matching file names.\r\nFor example, suppose the current directory (C:\\BOOK) contains three files, CHAP01.DOC, CHAP02.DOC and\r\nCHAPTER.DOC. Type the following, but do not press Enter:\r\nC:\\book\u003etype chap\r\nPress the command completion key. The command line immediately changes to:\r\nC:\\book\u003etype \"c:\\book\\chap01.doc\"\r\nThe partial file name is replaced with the first file name found in the directory. The full path name is substituted,\r\nand the whole argument is placed in double quotes. This ensures that a valid argument is created even if the file\r\nname contains spaces. Press the command completion key again, and the command line changes to:\r\nC:\\book\u003etype \"c:\\book\\chap02.doc\"\r\nThe command completion character can also be used when no characters of a file or directory name are entered.\r\nType the command up to the file or directory name, making sure that the last character entered is a space. Then\r\npress the command completion key. In this case, all file and directory names in the current directory are presented\r\nin sequence.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 13 of 43\n\nCommand completion is a very useful editing tool, particularly when long file and directory names are in use.\r\nThe DOSKEY command provides command line control of various advanced shell command editing features. In\r\nMS-DOS, the DOSKEY command was a Terminate-and-Stay Resident (TSR) that needed to be loaded before its\r\nfeatures were available for use. In Windows NT, these features are built into the shell and are always available;\r\nDOSKEY merely provides an interface to control them.\r\nThe DOSKEY command actually provides three distinct functions:\r\nInsert mode control\r\ncommand history buffer management\r\ncommand macro management\r\nInsert mode control sets the initial Insert/Overwrite edit toggle. For example, this command sets the initial toggle\r\nto Insert mode:\r\nC:\\\u003edoskey /insert\r\nThis command sets the initial toggle to overwrite mode:\r\nC:\\\u003edoskey /overstrike\r\nThe initial state of this toggle is set for a console window using the console window property sheet Options tab, as\r\ndescribed in Chapter 1.\r\nThe following DOSKEY command clears the command history buffer (described in the previous section\r\n\"Command History Editing\") of all commands:\r\nC:\\\u003edoskey /reinstall\r\nThis command optionally sets a new size for the command history buffer. For example:\r\nC:\\\u003edoskey /reinstall /listsize=100\r\nThis example clears the command history buffer and sets the number of commands in the buffer to 100. The\r\ncommand history buffer size can also be set for a console window using the Console Window property sheet\r\nOptions tab, as described in Chapter 1.\r\nThe final set of DOSKEY commands provides control of command shell macros. Command macros are shorthand\r\ncommands for longer, more complex shell commands. Macros can reduce typing or, in scripts, provide simple\r\none-line functions. For example, to define a new macro named LS, which executes a DIR command, enter:\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 14 of 43\n\nC:\\\u003edoskey ls=dir\r\nThe command shell will now accept LS as an alias for the DIR command. For example:\r\n1. C:\\\u003els\r\n2. Volume in drive C is BOOTFAT\r\n3. Volume Serial Number is 3F3F-1704\r\n4.\r\n5. Directory of C:\\\r\n6.\r\n7. 05/31/94 01:22p 54,645 COMMAND.DOS\r\n8. ...etc.\r\nCommand macros, like all shell commands, are not case sensitive. The simple definition of LS is not a direct\r\nreplacement for the DIR command, however, because it does not accept arguments. For example:\r\nC:\\\u003els *.dos\r\nwill not work as expected. The *.DOS argument is ignored, and the entire directory is displayed. To pass\r\narguments to a macro, define the macro as follows:\r\nC:\\\u003edoskey ls=dir $*\r\nThe special $* argument acts as a placeholder for all arguments entered on the command line. The LS macro is\r\nnow an exact replacement for the DIR command. The DOSKEY section in the \"Command Reference\" in Part III\r\nlists additional special arguments that can be used within macro definitions.\r\nObviously, directly replacing one command by an alias is of limited use. However, macros can replace more\r\ncomplex commands. For example:\r\nC:\\\u003edoskey ls=dir /od $*\r\nThis definition of the LS macro always provides directory listings sorted in date order. The /OD switch tells the\r\nDIR command to sort files according to date and time.\r\nThe following macro definition creates a command that displays the user name of the currently logged-on user:\r\n1. C:\\\u003edoskey myname=for /f \"delims=\\ tokens=2\" %i in ('whoami') do @echo %i\r\n2. C:\\\u003emyname\r\n3. TimHill\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 15 of 43\n\nThe MYNAME macro uses an advanced FOR command (described in Chapter 4, \"Control Flow, Procedures, and\r\nScript Nesting\") and the WHOAMI command to extract the user name. Typing MYNAME is far simpler than\r\nentering the complex FOR command. Macros are also very useful when combined with the various compound\r\ncommand symbols described in the section \"Running Multiple Commands.\" For example:\r\nC:\\\u003edoskey dircount=dir $* $B find \"\u003cDIR\u003e\" /c\r\nThis example creates a DIRCOUNT macro that counts the number of directories in a specified directory. The $B\r\nargument acts as a placeholder for the pipe symbol (|). Alternatively, the pipe symbol can be entered directly by\r\nescaping it using a ^ character. Pipes are described in the section \"Command Redirection.\"\r\nMacros can be used with any Windows NT application which accepts line-by-line command input. However, each\r\nmacro is explicitly defined for a specific application. By default, macros are defined for use by the command\r\nshell, CMD.EXE. The /EXENAME switch defines a macro for another application. For example, to define an\r\nEXIT macro for use with FTP, enter:\r\nC:\\\u003edoskey /exename=ftp.exe exit=bye\r\nThe /MACROS switch lists defined macros for the command shell, for example:\r\n1. C:\\\u003edoskey /macros\r\n2. ls=dir $*\r\nThe /EXENAME switch lists macros for a specific application, for example:\r\n1. C:\\\u003edoskey /macros /exename=ftp.exe\r\n2. exit=bye\r\nThe /MACROS:ALL switch lists macros for all applications. The macros for each application are listed under the\r\napplication name, which is placed in brackets.\r\nThe /MACROFILE switches reads a set of macro definitions from a file, for example:\r\nC:\\\u003edoskey /macrofile=macros.mac\r\nThe format of macros in the macro file exactly matches the output of the DOSKEY command with the\r\n/MACROS:ALL switch. Therefore, it is possible to define a set of macros interactively and then use DOSKEY to\r\ncreate a macro file. Later, the macros can be recalled. For example:\r\n1. C:\\\u003edoskey ls=dir /od $*\r\n2. C:\\\u003edoskey /exename=ftp.exe exit=bye\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 16 of 43\n\n3. ...etc.\r\n4. C:\\\u003edoskey /macros:all \u003emacros.mac\r\nThis example uses command output redirection (described in the section \"Command Redirection\") to capture the\r\nmacro definitions to the file MACROS.MAC. Later, the /MACROFILE switch can reload the macros. One\r\nconvenient way to do this is to use the CMD.EXE /K switch. For example:\r\nC:\\\u003ecmd /k doskey /macrofile=macros.mac\r\nThis starts a new shell and pre-loads all the macros in MACROS.MAC. If this command is placed in a shortcut, a\r\nnew command shell can be started and a set of macros loaded automatically without any typing.\r\nAs described at the beginning of this chapter, a basic shell command is composed of a command name followed\r\nby zero or more arguments. The command name specifies the action to be performed, and the arguments provide\r\nadditional data used by the command to perform this action. In order to carry out the action specified by the\r\ncommand, the command shell must decode the command. This section describes the steps taken by the shell to\r\ndecode each command.\r\nCommands can be broken into two main categories: internal and external. An internal command is one that is\r\nbuilt-in to the shell itself. An external command is one that is contained within an executable file on the disk. For\r\nexample, the COPY command is internal, while the XCOPY command is external (it is contained within the\r\nXCOPY.EXE file). Generally, the distinction between internal and external commands is unimportant, except that\r\ninternal commands have no associated executable file.\r\nThe following are all of the Windows NT internal commands:\r\nASSOC CALL CHDIR/CD CLS\r\nCOLOR COPY DATE DIR\r\nDPATH ECHO ENDLOCAL ERASE/DEL\r\nEXIT FOR FTYPE GOTO\r\nIF MKDIR/MD MOVE PATH\r\nPAUSE POPD PROMPT PUSHD\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 17 of 43\n\nREM RENAME/REN RMDIR/RD SET\r\nSETLOCAL SHIFT START TIME\r\nTITLE TYPE VER\r\nTwo environment variables are intimately associated with shell command execution: PATH and PATHEXT.\r\nThe PATH environment variable defines the Windows NT search path. The search path is a list of directories that\r\nare searched when the command shell attempts to locate an executable file. Separate directories in the path list\r\nwith semi-colons. For example, a typical path might contain:\r\nd:\\winnt40\\system32;d:\\winnt40;d:\\ntreskit;c:\\bin;c:\\dos\r\nThe PATH command manipulates the PATH environment variable, although the variable can also be directly\r\nmanipulated via the SET command (see Chapter 3 for a description of the SET command). To set a new system\r\npath, follow the PATH command with a new path list. For example:\r\nC:\\\u003epath c:\\bin;c:\\scripts;d:\\winnt\r\nThis PATH command tells Windows NT to search the C:\\BIN, C:\\SCRIPTS, and D:\\WINNT directories for\r\nexecutable files.\r\nEnter a PATH command without any arguments to display the current search path. For example:\r\n1. C:\\\u003epath\r\n2. PATH= d:\\winnt40\\system32;d:\\winnt40;d:\\ntreskit;c:\\bin;c:\\dos\r\nOne common use of the PATH command is adding a new directory to the search path. To do this, specify the\r\nexisting search path as part of the new path. For example, to add a new directory, C:\\NEWDIR, to the start of the\r\npath, use this command:\r\nC:\\\u003epath c:\\newdir;%PATH%\r\nUse this command to add the same directory to the end of the path:\r\nC:\\\u003epath %PATH%;c:\\newdir\r\nThe PATH variable is initialized from the following sources of information:\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 18 of 43\n\nThe System Environment, which is set via the Control Panel System icon.\r\nThe User Environment, which is set via the Control Panel System icon.\r\nAny PATH statements in AUTOEXEC.BAT (if parsing is enabled).\r\nAt logon time, path information from the sources listed above is concatenated together to form the initial path.\r\nAfter logging on, the PATH command is used to alter the path.\r\nThe PATHEXT environment variable defines the list of file extensions checked by Windows NT when searching\r\nfor an executable file. Like the PATH variable, semi-colons separate individual items in the PATHEXT variable.\r\nThe default value of PATHEXT is .COM;.EXE;.BAT;.CMD. The PATHEXT variable is manipulated via the SET\r\ncommand. For example, to add the .PL extension, use the following command:\r\nC:\\\u003eset PATHEXT=%PATHEXT%;.pl\r\nThe following section describes how Windows NT and the command shell use the PATH and PATHEXT variables.\r\nWhen a command is submitted for execution (either by typing or as part of a script), the shell performs the\r\nfollowing actions:\r\n1. All parameter and environment variable references are resolved (see chapter 3).\r\n2. Compound commands are split into individual commands and each is then individually processed\r\naccording to the following steps (see the section \"Running Multiple Commands\" for details of compound\r\ncommands). Continuation lines are also processed at this step.\r\n3. The command is split into the command name and any arguments.\r\n4. If the command name does not specify a path, the shell attempts to match the command name against the\r\nlist of internal shell commands. If a match is found, the internal command executes. Otherwise, the shell\r\ncontinues to step 5.\r\n5. If the command name specifies a path, the shell searches the specified path for an executable file matching\r\nthe command name. If a match is found, the external command (the executable file) executes. If no match\r\nis found, the shell reports an error and command processing completes.\r\n6. If the command name does not specify a path, the shell searches the current directory for an executable file\r\nmatching the command name. If a match is found, the external command (the executable file) executes. If\r\nno match is found, the shell continues to step 7.\r\n7. The shell now searches each directory specified by the PATH environment variable, in the order listed, for\r\nan executable file matching the command name. If a match is found, the external command (the executable\r\nfile) executes. If no match is found, the shell reports an error and command processing completes.\r\nIn outline, if the command name does not contain a path, the command shell first checks to see if the command is\r\nan internal command, then checks the current directory for a matching executable file, and then checks each\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 19 of 43\n\ndirectory in the search path. If the command name does contain a path, the shell only checks the specified\r\ndirectory for a matching executable file.\r\nIf the command name includes a file extension, the shell searches each directory for the exact file name specified\r\nby the command name. If the command name does not include a file extension, the shell adds the extensions listed\r\nin the PATHEXT environment variable, one by one, and searches the directory for that file name. Note that the\r\nshell tries all possible file extensions in a specific directory before moving on to search the next directory (if there\r\nis one).\r\nFor example, the following command explicitly specifies the path, command name, and file extension:\r\nC:\\\u003ec:\\bin\\edit.exe\r\nThis command executes the program EDIT.EXE found in the directory C:\\BIN. If the program is not found, the\r\nshell reports an error.\r\nThis example omits the path to EDIT.EXE:\r\nC:\\\u003eedit.exe\r\nTo execute this command, the shell searches the current directory and then each directory in the search path until\r\nEDIT.EXE is found, or reports an error if the file is not found.\r\nThis example omits the path and file extension:\r\nC:\\\u003eedit\r\nTo execute this command, the shell searches the current directory and then each directory in the search path.\r\nAssuming that the PATHEXT variable contains .COM;.EXE;.BAT;.CMD, each directory is searched for\r\nEDIT.COM, EDIT.EXE, EDIT.BAT and EDIT.CMD before the shell moves on to the next search directory.\r\nOnce the command shell resolves the command name either to an internal command or an external executable file,\r\nit executes the command as follows:\r\nIf the command is internal, the shell executes it directly.\r\nIf the command is a 16-bit or 32-bit Windows GUI executable program, the shell runs the program but does\r\nnot wait for the command to complete.\r\nIf the command is a 32-bit console application, or a 16-bit MS-DOS application, the shell runs the\r\ncommand in the current console window and waits for the command to complete.\r\nIf the command is a script file (.BAT or .CMD), the shell switches to script mode and begins executing the\r\nscript.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 20 of 43\n\nIf the command is a document or data file name associated with an application, the shell executes the\r\nappropriate application. The shell applies the previous rules based upon the type of the application\r\nassociated with the data or document file. See the following section for more information on file\r\nassociations.\r\nNotice that the command shell does not wait for GUI applications to complete execution before it continues. This\r\nbehavior can be modified using the START command, described in the following section.\r\nWindows NT provides a database, in the system registry, which allows files to be associated with a particular\r\napplication. The primary use of this association is called automatic application launching. This feature is used\r\nextensively in the GUI environment: whenever a data file or document is double-clicked, the associated\r\napplication automatically launches, and the specified file or document then opens within that application.\r\nTip File associations can also be edited using Windows NT Explorer (using the View, Options command).\r\nHowever, the commands presented here have a finer degree of control than that provided by Explorer. For\r\nexample, Explorer provides no easy way to delete an individual association.\r\nFile associations are also applicable to the command shell environment. For example, suppose Microsoft Word is\r\ninstalled on a computer. During installation of Word, .DOC files are associated with the Word application. Once\r\nthis is done, it is possible to launch Word and open a .DOC file from a shell command simply by typing its name.\r\nFor example:\r\nC:\\\u003ec:\\docs\\letter.doc\r\nThis command launches Word and opens the file C:\\DOCS\\LETTER.DOC. If the file is in the current directory,\r\nthe path is not required. For example:\r\nC:\\docs\u003eletter.doc\r\nIn fact, the command shell applies the same rules when opening a document or data file as it does when searching\r\nfor any external command. Thus, in the previous example, the shell looks for LETTER.DOC in the current\r\ndirectory and in all directories specified by the search path. If the C:\\DOCS directory is added to the search path,\r\nthe file LETTER.DOC can be opened in Word from any directory, merely by typing its name. For example:\r\n1. C:\\docs\u003epath c:\\docs;%PATH%\r\n2. C:\\docs\u003ecd ..\r\n3. C:\\\u003eletter.doc\r\nAs previously noted, the file association database is maintained in the Windows NT Registry. Therefore, changes\r\nmade to file associations are persistent—they are retained even after the computer is reset. In addition, the\r\ndatabase is maintained in the HKEY_LOCAL_COMPUTER portion of the registry. Thus, changes made to the\r\nfile association database effect all users of a particular computer.\r\nThe file association database works with three items of information:\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 21 of 43\n\nfile extensions\r\nfile types\r\nlaunch commands\r\nA file extension is the familiar file name suffix after the last period in the file name. For example,\r\nMYSCRIPT.BAT has a file extension of .BAT. Most file extensions are one, two, or three characters long. A file\r\ntype is a name for a particular class of file. For example, Windows NT scripts might be assigned a file type of\r\nWindows.Script. A launch command is a prototype command used to launch the associated application. For\r\nexample, NOTEPAD.EXE %1 is a typical launch command.\r\nWhen a document or data filename is specified as a command name, the shell uses the file association database to\r\nlaunch the correct application. It does this as follows:\r\n1. The shell extracts the file extension from the specified document file. It then searches the database for a\r\nmatching file association. If no match is found, the shell reports an error and command processing\r\ncompletes.\r\n2. The shell then obtains from the database the name of the file type associated with the file extension.\r\n3. The shell then searches the database again for the specified file type. If no match is found, the shell reports\r\nan error and command processing completes.\r\n4. The shell obtains from the database the launch command associated with the file type.\r\n5. The shell now parses the launch command, replacing any parameters with arguments specified in the\r\noriginal shell command.\r\n6. Finally, the shell executes the parsed command. Typically, this launches the associated application.\r\nThis procedure is best understood through an example. Consider this command:\r\nC:\\docs\u003eletter.doc\r\nThe shell first extracts the file extension, .DOC, and searches the database for the file type associated with this file\r\nextension. Typically, this yields a file type such as Word.Document.8. The shell now uses this file type to search\r\nfor the launch command. Assume this is WINWORD.EXE %1. The %1 in this command is a formal parameter,\r\nwhich the shell replaces with the document file name (LETTER.DOC). So the final launch command is\r\nWINWORD.EXE LETTER.DOC. When this command executes, Word runs, and the LETTER.DOC file is\r\nopened.\r\nFile types are thus an intermediary between a file extension and a launch command. They exist to allow multiple\r\nfile extensions to be associated with the same launch command. For example, a paint program might need to\r\nassociate .BMP, .JPG, .TIF and .TGA files with a launch command. Rather than entering the same launch\r\ncommand in the database four times, the program uses a single file type and launch command, and then associates\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 22 of 43\n\nthe file extensions with this type. Any subsequent changes made to the launch command are then automatically\r\napplied to all associated file extensions.\r\nThe ASSOC, FTYPE and ASSOCIATE commands are used to manipulate the file association database. The\r\nASSOC command connects a file extension with a file type, and the FTYPE command connects a file type with a\r\nlaunch command.\r\nUse the FTYPE command to create or edit a file type and associate it with a launch command. For example:\r\nC:\\\u003eftype REXX.File=c:\\rexx\\rexx.exe \"%1\"\r\nThis creates the file type REXX.File and associates the prototype command C:\\REXX\\REXX.EXE \"%1\" with the\r\ntype. Notice the use of double quotes around the %1 parameter. This ensures that the command is handled\r\ncorrectly even if the specified document name contains spaces.\r\nTip When defining a prototype command, it is advisable to include the full path name of the executable file,\r\nunless the directory containing the executable will always be part of the search path.\r\nThe FTYPE command can also display the current launch command for a file type. For example:\r\n1. C:\\\u003eftype REXX.File\r\n2. REXX.File=REXX \"%1\"\r\nFinally, the command and file type can be deleted. For example:\r\n1. c:\\\u003eftype REXX.File=\r\n2. c:\\\u003eftype REXX.File\r\n3. File type 'REXX.File' not found or no open command associated with it.\r\nOnce a file type and launch command are set up, the ASSOC command associates file extensions with that file\r\ntype. For example:\r\n1. C:\\\u003eassoc .rex=REXX.File\r\n2. .rex=REXX.File\r\nThe current association of a file type is displayed using ASSOC. For example:\r\n1. C:\\\u003eassoc .rex\r\n2. .rex=REXX.File\r\nFinally, the file extension association can be deleted. For example:\r\nC:\\\u003eassoc .rex=\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 23 of 43\n\nNotice that for both FTYPE and ASSOC, specifying the file type or file extension only displays the current\r\nassociation. Specifying the file type or file extension with a trailing = character deletes the current association.\r\nThe ASSOCIATE [RK] command provides a shorthand method to perform an ASSOC and FTYPE in one step.\r\nWhile not as versatile as the ASSOC/FTYPE combination, it is easier to use. ASSOCIATE directly associates a\r\nfile extension with an application, providing the necessary file type and launch command automatically. For\r\nexample:\r\nC:\\\u003eassociate .rex c:\\rexx\\rexx.exe\r\nThis command directly associates .REX files with the REXX.EXE application. The association can be deleted\r\nusing the following command:\r\nC:\\\u003eassociate .rex /d\r\nMore information on the ASSOC, FTYPE and ASSOCIATE commands can be found in the \"Command\r\nReference.\"\r\nBy using a combination of the PATH and PATHEXT variables, along with the application association database,\r\ncomplete integration of new command types into the command shell is possible.\r\nFor example, suppose a REXX interpreter, REXX.EXE, is installed in the C:\\REXX directory. REXX scripts can\r\nthen be executed using commands such as:\r\nC:\\\u003ec:\\rexx\\rexx myrexx.rex arg1 arg2 arg3\r\nThis executes the REXX interpreter, REXX.EXE, which then interprets and runs the script MYREXX.REX. In\r\nthis example, ARG1 etc. are arguments passed to the script.\r\nBy adding an association to the file association database, the invocation of the REXX interpreter can be made\r\nimplicit. For example:\r\n1. C:\\\u003eftype REXX.File=c:\\rexx\\rexx.exe \"%1\" %*\r\n2. C:\\\u003eassoc .rex=REXX.File\r\nThese commands create the needed associations, so that a .REX file launches the REXX.EXE interpreter. Notice\r\nthat by including the full path name in the prototype command the interpreter executes without adding the\r\nC:\\REXX directory to the search path. In addition, the %1 parameter is placed in double quotes so that .REX\r\nfilenames containing spaces are correctly handled. Finally, the special %* parameter represents all additional\r\narguments following the first (in this case ARG1 ARG2 ARG3). Now, the original script command can be\r\nsimplified to:\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 24 of 43\n\nC:\\\u003emyrexx.rex arg1 arg2 arg3\r\nBy adding the file extension .REX to the list of file extensions specified with PATHEXT, the .REX file extension\r\ncan also be made implicit. For example:\r\n1. C:\\\u003eset PATHEXT=%PATHEXT%;.REX\r\n2. C:\\\u003emyrexx arg1 arg2 arg3\r\nAt this point, entering a REXX script command to execute is as convenient as entering any native shell command.\r\nFinally, the MYREXX.REX script can be stored in a central directory, and that directory added to the system\r\nsearch path. For example:\r\n1. C:\\\u003emkdir c:\\rexxscripts\r\n2. C:\\\u003emove myrexx.rex c:\\rexxscripts\r\n3. C:\\\u003eset PATH=%PATH%;c:\\rexxscripts\r\n4. C:\\\u003emyrexx arg1 arg2 arg3\r\nAt this point, the MYREXX.REX script can be executed in any directory, just by typing its name.\r\nTip File association database changes are persistent. However, changes to environment variables are not.\r\nTherefore, the changes to the PATH and PATHEXT variables shown in the prior examples are lost when the\r\nsystem is shutdown, or the current shell terminated. To make these changes persistent, edit the PATH and\r\nPATHEXT variables in the Environment tab of the Control Panel System applet.\r\nPrevious sections described how the command shell implicitly interprets and executes a basic command. The\r\nSTART command explicitly executes a shell command, and provides additional control over how the command is\r\nhandled by Windows NT.\r\nThe syntax of the START command is:\r\nSTART [\"title\"] [switches] command-name [args]\r\nThe first item following the START command is an optional window title, enclosed in double quotes. By default,\r\nthe START command executes the specified command in a new window. In this case, the title text is used as the\r\nwindow title.\r\nFollowing the title are zero or more switches that control the operation of the START command. A command-name must then be present, which specifies the command to run. Following the command name are zero or more\r\ncommand arguments, which are passed to the specified command.\r\nTip All START switches and options must appear before the command-name. Switches and options placed after\r\nthe command-name are passed, unaltered, to the command being started.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 25 of 43\n\nWithout any of the optional switches, the START command executes the specified command. It does this as\r\nfollows:\r\nIf the command is a 16-bit or 32-bit Windows GUI executable program, the START command runs the\r\nprogram but does not wait for the command to complete.\r\nIf the command is a 32-bit console application, or a 16-bit MS-DOS application, the START command\r\nruns the command in a new console window but does not wait for the command to complete.\r\nIf the command is a script file (.BAT or .CMD), or an internal command, the START command executes a\r\nnew command shell (CMD.EXE) in a new console window. The script or internal command is executed\r\nusing the /K switch. The START command does not wait for the command or script to complete.\r\nIf the command is a document or data file name associated with an application, the START command\r\nexecutes the appropriate application. The START command applies the previous rules based upon the type\r\nof the application associated with the data or document file.\r\nNotice that the operation of the START command differs from the default command shell sequence:\r\nFirst, the START command never waits for the command to complete.\r\nSecond, all console commands start in a new console window, rather than the current console window.\r\nFinally, if the command is a script file or internal shell command, the START command executes the\r\ncommand using a new command shell (CMD.EXE) with the /K switch. This means that, after the script of\r\ninternal command completes, the new command shell does not terminate, but instead enters interactive\r\nmode. For example:\r\nC:\\\u003estart \"New Window\" dir\r\nThis command executes a DIR command in a new console window, giving the new window the title New\r\nWindow. Since DIR is an internal command, the new command shell enters Interactive mode and prompts for\r\nadditional commands after the DIR command completes.\r\nTo over-ride this behavior, explicitly execute a new command shell and use the /C switch. For example:\r\nC:\\\u003estart \"New Window\" cmd /c dir\r\nIn this example, the command-name for the START command is actually CMD. The /C switch and DIR command\r\nare arguments to the CMD.EXE command shell. The START command therefore executes the command CMD /C\r\nDIR in a new console window. When the DIR command completes, the new command shell terminates, and the\r\nnew console window closes.\r\nThe START command is most useful when one or more of the optional switches are used. All START switches\r\nfollow the window title (if used) and precede the command name.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 26 of 43\n\nThe /D switch specifies a new current drive and directory for the command. Without /D, the command inherits the\r\ndrive and directory of the command shell. Follow the /D switch with the new drive and directory. For example:\r\nC:\\\u003estart /dc:\\book dir\r\nThe /I switch controls environment inheritance. Normally, all commands executed by the command shell inherit a\r\ncopy of the current environment (this is described in detail in Chapter 3). The /I switch causes the command to\r\ninherit the environment as it existed when the command shell was first started. Thus, any changes made to the\r\nenvironment within the current command shell are not passed to the executed command.\r\nThe /MIN and /MAX switches specify the initial state of the new window created by the START command. These\r\nswitches apply to all applications and commands, including 16 and 32-bit GUI applications. Without /MIN or\r\n/MAX, the START command creates the new window using Windows NT default settings. The /MIN switch\r\ncreates the new window minimized, that is, as a task bar button. The /MAX switch creates the new window\r\nmaximized, that is, occupying the entire screen.\r\nThe /LOW, /NORMAL, /HIGH and /REALTIME switches set the priority class for the command or application.\r\nBy default, the START command executes all commands or applications at normal priority. The /LOW switch\r\nexecutes the application at low priority, while the /HIGH switch executes the application at high priority. The\r\n/REALTIME switch executes the application at real-time priority.\r\nTroubleshooting Tip Use of the /REALTIME switch is strongly discouraged, as its use can compromise Windows\r\nNT stability.\r\nUsing /LOW to execute an application at low priority is a very useful way of running a low priority background\r\ntask. For example:\r\nC:\\\u003estart \"Cleanup\" /low /min cmd /c cleanup.bat\r\nThis command executes the CLEANUP.BAT script at low priority. The /MIN switch is used so that the script\r\nappears only as a button on the task bar.\r\nAs described above, the START command does not wait for the new command to complete; a new command\r\nprompt appears immediately and new commands can be executed at once. When used in a script, the next line in\r\nthe script executes immediately. The /WAIT switch makes the START command wait for the command to\r\ncomplete before continuing. This switch applies to all commands and applications, including GUI applications.\r\nFinally, the /B switch executes the command without creating a new console window. This switch is application\r\nonly to internal commands and external console applications—it is ignored if the command specifies a GUI\r\napplication. Using /B also implies the /WAIT switch.\r\nThe command shell provides the following simple commands to control script output:\r\nThe REM command, which is used for script comments.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 27 of 43\n\nThe CLS command, which clears the console window.\r\nThe COLOR command, which controls colors used in the console window.\r\nThe TITLE command, which changes the console window title bar text.\r\nThe @ command, which controls command echo on a line-by-line basis.\r\nThe ECHO command, which controls command echo and also displays text.\r\nThe NOW [RK] command, which displays time-stamped text.\r\nThe REM (remark) command is the simplest script command because it does nothing. Any text can follow the\r\nREM command. REM commands are used for shell comments, and should be used liberally within a script (for\r\nexample, to clarify complex script commands and document the script logic). The REM command also suppresses\r\nthe meaning of reserved shell characters within the remark text. For example, the following is a valid remark:\r\nC:\\\u003erem This is valid in a REM statement: \u0026, \u0026\u0026, ||, ^\r\nNormally, the shell assigns special meanings to \u0026, \u0026\u0026 etc., but in a remark command these characters are treated\r\nas regular text.\r\nTroubleshooting Tip Because the REM command suppresses the normal interpretation of special shell characters,\r\nREM commands cannot appear within a multi-line command. For example, the following is invalid:\r\n1. if \"%X%\"==\"ABC\" (\r\n2. rem An illegal comment!\r\n3. goto :EXIT\r\n4. )\r\nThe shell combines multi-line commands (like the one above) into a single line before executing them. Therefore,\r\nthe shell \"sees\" the command like this:\r\nif \"%X%\"==\"ABC\" ( rem An illegal comment! \u0026 goto :EXIT )\r\nThe REM command will include all the text on the line, up to and including the closing parenthesis. The GOTO\r\ncommand is not seen as a command at all, but instead as additional comment text.\r\nThe CLS command clears the current console window and positions the cursor to the top left of the window.\r\nSubsequent command output begins at the top of the window and works down the screen. The screen is cleared\r\nusing the current window colors. The CLS command is useful when a script needs to present un-cluttered output.\r\nThe COLOR command sets the text and background colors for the console window. When a console window is\r\nstarted, it uses the colors set using the Console Window property sheet Colors tab, as described in chapter 1. The\r\nCOLOR command without any arguments returns the console window to these default colors. With a single\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 28 of 43\n\nargument, the COLOR command sets the text and background colors. The argument must be two characters long.\r\nThe first character specifies the background color and the second character specifies the text color. Table 2.3\r\nshows the color codes used.\r\nTable 2.3 COLOR Command Color Codes\r\nCode Color Code Color\r\n0 Black 8 Gray\r\n1 Blue 9 Light Blue\r\n2 Green A Light Green\r\n3 Aqua B Light Aqua\r\n4 Red C Light Red\r\n5 Purple D Light Purple\r\n6 Yellow E Light Yellow\r\n7 White F Bright White\r\nFor example, this command sets a pleasing white on blue color combination:\r\nC:\\\u003ecolor 17\r\nThe TITLE command changes the title bar of the console window to the specified text. The text in the\r\ncorresponding button on the task bar is also changed. The TITLE command is useful to show the progress of long\r\nor complex scripts. It is superior to ECHO for this purpose as it does not scroll text in the console window, and the\r\ntext is visible in the task bar even when the console window is minimized. For example:\r\nC:\\\u003etitle Backup Drive C:...\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 29 of 43\n\nThe @ command controls command echo on a line-by-line basis. By default, the shell displays each command in a\r\nscript before it is executed. Prefix a command with an @ character to suppress command echo. For example:\r\n@title Welcome to the script!\r\nThe @ command can be placed within a compound command (described in the upcoming section \"Running\r\nMultiple Commands\") to control echo of individual portions of the command, although the utility of this is\r\nquestionable. For example, this compound command will not echo when executed as part of a script:\r\n@(echo starting...)\u0026(title Script phase 1)\r\nHowever, the command\r\n(@echo starting...)\u0026(title Script phase 1)\r\nechoes as:\r\nC:\\\u003e( )\u0026(title Script phase 1)\r\nThe @ command controls command echo on a command-by-command basis. The ECHO command controls\r\ncommand echo for an entire script. This command disables command echo:\r\nC:\\\u003eecho off\r\nThis command enables command echo:\r\nC:\\\u003eecho on\r\nTo display the current state of command echo, enter:\r\n1. C:\\\u003eecho\r\n2. ECHO is on.\r\nThe ECHO command is typically used at the start of a script to disable command echo for the duration of the\r\nscript. For example, many scripts begin with this line:\r\n@echo off\r\nThis disables echo for the entire script. The echo of the ECHO command is itself suppressed by using the @\r\ncommand. (The example above is the first special script line that was shown in the section \"Special Script Lines\"\r\nin chapter 1.) Once echo is disabled (or enabled) in a script, the echo state is maintained within the script and\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 30 of 43\n\nwithin script procedures and nested scripts. The only exception to this is nested scripts executed via a CMD or\r\nSTART command. These scripts run in a new command shell, which always starts with echo enabled.\r\nEcho can be disabled interactively at a command prompt by entering an ECHO OFF command. In this case, the\r\ncommand prompt is suppressed until echo is enabled again. Typed commands are still echoed during entry,\r\nhowever.\r\nThe current echo state is used when the command shell switches from Interactive mode to script mode to begin\r\nexecuting a script. Thus, if echo is enabled interactively, the script begins with echo enabled, and vice versa.\r\nHowever, the shell remembers the original interactive echo state, and recalls it when the script completes.\r\nTherefore, after a script ends, the echo state reverts to the value it had before the script began execution.\r\nThe ECHO command is also used to echo arbitrary text to the console window. For example:\r\nC:\\\u003eecho Hello, world!\r\nIf command echo is disabled, the command itself is not echoed, but the text specified in the ECHO command is\r\nechoed. When used this way, the ECHO command is the Windows NT script equivalent of the PRINT statement\r\nfound in many languages. Typically, ECHO is used with environment variable substitution, which is described in\r\nChapter 3.\r\nTip The ECHO command cannot be used to echo an empty line. For example:\r\n1. C:\\\u003eecho\r\n2. ECHO is on.\r\nAs can be seen, the ECHO command displays the current state of command echo, not an empty line. The nearest\r\nequivalent is to echo something inconsequential, such as a single period. For example:\r\n1. C:\\\u003eecho .\r\n2. .\r\nThe NOW [RK] command also displays arbitrary text. However, NOW prefixes the text with the current time and\r\ndate. This is useful when the time taken to execute a command must be monitored. For example:\r\n1. E:\\workdir\u003enow Start\r\n2. Wed Oct 21 12:20:07 1997 -- Start\r\n3. E:\\workdir\u003eecho Quick command\r\n4. Quick command\r\n5. E:\\workdir\u003enow End\r\n6. Wed Oct 21 12:20:18 1997 -- End\r\n7. E:\\workdir\u003e\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 31 of 43\n\nMost console applications and commands generate output, and many accept input. This input or output is in the\r\nform of a stream of characters (either ANSI or Unicode). Applications generally work with up to three streams, as\r\nfollows:\r\nThe command input stream is used by the application or command to read input. By default this stream\r\ncomes from keys typed at the keyboard.\r\nThe command output stream is used by the application or command to display output. By default, this\r\nstream is displayed in the console window.\r\nThe command error output stream is used by the application or command to display errors. By default, this\r\nstream is displayed in the console window.\r\nThe default stream input and output provides normal interactive command operation: input is obtained from the\r\nkeyboard, and output is displayed in the console window. Note that the distinction between the command output\r\nstream and the command error output stream is somewhat arbitrary. An application or command can direct output\r\nto whichever stream it wishes. Typically, however, normal output is sent to the command output stream, and errors\r\nare sent to the command error output stream.\r\nThe command shell provides facilities to change the default stream input and output. These facilities are accessed\r\nby placing special command redirection symbols in a command. Table 2.4 shows the command redirection\r\nsymbols.\r\nTable 2.4 Command Redirection Symbols\r\nSymbol Description\r\n\u003efile\r\nRedirects command output to the file specified. You can also use a standard device name such as\r\nLPT1, CON, PRN or CONOUT$ as the file name. Any preexisting contents of the file are lost.\r\n\u003e\u003efile\r\nRedirects command output to the file specified. If the file already exists, all command output is\r\nappended to the end of the file.\r\n\u003cfile\r\nRedirects command input from the file specified. You can also use a standard device name such\r\nas CON or CONIN$.\r\n2\u003efile\r\nRedirects command error output to the file specified. You can also use a standard device name\r\nsuch as LPT1, CON, PRN or CONOUT$ as the file name. Any preexisting contents of the file\r\nare lost.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 32 of 43\n\nSymbol Description\r\n2\u003e\u00261\r\nRedirects command error output to the same location as command output. This makes any\r\ncommand output redirection also apply to command error output.\r\ncmd1 |\r\ncmd2\r\nPipes the command output of cmd1 to the command input of cmd2. Multiple pipe characters are\r\nallowed, creating a chain of commands, each sending output to the next command in the chain.\r\nCommand redirection symbols are not visible to the command. The shell processes them before the command is\r\nexecuted and they are not passed as arguments to the command. The \u003c, \u003e, and | symbols are reserved shell\r\ncharacters. If these symbols must be passed as command arguments, instead of being used as redirection symbols,\r\nthen they must be escaped using the ^ character.\r\nThe \u003e redirection symbol redirects command output to the specified file. For example:\r\nC:\\\u003edir \u003ec:\\dir.txt\r\nThis example creates a text file C:\\DIR.TXT containing the output of the DIR command. The \u003e symbol can be\r\nplaced anywhere in the command, but is typically placed at the end of the command. A space is permitted between\r\nthe \u003e symbol and the file name. If the file specified by the redirection symbol already exists, any existing contents\r\nare deleted before the command is executed.\r\nTip Only one command output redirection symbol is allowed per command. It is not possible, for example, to\r\nduplicate command output by redirecting the command output to multiple files.\r\nThe \u003e\u003e redirection symbol redirects command output to the specified file, but concatenates the output onto the\r\nend of the file. For example:\r\nC:\\\u003edir \u003e\u003e c:\\dir.txt\r\nThis example adds the output of the DIR command to the end of the file C:\\DIR.TXT. If the file specified does not\r\nexist, it is created.\r\nThe \u003c redirection symbol redirects command input from the specified file. For example:\r\nC:\\\u003esort \u003cc:\\dir.txt\r\nThis command sorts the contents of the file C:\\DIR.TXT and then displays the result. A space is permitted\r\nbetween the \u003c symbol and the file name. The \u003c symbol is used less frequently than \u003e and \u003e\u003e, since there are few\r\nshell commands which accept console input.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 33 of 43\n\nThe 2\u003e redirection symbol redirects command error output to the specified file. For example:\r\nC:\\\u003edir 2\u003ec:\\error.txt\r\nThis example redirects the error output of the DIR command to the file C:\\ERROR.TXT. A space is permitted\r\nbetween the 2\u003e symbol and the file name. Notice that this example does not redirect regular command output, so\r\nthe directory listing is still displayed. Only error output (if any) is captured to the file.\r\nThe redirection symbols can be combined in a single command. For example:\r\nC:\\\u003esort \u003cc:\\dir.txt \u003ec:\\sortdir.txt 2\u003ec:\\error.txt\r\nThe 2\u003e\u00261 redirection symbol redirects command error output to command output. This means that command\r\nerror output is sent to the same destination as command output. For example:\r\nC:\\\u003edir \u003ec:\\dir.txt 2\u003e\u00261\r\nThis command sends both command output and command error output to the file C:\\DIR.TXT.\r\nThe | (pipe) redirection symbol sends the command output of cmd1 to the command input of cmd2. For example:\r\nC:\\\u003edir | sort\r\nThis example sends the command output of the DIR command to the command input of the SORT command. The\r\noutput from the SORT command is then displayed. Alternatively, the SORT output can be sent to another\r\ncommand. For example:\r\nC:\\\u003edir | sort | more\r\nThis example sends the command output of the DIR command to the command input of the SORT command.\r\nThen, the command output of the SORT command is sent to the command input of the MORE command. Finally,\r\nthe command output of the MORE command is displayed.\r\nWhen the command shell processes a pipe (|) symbol, it actually runs both commands specified simultaneously.\r\nThe right hand command is suspended until the left hand command begins generating command output. Then, the\r\nleft hand command wakes up and processes the output. When this output has been processed, the command is\r\nagain suspended until more input is available. The synchronization of both commands is handled automatically by\r\nthe command shell and Windows NT.\r\nThe pipe symbol is both a redirection symbol and a compound command symbol. Compound commands are\r\ndiscussed in the next section.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 34 of 43\n\nCommand redirection effects are inherited by nested commands. If a command starts with its command output\r\nredirected, and this command then starts additional commands, these commands inherit the same redirection as the\r\nparent command. This is frequently used with nested shells to capture all script output to a file. For example:\r\nC:\\\u003ecmd /c myscript.bat \u003eresult.txt\r\nThis command executes the script MYSCRIPT.BAT in a new shell. Since the new command shell has its\r\ncommand output redirected to the file RESULT.TXT, all commands run by the shell (i.e. those in the script) also\r\nhave their output redirected.\r\nBy default, all command input and output is processed as ANSI (or ASCII) characters. However, if the shell is\r\nstarted with the /U switch (see the \"CMD\" section in the \"Command Reference\"), command input and output is\r\nprocessed as Unicode characters. In this case, if a command generates ANSI output, the shell automatically\r\nconverts ANSI command output to Unicode.\r\nIn previous sections, the simple \"command and arguments\" syntax of a shell command was described. The shell\r\nalso supports compound commands, where a command line specifies more than one command to execute.\r\nCompound commands are indicated by special compound command symbols. Table 2.5 shows the compound\r\ncommand symbols.\r\nTable 2.5 Compound Command Symbols\r\nSymbol Description\r\ncmd1 \u0026\r\ncmd2\r\nExecutes command cmd1, then command cmd2. Additional commands can be added using\r\nadditional ampersand symbols.\r\ncmd1 \u0026\u0026\r\ncmd2\r\nExecutes command cmd1, then executes command cmd2 only if cmd1 completed\r\nsuccessfully.\r\ncmd1 || cmd2\r\nExecutes command cmd1, then executes command cmd2 only if cmd1 did not complete\r\nsuccessfully.\r\n( )\r\nUse parentheses to indicate the nesting of complex multi-command sequences. Also used in\r\nIF ... ELSE commands.\r\nThe \u0026, |, (, and ) symbols are reserved shell characters. If these symbols must be passed as command arguments,\r\ninstead of being used as compound command symbols, then they must be escaped using the ^ character.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 35 of 43\n\nThe simplest compound command symbol is \u0026. The \u0026 symbol separates multiple commands on a single\r\ncommand line. For example:\r\n1. C:\\\u003eecho Command 1 \u0026 echo Command 2\r\n2. Command 1\r\n3. Command 2\r\nThis example shows two ECHO commands on a single line. When \u0026 is used to separate multiple commands, the\r\ncommands execute one at a time, starting with the first command. Each command runs to completion before the\r\nnext command executes.\r\nAny number of commands can be placed on a single line using the \u0026 symbol. For example:\r\nC:\\\u003edir c:\\bin \u003efiles.txt \u0026 dir c:\\dos \u003e\u003efiles.txt \u0026 type files.txt\r\nThis example accumulates the results of two DIR commands into the file FILES.TXT and then displays the\r\ncontents of this file. This example also shows that command redirection symbols can be used with each individual\r\ncommand in a compound command.\r\nThe \u0026\u0026 and || compound command symbols provide conditional command execution. The first (left) command is\r\nexecuted. If the \u0026\u0026 symbol is used, the second command executes only if the first command completed\r\nsuccessfully. If the || symbol is used, the second command executes only if the first command did not complete\r\nsuccessfully. A command completes successfully if it returns an exit code of 0 or no exit code at all. Exit codes are\r\ndiscussed in chapter 3. For example:\r\nC:\\\u003everify on || echo Verify command failed!!\r\nSince the VERIFY command executed successfully, the ECHO command was not executed. However, in this\r\nexample:\r\n1. C:\\\u003everify ox || echo Verify command failed!!\r\n2. An incorrect parameter was\r\n3. entered for the command.\r\n4. Verify command failed!!\r\nThe ECHO command was executed because the VERIFY command syntax was incorrect, causing the command\r\nto exit with a non-zero error code.\r\nMultiple commands can be chained together using additional \u0026\u0026 and || compound command symbols. For\r\nexample:\r\nC:\\\u003edir \u0026\u0026 copy a b \u0026\u0026 echo OK!\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 36 of 43\n\nThe COPY command executes only if the DIR command succeeds, and the ECHO command executes only if the\r\nCOPY command succeeds.\r\nThe parentheses symbols ( and ) are used to resolve command ambiguities and indicate the binding of compound\r\ncommand and redirection symbols. They are also used in the IF command and to specify multi-line commands.\r\nFor example, the following command collects two directory listings into the file FILES.TXT:\r\nC:\\\u003edir *.exe \u003efiles.txt \u0026 dir *.com \u003e\u003efiles.txt\r\nThe following command might appear to do the same, but in fact it does not work:\r\nC:\\\u003edir *.exe \u0026 dir *.com \u003efiles.txt\r\nThis second example fails because the command redirection symbols have a higher precedence than the\r\ncompound command symbols. The shell interprets this command as follows:\r\n1. C:\\\u003edir *.exe\r\n2. C:\\\u003edir *.com \u003efiles.txt\r\nThis displays the result of the first DIR command in the console window—which is not the desired effect.\r\nThe second example can be corrected by using parentheses to alter the binding of the various symbols, as follows:\r\nC:\\\u003e(dir *.exe \u0026 dir *.com) \u003efiles.txt\r\nThis compound command executes two DIR commands, one after the other. The output of both commands is\r\nredirected into the file FILES.TXT. The parentheses change the binding of the command symbols. Notice that this\r\ncommand is far cleaner than the earlier example, and that the file FILES.TXT is only specified once in the\r\ncommand.\r\nParentheses can be nested to specify arbitrarily complex compound commands. For example:\r\nC:\\\u003e((echo command1) \u0026 (echo command2)) \u0026\u0026 (echo command 3)\r\nAn opening parenthesis can be placed anywhere on a command where a command-name is expected. When a\r\ncommand is enclosed in parentheses, either a closing parenthesis or a compound command symbol marks the end\r\nof the command. For example:\r\n1. C:\\\u003eecho (command)\r\n2. (command)\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 37 of 43\n\n3. c:\\\u003e(echo command)\r\n4. command\r\nNotice that the second ECHO command did not echo the closing parenthesis. In this case, the shell treats this as\r\nthe end of the command, and this parenthesis is not passed as part the ECHO arguments. In the first case, the\r\ncommand itself does not begin with a parenthesis, and so the end of line marks the end of the command. In this\r\ncase, the entire (command) text is passed to the ECHO command.\r\nParentheses can also be used to enter multi-line commands. If a command line ends with one or more sets of\r\nunbalanced parentheses, the command line is assumed to continue on the next line. If the command was entered\r\ninteractively, the shell prompts for more input until all parentheses balance. If the command is part of a script, the\r\nshell reads additional script lines until all the parentheses balance. For example:\r\n1. C:\\\u003e(\r\n2. More?echo command1\r\n3. More?echo command2\r\n4. More?)\r\n5. command1\r\n6. command2\r\nThe first line consists only of an open parenthesis. The shell detects this, and prompts for more input. Next, two\r\nECHO commands are entered. Finally, a closing parenthesis balances the opening parenthesis and the command is\r\ncomplete. The shell then executes the compound command, which executes the two individual ECHO commands.\r\nIndividual commands do not span lines in multi-line commands. The end of a physical line always terminates a\r\nsimple command (either as typed or as entered in a script file). Notice in the preceding example how the end of the\r\nphysical line terminated each ECHO command.\r\nCompound commands and multi-line commands are particularly useful with IF and FOR statements. These\r\ncommands are described in Chapter 4. For example:\r\n1. if exist *.bak (\r\n2. echo Deleteing *.BAK files...\r\n3. del *.bak\r\n4. )\r\nThe IF command executes the following command if one or more .BAK files exist in the current directory.\r\nParentheses are used to execute a compound multi-line command. In this example, the ECHO command displays\r\na warning, and then the DEL command deletes the files. The indentation here is not mandatory, but does help to\r\nindicate the flow of control.\r\nTip Using a parenthesis, even when not strictly needed, is a useful way to increase script readability, and makes\r\nexplicit the command execution precedence rules.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 38 of 43\n\nThe previous sections showed how individual commands can be combined using command redirection and\r\ncompound command symbols. Although command redirection can be used with any command, it is most effective\r\nwith commands that are specifically designed as command filters. Generally, a command filter reads command\r\ninput, permutes, or processes the input in some manner, and then writes the permuted input to its command output.\r\nCommand filters are typically connected to other commands and each other via the pipe (|) redirection symbol.\r\nCommand filters are frequently used in scripts to extract specific information needed by a script. Typically, a\r\nFIND command can filter the output of a command, extracting only the line (or lines) which contain the required\r\ninformation. The script can then further process this filtered data. Many of the sample scripts in Part II use this\r\ntechnique.\r\nWindows NT provides four command filters:\r\nThe MORE command, which is used to paginate command output.\r\nThe SORT command, which can sort command output alpha-numerically.\r\nThe FIND command, which filters lines that contain a specified text string.\r\nThe CLIP [RK] command, which captures command output to the Windows NT clipboard.\r\nThe MORE command breaks its command input into pages. A page is the same number of lines as the console\r\nwindow. For example:\r\nC:\\\u003edir | more\r\nThis command displays the current directory, one page at a time. Press the spacebar to advance to the next page.\r\nThe MORE command supports several switches to control the output format. These are detailed in the MORE\r\nsection of the \"Command Reference.\"\r\nThe SORT command sorts its command input alpha-numerically, using the ASCII collating sequence. For\r\nexample:\r\nC:\\\u003esort \u003cdata.txt\r\nThis command sorts the contents of file DATA.TXT and displays the result. By default, the SORT command sorts\r\nin ascending order. The /R switch sorts in reverse (descending) order. Lines are sorted based on the data at the start\r\nof each line. Use the +n switch to sort based on data starting at column n on each line. For example:\r\nC:\\\u003edir | sort +14\r\nThis command sorts the output of the DIR command based on data starting at column 14 on each line.\r\nThe FIND command filters command input, passing to its command output only those lines which contain a\r\nspecified string. For example:\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 39 of 43\n\nC:\\\u003edir | find \"\u003cDIR\u003e\"\r\nThis command filters only directory lines from the output of the DIR command. The FIND command supports\r\nseveral switches to control the filtering process—these are detailed in the FIND topic of the \"Command\r\nReference.\" One useful switch is /C, which outputs only a count of the lines which match the string, instead of the\r\nlines themselves. For example:\r\nC:\\\u003edir | find \"\u003cDIR\u003e\" /c\r\nThis command counts the number of directories in the current directory.\r\nTip Windows NT also supports the more powerful FINDSTR command, which provides more sophisticated\r\nfiltering capabilities. However, FINDSTR works with files as input. FINDSTR is described in the \"Command\r\nReference.\"\r\nThe CLIP [RK] command places its command input into the clipboard. For example:\r\nC:\\\u003edir | clip\r\nThis command dumps the output of the DIR command into the clipboard. Once in the clipboard, the text can be\r\npasted into any Windows application.\r\nWindows NT provides a special feature called the Schedule Service that can execute any command or application\r\nperiodically or at a specified time and date. Scheduled commands can execute even if there is no user logged on to\r\nthe computer. The Schedule Service is therefore ideal for running periodic maintenance tasks, such as file\r\nbackups. For example, the REPL.BAT sample script in Part II uses the Schedule Service to provide periodic file\r\nreplication services.\r\nThe Schedule Service is controlled through several shell commands. These commands can control the service\r\neither on the local computer or any computer on the network (assuming that the user has sufficient rights to access\r\nthat computer). The ability to control the Schedule Service remotely is particularly useful for system\r\nadministrators managing many servers.\r\nThe two most common mistakes made when using Windows NT involve the setup and use of the Schedule\r\nService:\r\nFirst, the Schedule Service, like all services, logs on by default as the LocalSystem account. Typically, this\r\naccount has insufficient rights to perform many operations (such as accessing network drives).\r\nSecond, because the Schedule Service operates independently of a user logon, drive mappings and other\r\nper-user settings are not necessarily available when a scheduled command executes. For example, the\r\nenvironment available to a command executed by the schedule service is restricted to the system\r\nenvironment set via Control Panel.\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 40 of 43\n\nFortunately, both these problems are easily solved. Creating a special account exclusively for the Schedule Service\r\n(named, for example, ScheduleService) solves the first problem. This account is then assigned whatever access\r\nrights and group memberships are needed to ensure that any scheduled commands are successful. Explicitly\r\nmapping all required resources within a scheduled script (such as network drives) solves the second problem.\r\nAlternatively, use UNC names directly in commands.\r\nFor example, assume a logon script maps drive X: to \\\\SERVER\\COMMON. This command, which operates\r\ncorrectly when used interactively, fails when used with the Schedule Service:\r\ncopy x:\\myfiles\\*.bak c:\\backups\r\nThe Schedule Service does not execute the logon script, so drive X: is not available. The first solution to this\r\nproblem uses the NET command to explicitly map drive X: in the script. For example:\r\n1. net use x: \\\\server\\common\r\n2. copy x:\\myfiles\\*.bak c:\\backups\r\n3. net use x: /delete\r\nThe second solution uses UNC names directly. For example:\r\ncopy \\\\server\\common\\myfiles\\*.bak c:\\backups\r\nVirtually all built-in commands, and most external commands, accept UNC names as well as path names as\r\narguments.\r\nThe final stage to prepare the Schedule Service for use is starting the service. Typically, the start-up options for\r\nthis service are changed to require the service to automatically start whenever Windows NT boots. Both of these\r\noperations can be accomplished via the Control Panel Services applet. Figure 2.6 shows the Services applet\r\nrunning. The Schedule Service is named Schedule. Click the Start button to start the service, or the Stop button to\r\nstop the service.\r\nClick the Startup button to set service startup options. Figure 2.7 shows the startup options dialog box. Typically,\r\nthe Startup Type should be Automatic, and the Log On As option should be the account setup specifically for the\r\nschedule service.\r\nCc723564.shell06(en-us,TechNet.10).gif\r\nFigure 2.6: The Control Panel Services applet\r\nOnce the Schedule Service is running, commands can be scheduled using either the AT command or the SOON\r\n[RK] command. Refer to the \"Command Reference\" for a detailed description of these commands. The Windows\r\nNT Resource Kit also provides a GUI interface to the Schedule Service, called WINAT.\r\nFigure 2.7: Schedule Service startup options\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 41 of 43\n\nFigure 2.7: Schedule Service startup options\r\nBy default, all AT commands manage the schedule service on the local computer. Specify the computer name as\r\nthe first argument to manage the service on another computer. For example, this command lists all scheduled\r\ncommands on the current computer:\r\n1. C:\\\u003eat\r\n2. Status ID Day Time Command Line\r\n3. ---------------------------------------------------------------------------\r\n4. 0 Each F 8:00 PM \"e:\\Tools\\NAVNT\\NAVWNT\" /L\r\nThis command lists all scheduled commands on the computer CRAFT:\r\nC:\\\u003eat \\\\craft\r\nEnter an AT command and specify the execution time and the command to execute to schedule a command for\r\nexecution. For example:\r\nC:\\\u003eat 23:00:00 /every:monday cmd /c sysbkup.bat\r\nThis command schedules the command CMD /C SYSBKUP.BAT to execute at 11 p.m. The command is executed\r\nevery Monday whenever the system clock indicates the time is 11 p.m.\r\nThe SOON [RK] command schedules a command to execute a certain number of seconds in the future. For\r\nexample:\r\nC:\\\u003esoon 600 \"cmd /c sysbkup.bat\"\r\nThis command schedules the command CMD /C SYSBKUP.BAT to execute in 10 minutes (600 seconds) time.\r\nSOON can be used to setup a script for periodic execution. Simply place a SOON command as the first line of the\r\nscript. For example, place this line as the first line in MYSCRIPT.BAT:\r\nsoon 600 \"cmd /c myscript.bat\"\r\nThen manually schedule MYSCRIPT.BAT to execute once. The first time MYSCRIPT.BAT executes, it\r\nreschedules a new copy of itself to run in ten minutes. Thus, every 10 minutes, a new copy of MYSCRIPT.BAT is\r\nexecuted.\r\nTim Hill is an independent software developer specializing in systems software and operating system\r\narchitectures.\r\nCopyright © 1998 by MacMillan Technical Publishing\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 42 of 43\n\nWe at Microsoft Corporation hope that the information in this work is valuable to you. Your use of the information\r\ncontained in this work, however, is at your sole risk. All information in this work is provided \"as -is\", without any\r\nwarranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored,\r\nrecommended, supported or guaranteed by Microsoft Corporation. Microsoft Corporation shall not be liable for\r\nany damages you may sustain by using this information, whether direct, indirect, special, incidental or\r\nconsequential, even if it has been advised of the possibility of such damages. All prices for products mentioned in\r\nthis document are subject to change without notice.\r\nInternational rights = English only.\r\nLink\r\nClick to order\r\nSource: http://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nhttp://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120\r\nPage 43 of 43",
	"extraction_quality": 1,
	"language": "EN",
	"sources": [
		"MITRE"
	],
	"references": [
		"http://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120"
	],
	"report_names": [
		"cc723564.aspx#XSLTsection127121120120"
	],
	"threat_actors": [],
	"ts_created_at": 1775438999,
	"ts_updated_at": 1775791233,
	"ts_creation_date": 0,
	"ts_modification_date": 0,
	"files": {
		"pdf": "https://archive.orkl.eu/7d1279bf8dd28b5a734e3f9febd1a781b04b3969.pdf",
		"text": "https://archive.orkl.eu/7d1279bf8dd28b5a734e3f9febd1a781b04b3969.txt",
		"img": "https://archive.orkl.eu/7d1279bf8dd28b5a734e3f9febd1a781b04b3969.jpg"
	}
}