Section 5.2. Command Prompt Commands


5.2. Command Prompt Commands


Change or view the attributes of one or more files or folders.


To open

Command Prompt attrib


Usage

 attrib [+r|-r] [+a|-a] [+s|-s] [+h|-h] [filename] [/s [/d]] 


Description

Attrib allows you to change the file and folder attributes from the command linesettings otherwise available only in the Properties window of a file or folder. You can think of the attributes as switches, independently turned on or off for any file or group of files. The individual attributes are as follows:


R (read-only)

Turn on the read-only attribute of a file or folder to protect it from accidental deletion or modification. If you attempt to delete a read-only file, Windows will prompt you before allowing you to delete it. Different applications handle read-only files in different ways; usually you will not be allowed to save your changes to the same filename.


A (archive)

The archive attribute has no effect on how a file is used, but it is automatically turned on when a file is modified or created. It is used primarily by backup software to determine which files have changed since a backup was last performed; most backup programs turn off the archive attribute on each file that is backed up.


S (system)

Files with the system attribute are typically used to boot the computer. There's little reason to modify a file with the system attribute, or to ever turn on or off the system attribute for any file. If you turn off the system attribute of an important file, it may stop the file from working.


H (hidden)

To hide any file or folder from plain view in Explorer or on the Desktop, turn on its hidden attribute.


Examples

To hide a file in Explorer, right-click on it, select Properties, and turn on the hidden option. To hide the same file using the command line, type:

 attrib +h filename 

where filename is the full path- and filename of the file to change. To specify multiple files, include a wildcard, such as *.* (for all files) or *.txt (for all files with the .txt filename extension). Note the use of the plus sign (+) to turn on an attribute; use the minus sign (-) to turn it off. For example, to turn off the hidden attribute and simultaneously turn on the archive attribute, type:

 attrib -h +a filename 

To display the attributes of a file or a group of files in Explorer, select Details from the View menu. Then, select Choose Details from the View menu and turn on the Attributes option. To display the attributes of a file or a group of files on the command line, type:

 attrib filename 

where filename is the full path- and filename(s) of the files you want to view. Omit filename to display the attributes of all the files in the current folder. If filename is not used, or if it contains wildcards (in other words, if the command is intended to act on more than one file), you can use the /s option to further include the contents of all subfolders of the current folder. The /d option instructs Attrib to act upon folders as well as files, but it has meaning only if you use it in conjunction with the /s parameter.


Display the name of, or change, the current working directory (folder).


Usage

 cd [/d] [directory] chdir [/d] [directory] 


Description

With no arguments, cd displays the full pathname of the current directory. Given the pathname of an existing directory, it changes the current directory to the specified directory.

If directory is on a different drive (for example, if the current directory is c:\dream and you type cd d:\nightmare), the current working directory on that drive is changed, but the current working drive is not. To change the current drive, use the /d parameter, or simply type the letter followed by a colon, by itself, at the prompt (see the following examples).

Pathnames can be absolute (including the full path starting with the root) or relative to the current directory. A path can be optionally prefixed with a drive letter. The special path .. refers to the parent of the current directory.


Examples

If the current drive is C:, make c:\temp\wild the current directory:

 C:\>cd \temp\wild C:\temp\wild> 

Note how the current working directory is displayed in the prompt. If the current directory is c:\temp, all that is necessary is:

 C:\temp>cd wild C:\temp\wild> 

Change to the parent directory:

 C:\more\docs\misc>cd .. C:\more\docs> 

Change to the root directory of the current drive:

 C:\Windows\Desktop\>cd \ C:\> 

Change to another drive:

 C:\>cd /d d:\ D:\> 

or simply:

 C:\>d: D:\> 


Clear the Command Prompt window and buffer, leaving only the Command Prompt and cursor.


Description

Type cls at the prompt to clear the screen and the screen buffer, which is useful for privacy concerns or simply reducing clutter.

The difference between using cls and simply closing the current Command Prompt window and opening a new one is that your working environment (such as the current directory) is preserved with cls.


Copy one or more files to another location.


Usage

 copy source destination copy [/a | /b] source [/a | /b] [+ source [/a | /b] [+ ...]] [destination [/a | /b]] [/v] [/y | /-y] [/d] [/z] [/n] [/l] 


Description

copy makes a complete copy of an existing file. If another file by the same name exists at destination, you will be asked whether you want to overwrite it.

Omit the destination to copy the specified files to the current working directory. If the file (or files) to be copied is in a different directory or on a different disk, you can omit the destination filename. The resulting copy or copies will have the same name as the original.

You can use the special device name con (or con:) in place of either the source (or destination) filename to copy from the keyboard to a file (or from a file to the screen).

copy accepts the following parameters and options:


/a

Specifies that the file to copy is in ASCII format.


/b

Specifies that the file to copy is a binary file.


/v

Verifies that new files are written successfully by comparing them with the originals.


/y

Suppresses prompting to confirm that you want to overwrite an existing destination file.


/-y

Enables prompting to confirm that you want to overwrite an existing destination file with the same name (default).


/d

Allows the new file to be created as a decrypted file (NTFS volumes only).


/l

When copying a symbolic link (see "mklink," later in this chapter), copies the file as a link rather than making a couple of the source file (the default behavior).


/n

Copies the file using the short filename that Vista generated for compatibility with DOS, old versions of Windows, and devices that can't support long filenames on their flash memory.


/z

Copies networked files in restartable mode. If the network connection is lost during copying (if the server goes offline and severs the connection, for example), /z will resume the copying after the connection is reestablished.


Examples

Copy the file temp.txt from C:\ to d:\files (all three examples do the same thing):

 C:\>copy c:\temp.txt d:\files\temp.txt C:\>copy c:\temp.txt d:\files C:\>copy temp.txt d:\files 

The third sample in the preceding code works here because the source file is located in the current directory. Here's another way to do it:

 C:\>d: D:\>cd \files D:\files>copy c:\temp.txt 

Copy all the files from the directory d:\Cdsample\Images to the current directory, giving the copies the same names as the originals:

 C:\>copy d:\cdsample\images\*.* C:\>copy d:\cdsample\images\*.*. 

Copy the file words.txt in the current directory to d:\files, renaming it morewords.txt:

 C:\>copy words.txt d:\files\morewords.txt 

Copy all of the files in the current directory to d:\files (all three examples do the same thing):

 C:\>copy *.* d:\files C:\>copy .\*.* d:\files C:\>copy . d:\files 


Display or set the system date.


Usage

 date [/t | date] 


Description

date is essentially a holdover from the very early days of DOS when the user was required to enter the system date and time every time the computer was started. Now it's essentially included as a way to set the date from the command line; the preferred method is to use Control Panel [Clock, Language and Region] Date and Time.

If you type date on the command line without an option, the current date setting is displayed and you are prompted for a new one. Press Enter to keep the same date.

date accepts the following options:


date

Specifies the date. Use the mm-dd-[ yy ] yy format. Values for yy can be from 80 through 99; values for yyyy can be from 1980 through 8907. Separate month, day, and year with periods, hyphens, or slashes.


/t

Displays the current date without prompting for a new one. You can use this if you need to append a timestamp to the end of a file, as in date /t >> logfile.txt.


Delete one or more files.


Usage

 del [/p] [/f] [/s] [/q] [/a:attributes] filename erase [/p] [/f] [/s] [/q] [/a:attributes] filename 


Description

You use the del command to delete one or more files from the command line without sending them to the Recycle Bin.

The del options are:


filename

Specifies the file(s) to delete. If you do not specify the drive or path, the file is assumed to be in the current directory. You can use standard * and ? wildcards to specify the files to delete.


/p

Prompts for confirmation before deleting each file.


/f

Forces deletion of read-only files.


/s

Deletes specified files in all subdirectories (when using wildcards).


/q

Quiet mode; do not prompt if filename is *.*.


/a: attributes

Selects files to delete based on attributes (read-only, hidden, system, or archive). See "Attrib," earlier in this chapter, for more information on attributes.


Examples

Delete the file myfile.txt in the current directory:

 C:\>del myfile.txt 

Delete the file myfile.txt in the c:\files directory:

 C:\>del c:\files\myfile.txt 

Delete all files with the pattern myfile.* (e.g., myfile.doc, myfile.txt, etc.) in the current directory, but prompt for each deletion:

 C:\>del c:\files\myfile.* /p 


Display a list of files and subdirectories in a directory (folder).


Usage

 dir [filename] [/b] [/c] [/d] [/l] [/n] [/p] [/q] [/s]   [/w] [/x] [/4] [/a:attributes] [/o:sortorder]   [/t:timefield] 


Description

Without any options, dir displays the disk's volume label and serial number, a list of all files and subdirectories (except hidden and system files) in the current directory, file/directory size, date/time of last modification, the long filename, the total number of files listed, their cumulative size, and the free space (in bytes) remaining on the disk.

If you specify one or more file or directory names (optionally including drive and path, or the full UNC path to a shared directory), information for only those files or directories will be listed.

You can use wildcards (* and ?) to display a subset of files and subdirectories in a given location.

dir accepts the following options:


/a: attributes

Displays only files with/without specified attributes (using - as a prefix specifies "not," and a colon between the option and attribute is optional). See "Attrib," earlier in this chapter, for more information on attributes.


/b

Use bare format (no heading information or summary). Use with /s to list all files in the current directory and subdirectories.


/c

Displays the thousand separator in file sizes. This is the default; use /-c to disable display of the separator.


/d

Same as /w, except files are sorted vertically.


/l

Use lowercase.


/n

Lists files in a Unix-like display, where filenames are shown on the right. This is the default view.


/o: sortorder

Lists files in sorted order (using - as a prefix reverses the order, and a colon between the option and attribute is optional):


d

By date and time (earliest first)


e

By extension (sorted alphabetically)


g

Group directories first


n

By name (sorted alphabetically)


s

By size (smallest first)


/p

Pauses after each screenful of information; press any key to continue.


/q

Displays the owner of each file.


/s

Includes all files in all subdirectories, in addition to those in the current directory.


/t: timefield

Controls which time is used when sorting:


c

Created


a

Last accessed


w

Last modified (written)


/w

Wide list format. File- and directory names are listed in columns and sorted horizontally. The actual number of columns varies based on the length of the longest filename and the screen width. Use /d instead to sort vertically.


/x

Include the "short" 8.3 versions of long filenames. For example, Sam's File.txt has an alternate filename, samsfi~1.txt, to maintain compatibility with older applications.


/4

Display the listed years as four digits. By default, two-digit years are displayed.


Examples

Display all files in the current directory:

 C:\>dir 

Display all files in the current directory that end with the .txt extension:

 C:\>dir *.txt 

Display all files, listing years in four digits and pausing for each screenful:

 C:\>dir /4 /p 

Display all files, sorted by date and time, latest first:

 C:\>dir /o-d 

Display only directories:

 C:\>dir /ad 

List all files on disk, sorted by size, and store output in the file allfiles.txt:

 C:\>dir \ /s /os > allfiles.txt 

List the contents of the shared folder cdrom on machine bubba:

 C:\>dir \\bubba\cdrom 


Display a string of text; turn command echoing on or off.


Usage

 echo [on | off | message] 


Description

echo is typically used with other commands or in a batch file to display text on the screen. It's also used to control command echoing from batch files.

You can use the following options with echo:


on | off

By default, each command in a batch file is echoed to the screen as it is executed; echo on and echo off toggle this feature. To turn echoing off without displaying the echo off command, use @echo off. The @ symbol in front of any command in a batch file prevents the line from being displayed.


message

Types the message you'd like displayed to the console (screen).


Examples

To display an ordinary message, use the following:

 echo Hello World! 

To display a blank line, use one of the following (both are equivalent):

 echo. echo, 

(Note the absence of the space between the echo command and the punctuation; you can also use a colon, semicolon, square brackets, backslash, or forward slash.)

One handy use of echo is to answer y to a confirmation prompt such as the one del issues when asked to delete all the files in a directory. For example, if you wanted to clear out the contents of the \temp directory from a batch file, you could use the following command:

 echo y | del c:\temp\*.* 

or even:

 echo y | if exists c:\temp\*.* del c:\temp\*.* 

This construct works because the pipe character takes the output of the first command and inserts it as the input to the second.

You can use echo to announce the success or failure of a condition tested in a batch file:

 if exist *.rpt echo The report has arrived. 

It's a good idea to give users usage or error information in the event that they don't supply proper arguments to a batch file. You can do that as follows:

 @echo off if (%1) == (  ) goto usage . . . goto end :usage echo You must supply a filename. :end 


Search in one or more files for text.


Usage

 find [/v] [/c] [/n] [/i] [/offline] "string" [filename[ ...]] 


Description

After searching the specified files, find displays any lines of text that contain the string you've specified for your search. find is useful for searching for specific words (strings) in files.

The find options are:


"string"

The text to look for, enclosed in quotation marks.


filename

The file(s) in which to search. Wildcards (*, ?) are supported, and you can specify multiple filenames as long as you separate them with commas. If filename is omitted, find searches text typed at the prompt or piped from another command via the pipe character (|), as described in "Wildcards, Pipes, and Redirection," earlier in this chapter.


/c

Displays only the count of lines containing the string.


/i

Ignores the case of characters when searching for the string.


/n

Displays line numbers with the displayed lines.


/v

Displays all lines not containing the specified string.


/offline

Includes files with the offline attribute set (that otherwise would be skipped).


Examples

Search for redflag in myemployees.txt:

 C:\>find "redflag" myemployees.txt 

Count occurrences of the word deceased in myemployees.txt:

 C:\>find /c "deceased" myemployees.txt 

Search the current directory for the string "cls" in all .bat files and store the result in the file cls.txt (note that >> rather than > is necessary when redirecting the output of a for loop):

 C:\>for  %f in (*.bat) do find "cls" %f >> cls.txt 


Create a new directory (folder).


Usage

 md [drive:]path mkdir [drive:]path 


Description

Windows Vista, like its predecessors, uses a hierarchical directory structure to organize its filesystem. On any physical disk, the filesystem begins with the root directory, signified by a lone backslash.

md and mkdir accept the following option:


[drive:]path

Specifies the directory to create.


Examples

Create a subdirectory named harry in the current directory:

 C:\tom\dick>md harry 

Create a new directory called newdir under the c:\olddir directory:

 C:\>md c:\olddir\newdir 

If c:\olddir doesn't exist, it will be created as well.

Create two new directories, c:\rolling and c:\stones:

 C:\>md rolling stones 

Create a single new directory, c:\rolling stones:

 C:\>md "rolling stones" 

(Enclose directory names in quotation marks to accommodate spaces.)


Create a link to another file or directory.


Usage

 mklink [[/d] | [/h] | [/j]] link target 


Description

mklink is used to create a Unix-style link to another file or directory; that link can be used just as if it were the original file or directory. When you create a symbolic link to a file, that file will show up in Windows Explorer as a shortcut, and if you use the command line to get a directory listing, it will be listed as a <SYMLINK> instead of as <DIR>, and you'll see the symbolic link followed by the real file. For example, if you create a symbolic link called newone to the file test, here's what you would see:

 <SYMLINK>      newone [c:\test] 

The mklink options are:


/d

Creates a symbolic link (the default). Use /j for directories.


/h

Creates a hard link instead of a symbolic link. You will not see SYMLINK in the dir listing, because a hard link is a first-class filename. Creating a hard link to an existing file adds another filename, and the file is not deleted until you delete the last hard link to it.


/j

Creates a directory junction. You will see JUNCTION in the dir listing instead of SYMLINK.


link

Specifies the new symbolic link name.


target

Specifies the path (relative or absolute) that the new link refers to.


Specifies the path (relative or absolute) that the new link refers to.

Move files and directories from one location to another.


Usage

 move [/y | /-y] filename[,...] destination 


Description

move works like copy, except that the source is deleted after the copy is complete. filename can be a single file, a group of files (separated by commas), or a single file specification with wildcards.

The move options are:


filename

Specify the location and name(s) of the file or files you want to move. Wildcards (*, ?) are supported.


destination

Specify the new location of the file. The destination parameter can consist of a drive, a directory name, or a combination of the two. When moving one file, destination may include a new name for the file. If you include the new filename but omit the drive or directory name, move effectively renames the file.


/y

Suppress prompting to confirm creation of a directory or overwriting of the destination. This is the default when move is used in a batch file.


/-y

Cause prompting to confirm creation of a directory or overwriting of the destination. This is the default when move is used from the command line.


Examples

Move myfile.txt from the current directory to d:\files:

 C:\>move myfile.txt d:\files\ 

Same, but rename the file to newfile.txt:

 C:\>move myfile.txt d:\files\newfile.txt 

Change the name of the directory d:\files to d:\myfiles:

 D:\>move d:\files myfiles 


Set or display the command search path.


Usage

 path [path1][;path2][;path3][;...] 


Description

When you type an executable filename at the Command Prompt (as opposed to a command built into the Command Prompt), Windows starts by looking in the current directory for a file that matches. If no matching file is found, Windows then looks in a series of other folders, which are known collectively as the path or the command search path.

The path statement is used to define additional directories to be included while searching for files. The path consists of a series of absolute directory pathnames, separated by semicolons. No spaces should follow each semicolon, and there should be no semicolon at the end of the statement. If no drive letter is specified, all pathnames are assumed to be on the current directory's drive.

Type path without any arguments to display the current command search path. The default path in Windows Vista is c:\windows\system32;c:\windows;c:\windows\system32\wbem.

When you type the name of a command, DOS looks first in the current directory and then in each successive directory specified in the path. Within each directory, it will look for executable files by their extension in the following order: .com, .exe, .bat, .cmd. Windows searches your path for certain other file types (i.e., .dll or .ocx) as well, although most cannot be executed from the command line.


Examples

Specify the directories c:\Stuff and d:\Tools in the path:

 C:\>path c:\stuff;d:\tools 

However, this will replace the path with these two folders. To add these folders to the existing path, type the following:

 C:\>path %path%;c:\stuff;d:\tools 


Change the appearance of the prompt.


Usage

 prompt [text] 


Description

Type prompt by itself (without text) to reset the prompt to its default setting.

The prompt options are:


text

Specifies a new command prompt. text can contain normal characters and the following special codes:


$_

Carriage return and linefeed


$$

Dollar sign ($)


$a

Ampersand (&)


$b

Pipe (|)


$c

Left parenthesis (()


$d

Current date


$e

Escape character (ASCII code 27), used to provide extended formatting


$f

Right parenthesis ())


$g

Greater-than sign (>), commonly known as the caret


$h

Backspace (erases preceding character)


$l

Less-than sign (<)


$n

Current drive


$p

Current drive and path


$q

Equals sign (=)


$s

Space


$t

Current time


$v

Windows version number


Examples

Specify the current drive and directory followed by the greater-than sign (>), the default prompt in Windows Vista:

 C:\>prompt $p$g 

Specify the drive and directory on one line and the date, followed by the greater-than sign (>), on another:

 C:\>prompt $p$_$d$g 

Specify the drive only, followed by the greater-than sign (>), which was the default prompt on early versions of DOS:

 C:\>prompt $n$g 


Find out information about sessions, processes, and users.


Usage

 query process | session | termserver | user 


Description

The query options are:


process

Lists the currently running processes, including filenames, and associated usernames


session

Lists the current sessions, including their IDs and associated usernames


termserver

Lists the currently running terminal servers


user

Lists information about logged-in users, including the time they logged on, their amount of idle time, their session name, and their session ID


Displays information about users currently logged on to the system. (Not available in the Home edition.)


Usage

 quser username sessionname sessionid 


Description

quser displays information about currently logged-on users. It displays when the user logged on, whether the user is active, and the current time, the username, the session name, the session ID, and the amount of idle time for the user. (To find the session ID, use the query command. See the preceding section, "query.") You can use the following commands with quser:


username

Identifies the username of the user for whom you want to display information


sessionname

Displays the session name (for example, console) of the user for whom you want to display information


sessionid

Displays the session ID (for example, 2) of the user for whom you want to display information


Examples

Display information about the user preston:

 quser preston 

Display information about the user with the session name console:

 quser console 


Remove (delete) a directory.


Usage

 rd [/s] [/q] path rmdir [/s] [/q] path 


Description

Unlike in Windows Explorer, files and folders are deleted differently; if you try to use del to delete a directory, it will simply delete all the files in the directory, but the directory itself will remain. You use rd to delete empty directories and, optionally, to delete directories and all of their contents.

rd accepts the following options:


path

Specifies the directory to delete.


/s

Removes all files and subdirectories of the specified directory.


/q

Quiet mode; don't prompt when using /s.


Examples

Delete the empty subdirectory called newdir located in the c:\olddir directory:

 C:\>rd c:\olddir\newdir 

Delete the directory Online Services and all of its contents within the current directory, c:\Program Files:

 C:\Program Files>rd /s "online services" 

Note that you must use quotes with rd for folders with spaces in their names.


Rename a file or directory.


Usage

 ren [filename1] [filename2] rename [filename1] [filename2] 


Description

Use ren to rename any file or directory. Unlike in Windows Explorer, though, ren is capable of renaming several files at once (via the wildcards * and ?).

The ren options are:


[ filename1]

The name of the existing file or directory


[ filename2]

The new name to assign to the file or directory


Examples

Rename myfile.txt to file.txt:

 C:\>rename myfile.txt file.txt 

Rename chap 5.doc to sect 5.doc (the following two methods are identical):

 C:\>ren "chap 5.doc" "sect 5.doc" C:\>ren chap?5.doc sect?5.doc 

Each of these examples represents different ways to rename files with spaces in their names. In addition to the standard quotation marks, in certain circumstances you can use wildcards to avoid the space problem. Here, both chap 5.doc and sect 5.doc have spaces in the fifth character position, so you can use the single wildcard character (?).

Rename the files chap1.doc, chap2.doc, etc., to revchap1.doc, revchap2.doc, etc.:

 C:\>ren chap*.doc revchap*.doc 

ren can be a convenient way to rename the filename extensions of several files at once, as well:

 C:\>ren *.txt *.rtf C:\>ren *.htm *.html C:\>ren *.mpeg *.mpg 


A powerful, flexible tool for copying files.


Usage

 robocopy source [drive:\path or \\server\share\path] destination [drive:\path or \\server\share\path] [file [file]...] /a /m /s /e /mov /move /lev:n /fat 


Description

robocopy stands for Robust Copy for Windows, and the name is apt. It is far more powerful than the copy and xcopy commands, and in fact, a full explication of all of its commands and uses would take up half this chapter. For details about its full usage, type robocopy /? to get help.

You can use the command to copy files not only on a PC, but also between PCs and servers, and between servers as well. It can use wildcards for file selection.

The following are some of the primary commands that you can use with robocopy:


/a

Copy only files with the Archive attribute set.


/m

Copy only files with the Archive attribute and reset it.


/s

Copy subdirectories, but not empty ones.


/e

Copy subdirectories, including empty ones.


/mov

Move files (delete from source after copying).


/move

Move files and directories (delete from source after copying).


/lev: n

Copy on the top n levels of the source directory tree.


/fat

Create destination files using 8.3 FAT filenames only.


/mir

Mirrors a complete directory tree. You also can use it with other commands, such as /b for copying files in backup mode, to copy only files that have been changed.


Examples

Copy all .xls files from C:\newbudget to F:\newbudget, and use only 8.3 FAT filenames:

 robocopy *.xls c:\newbudget f:\newbudget /fat 


Display, assign, or remove environment variables.


Usage

 set [variable[=[string]]] set /p variable=[promptstring] set /a expression 


Description

The environment is a small portion of memory devoted to the storage of a few values called environment variables. You use set to manipulate environment variables from the command line, but because the Command Prompt's environment is reset when its window is closed, the usefulness of set is fairly limited for interactive use, although you'll use it a lot in your own batch files.

To make more permanent changes to environment variables, go to Control Panel [System and Maintenance] System Advanced system settings Environment variables. The variables in the upper listing are for the current user and the variables in the lower listing apply to all users. Some environment variables, such as the Temp user variable, are assigned with respect to other variables, like this:

 %USERPROFILE%\AppData\Local\Temp 

where %USERPROFILE% (note the percent signs [%] on both sides) signifies the USERPROFILE variable, which represents the path of the current user's home directory. See "path," earlier in this chapter, for another example of this usage.

In addition to providing a simple means of interapplication communication, environment variables are also useful for storing data used repeatedly in a batch file.

Type set without options to display all of the current environment variables. Type set with only a variable name (no equals sign or value) to display a list of all the variables whose prefix matches the name.

The set options are:


variable

Specifies the variable name. When assigning a new variable, the case used is preserved. But when referencing, modifying, or deleting the variable, variable is case-insensitive. If variable is specified by itself, its value is displayed. If variable is specified by itself with an equals sign, the variable is assigned an empty value and deleted. Variable cannot contain the equals sign (=).


string

Specifies a series of characters to assign to variable. As stated earlier, this can contain references to other variables when surrounded with preceding and trailing percent signs (%).


/p

Specifies that variable will be assigned by text input from the user, rather than string. As stated earlier, this can contain references to other variables with preceding and trailing percent signs (%).


promptstring

The text prompt to display when using the /p option.


/a

Specifies that expression is a numerical expression to be evaluated. If used from the Command Prompt, set /a will display the final evaluated result of expression, even if you include an assignment operator (such as =) to assign a variable.


expression

When used with the /a option, expression is a collection of symbols, numbers, and variables arranged so that it can be evaluated by set. The following symbols are recognized (in decreasing order of precedence):


( )

Parentheses for grouping


! ~ -

Unary operators (not, bitwise not, negative)


*/ %

Arithmetic operators (multiply, divide, modulus)


+-

Arithmetic operators (add, subtract)


<< >>

Logical shift


&

Bitwise "and"


^

Bitwise "exclusive or"


|

Bitwise "or"


= *= /= %= += -= &= ^= |= <<= >>=

Assignment


'

Expression separator

If you use /a with any of the bitwise or modulus operators, you need to enclose expression in quotes. Any non-numeric strings in expression are treated as environment variable names, and their values are converted to numbers during evaluation (zero is used for undefined variables); the percent signs (%) are not used here.


Examples

Set the variable dummy to the string not much:

 C:\>set dummy=not much 

Set the dircmd variable, which instructs the dir command (discussed earlier in this chapter) to sort directory listings by size, with the largest first:

 C:\>set dircmd=/s /o-s 

Append the directory c:\mystuff to the path (see "path," earlier in this chapter); note how the path variable is used on the right side of the equals sign so that its original contents aren't lost:

 C:\>set path=%path%;c:\mystuff 

Set the prompt (see "prompt," earlier in this chapter) to show the current time, followed by a right-angle bracket:

 C:\>set prompt=$t> 

Display the contents of the variable named dummy (both of the following statements are equivalent):

 C:\>set dummy C:\>echo %dummy% 

You can also reference environment variables with other commands:

 C:\>set workdir=C:\stuff\tim's draft C:\>cd %workdir% 

Here, the environment variable is used to store a long pathname for quick navigation to a frequently used directory.

Display the values of all variables that begin with the letter H:

 C:\>set h 

Clear the value of an environment variable, dummy:

 C:\>set dummy= 

Prompt the user to enter text to be inserted into the dummy variable. This is typically used in batch files (note that you have to put quotes around it, otherwise the > will be interpreted as the redirection operator):

 C:\>set /p dummy="Enter text here >" 

Evaluate an arithmetic expression (the two following expressions are not the same):

 C:\>set /a 7+(3*4) C:\>set /a (7+3)*4 

The results of these two expressions, 19 and 40, respectively, will be displayed. To assign the result to a variable, type the following:

 C:\>set /a dummy=7+(3*4) 

Even though you're assigning the result variable, the result will still be displayed (unless set is executed from a batch file). To suppress the output, type this:

 C:\>set /a dummy=7+(3*4) > nul 

In addition to any custom environment variables you may use, Windows Vista recognizes the following variables (many of which are predefined):


ALLUSERSPROFILE

The location of the All Users folder, usually c:\Users\All Users.


APPDATA

The location of the Application Data folder, usually c:\Users\%USERNAME%\AppData\Roaming.


COMMONPROGRAMFILES

The location of the Common Files folder, usually c:\Program Files\Common Files.


COMPUTERNAME

The network name of the computer, which you can set by going to Control Panel [System and Maintenance] See the name of this computer Change settings.


COMSPEC

The location of the Command Prompt application executable, c:\Windows\system32\cmd.exe by default.


COPYCMD

Whether the copy, move, and xcopy commands should prompt for confirmation before overwriting a file. The default value is /-y. To stop the warning messages, set copycmd to /y.


DIRCMD

Specifies the default options for the dir command. For example, setting dircmd to /p will cause dir to always pause after displaying a screenful of output.


ERRORLEVEL

The return code of the last command run. A zero value (0) indicates success; anything else indicates failure.


HOMEDRIVE

The drive letter of the drive containing the current user's home directory, usually c:, used with HOMEPATH.


HOMEPATH

Along with HOMEDRIVE, the path of the current user's home directory, which is usually \Users\%USERNAME%.


LOGONSERVER

The name of the computer as seen by other computers on your network, usually the same as COMPUTERNAME preceded by two backslashes.


NUMBER_OF_PROCESSORS

The number of processors currently installed. In a multiprocessor or multicore system, it can be two, four, or more.


OS

Used to identify the operating system to some applications; for Windows XP, OS is set to Windows_NT. You may be able to "fool" an older program that is programmed not to run on an NT system by changing this variable temporarily.


PATH

The sequence of directories in which the command interpreter will look for commands to be interpreted.


PATHEXT

The filename extensions (file types) Windows will look for in the directories listed in the path. The default is .COM, .EXE, .BAT, .CMD, .VBS, .VBE, .JS, .JSE, .WSF, .WSH, and .MSC.


PROCESSOR_ARCHITECTURE

The type of processor; set to x86 for 32-bit Windows running on Intel-compatible processors (such as the Core 2 Duo or Athlon X2).


PROCESSOR_REVISION

A series of values the processor manufacturer uses to identify the processor.


PROGRAMFILES

The location of the Program Files folder, usually c:\Program Files.


PROMPT

The format of the command-line prompt, usually $P$G. See "prompt," earlier in this chapter, for details.


SESSIONNAME

The name of the current Command Prompt session, usually Console.


SYSTEMDRIVE

The drive letter of the drive containing Windows, usually C:.


SYSTEMROOT

The location of the Windows directory (or more specifically, the name of the folder in which the \Windows\System32 folder can be found), usually c:\windows.


TEMP, TMP

The location where many programs will store temporary files. TEMP and TMP are two different variables, but they should both have the same value, usually set to c:\Users\%USERNAME%\AppData\Local\Temp (short name used to maintain compatibility with older DOS programs).


USERDOMAIN

The name of the domain to which the computer belongs (set by going to Control Panel System Computer Name Change). If no domain is specified, USERDOMAIN is the same as COMPUTERNAME.


USERNAME

The name of the current user.


USERPROFILE

The location of the current user's home directory, which should be the same as HOMEDRIVE plus HOMEPATH, usually c:\Users\%USERNAME%.


WINDIR

The location of the Windows directory, usually c:\windows.


Sort text or the contents of text files in alphanumeric order.


Usage

 sort [/r] [/+n] [/m kilobytes] [/l locale]   [/rec recordbytes] [/t [tempdir]]   [/o outputfilename] [filename] 


Description

The sort command sorts text on a line-by-line basis. Each line of the input is ordered alphanumerically and output to the screen (or optionally, stored in a file). By default, sorting starts with the character in the first column of each line, but you can change this with the /+n option. sort is often used in conjunction with either pipes or output redirection (both discussed earlier in this chapter). That is, you might want to sort the output of another command, and you will often want to redirect the output to a file so that it can be saved. sort takes the following options:


/r

Reverses the sort order; that is, it sorts Z to A and then 9 to 0.


/+ n

Sorts the file according to characters in column n.


/m kilobytes

Specifies the amount of main memory to allocate for the sort operation, in kilobytes. The default is 90 percent of available memory if both the input and output are files, and 45 percent of memory otherwise. The minimum amount of memory sort will use is 160 KB; if the available (or specified) memory is insufficient, sort will split up the operation using temporary files.


/l locale

Overrides the system default locale (see Control Panel Regional and Language Options). The "C locale yields the fastest collating sequence, and in Windows Vista, it is the only choice.


/rec recordbytes

Specifies the maximum number of characters on a line (in a record); the default is 4,096 and the maximum is 65,535.


/t tempdir

Specifies the location of the directory used to store temporary files, in case the data does not fit in main memory (see the /m option). The default is to use the system temporary directory.


/o outputfilename

Specifies a file where the output is to be stored. If not specified, the sorted data is displayed at the prompt. Using the /o option is faster than redirecting output (with the > symbol).


filename

The name (and optionally, full path) of the file to sort.


Examples

Display an alphabetically sorted directory (similar to dir /o):

 C:\>dir | sort 

Sort the contents of a file, data.txt, and store the sorted version in results.txt (the following four examples are equivalent, although the first is the most efficient):

 C:\>sort /o results.txt data.txt C:\>sort data.txt > results.txt C:\>sort /o results.txt < data.txt C:\>type data.txt | sort > results.txt 


Display or set the system time.


Usage

 time [/t | time] 


Description

Like date (discussed earlier in this chapter), time is essentially a holdover from the very early days of DOS when the user was required to enter the system date and time every time the computer was started. Now it's essentially included as a way to set the data from the command line; the preferred method is to use Control Panel [Clock, Language, and Region] Date and Time.

If you type time on the command line without an option, the current time setting is displayed, and you are prompted for a new one. Press Enter to keep the same date.

The time options are:


time

Sets the system time without a prompt. The format of time is hh:mm:ss [A|P], where:


hh

Hours: valid values = 023.


mm

Minutes: valid values = 059.


ss

Seconds: valid values = 059.


A|P

A.M. or P.M. (for a 12-hour format). If a valid 12-hour format is entered without an A or P, A is the default.


/t

Displays the current time without prompting for a new one.


Display the contents of a text file.


Usage

 type filename 


Description

The type command is useful if you need to quickly view the contents of any text file (especially short files). type is also useful for concatenating text files, using the >> operator.


Displays Windows version information.


Usage

 ver 


Description

ver shows the version of Windows you're using. You can also find the Windows version at Control Panel [System and Maintenance] System tab, but it wont show you the revision number.

ver takes no options.


Displays the location of files that match a filename.


Usage

 where /r /q [dir] /f /t [filename] 


Description

where will show you the location of files for which you're searching. It searches along your default path, although you can add directories to the default with the use of /r. It accepts wildcards such as * and ?. You can use the following commands with where:


/r

Searches for and displays the file, starting with the specified directory


/q

Quiet mode (no output, but it sets the exit code in %ERRORLEVEL%)


/f

Displays the matched filename in double quotes


/t

Displays the file size and last modified date and time for all matched files


Examples

Display the location of all files named eula.txt along the default path:

 where eula.txt 

Display the location and file size, and last modified date and time, for all .doc files in the directory \latest:

 where /r \latest /t *.doc 


Gets username and group information, security identifiers (SIDs), privileges, and logon identifier (logon ID) for the current user.


Usage

 whoami /upn /fqdn /user /groups /priv /logonid /all /fo format /nh 


Description

You use whoami to get a variety of information about the currently logged-in user. You can use the following options with the whoami command:


/upn

Displays the username in User Principal Name (UPN) format. (Works only when the user is connected to a domain.)


/fqdn

Displays the username in Fully Qualified Distinguished Name (FQDN) format. (Works only when the user is connected to a domain.)


/user

Displays information about the current user along with the SID.


/groups

Displays group membership for the current user, the type of account, SIDs, and attributes.


/priv

Displays the security privileges of the current user.


/logonid

Displays the logon ID of the current user.


/all

Displays the current username and the groups the user belongs to, along with the SID and privileges for the current user.


/fo format

Specifies the output format to be displayed. Valid values are TABLE, LIST, and CSV. Column headings are not displayed with the CSV format. The default format is TABLE.


/nh

Specifies that the column header should not be displayed in the output. This is valid only for the TABLE and CSV formats.


Examples

Display login ID and security privileges of the current user:

 whoami /logonid 

Display the current username, the groups the user belongs to, and the SID and privileges for the current user, in CSV output format, with no header displayed:

 whoami /all /fo CSV /nh 


Copy files and directory trees (directories, subdirectories, and their contents).


Usage

 xcopy source [destination] [/a | /m] [/d[:date]] [/p]   [/s [/e]] [/v] [/w] [/c] [/i] [/q] [/f] [/l] [/g]   [/h] [/r] [/t] [/u] [/k] [/n] [/o] [/x] [/y] [/-y]   [/z] [/b] [/exclude:filenames] 


Description

xcopy works like copy but provides more options and is often faster.

The xcopy options are:


source

Specifies the file(s) to copy; source must include the full path.


destination

Specifies the location and/or name of new files. If omitted, files are copied to the current directory.


/a

Copies files with the archive attribute set but doesn't change the attribute of the source file (similar to /m).


/c

Continues copying even if errors occur.


/d :date

Copies only files changed on or after the specified date. If no date is given, copies only those source files that are newer than existing destination files.


/e

Copies all directories and subdirectories (everything), including empty ones (similar to /s). May be used to modify /t.


/exclude: filenames

Specifies a file (or a list of files) containing strings of text (each on its own line). When any of the strings match any part of the absolute path of the file to be copied, that file will be excluded from being copied. Contrary to what you might expect, filenames does not actually list the filenames to exclude.


/f

Displays full source and destination filenames while copying (unless /q is specified); normally, only filenames are displayed.


/g

Allows the copying of encrypted files to a destination that does not support encryption; otherwise, such files are skipped.


/h

Copies hidden and system files also; normally files with the hidden or system attributes are skipped (see "Attrib," earlier in this chapter, for details).


/i

If a destination is not supplied and you are copying more than one file, assumes that the destination must be a directory. (By default, xcopy asks if the destination is a file or directory.)


/k

Duplicates the attributes of the source files; by default, xcopy turns off the read-only attributes.


/l

Displays files that would be copied given other options, but does not actually copy the files.


/m

Copies files with the archive attribute set, then turns off the archive attribute of the source file (similar to /a).


/n

Copies files using short (8.3) file- and directory names (for example, PROGRA~1 instead of Program Files). Use this feature to convert an entire branch of files and folders to their short names.


/o

Copies file ownership and ACL information.


/p

Prompts you before creating each destination file.


/q

Quiet mode; does not display filenames while copying.


/r

Overwrites read-only files.


/s

Copies directories and subdirectories, except empty ones (similar to /e).


/t

Creates the directory structure but does not copy files; does not include empty directories unless /e is specified.


/u

Copies from the source-only files that already exist on destination; used to update files.


/v

Verifies copied files by comparing them to the originals.


/w

Prompts you to press a key before copying (useful in batch files).


/x

Copies file audit settings (implies /o).


/y, /-y

Turns off or on (respectively) the prompt for overwriting existing files.


/z

Copies networked files in restartable mode.


/b

When copying a symbolic link (see "mklink," earlier in this chapter), copies the file as a link rather than making a couple of the source file (the default behavior).

The following are exit codes generated by xcopy. They can be tested in a batch file with ERRORLEVEL to determine whether the xcopy operation was successful.


0

All files were copied without errors.


1

No files were found to copy.


2

xcopy was terminated by Ctrl-C before copying was complete.


4

An initialization error occurred. Such an error would generally be caused by insufficient memory or disk space, or an invalid drive name or syntax.


5

A disk-write error occurred.


Examples

Copy all the files and subdirectories, including any empty subdirectories and hidden files, from c:\foobar to the root directory of d:

 C:\>xcopy \foobar d: /s /e /h 



Windows Vista Pocket Reference
Windows Vista Pocket Reference: A Compact Guide to Windows Vista (Pocket Guides)
ISBN: 0596528086
EAN: 2147483647
Year: N/A
Pages: 63

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net