Windows Batch Scripting/File and Directory Management

=2. File and Directory Management=

This chapter covers the following commands:


 * ATTRIB -      Displays or changes file attributes.
 * CD, CHDIR -   Displays the name of or changes the current directory.
 * COPY -        Copies one or more files to another location.
 * DEL -        Deletes one or more files.
 * DIR -         Displays a list of files and subdirectories in a directory.
 * ERASE -       Deletes one or more files.
 * MD, MKDIR -   Creates a directory.
 * MOVE -        Moves one or more files from one directory to another directory.
 * RMDIR -       Removes a directory.
 * REN, RENAME - Renames a file or files.
 * POPD -        Restores the previous value of the current directory saved by PUSHD.
 * PUSHD -       Saves the current directory then changes it.
 * RD -          Removes a directory.
 * REPLACE -     Replaces files.
 * TREE -        Graphically displays the directory structure of a drive or path.
 * XCOPY -       Copies files and directory trees.

The DIR, CD and MD Commands

 * Open a cmd.exe window (see chapter 1.)

There are two names for the current directory command, CD and CHDIR. Both commands work in exactly the same way and from now on we will use the shorter name CD. The CD command displays the name of the current directory.


 * Type CD :

The DIR command with no arguments list the contents of a directory.


 * Type DIR :

There are two names for the make directory command, MD and MKDIR. Both commands work in exactly the same way and again we will use the shorter form in the examples.

We start by creating a new directory:

We will now use the CD command with an argument and make "C:\cmd_guide" the current directory.

Note that the current directory is shown in the command prompt.

You've now seen the basics of the CD, DIR and MD commands. We will now go on to look at these in more depth.

The CD command without and argument now show the C:\cmd_guide directory as the current directory.

DIR Usage and Options
In the example above the MD command was used to create a single directory. We will look at this directory using the DIR command.

The standard output from the DIR command shows that the directory C:\cmd_guide has empty but it contains two directory references "." and "..". A single dot "." represents the current directory and ".." is the parent directory. The DIR command is shorthand for DIR.

Compare the outputs in the listings for DIR and DIR.

The command DIR .. will list the contents of the parent directory.

Make a directory, look at it use DIR /B and DIR /S /B

MD Usage
Creating multiple directories.

Creating nested directories.

Filtering/Searching with the DIR Command
Match an exact number of letters.

DIR Text From Book Page - Needs Reviewing
This command displays the contents of directories. To get help on the  command type  : C:\>dir /? Displays a list of files and subdirectories in a directory.

DIR [drive:][path][filename] [/A]attributes [/B] [/C] [/D] [/L] [/N] [/O]sortorder [/P] [/Q] [/S] [/T]timefield [/W] [/X] [/4]

[drive:][path][filename] Specifies drive, directory, and/or files to list.

/A         Displays files with specified attributes. attributes  D  Directories                R  Read-only files H Hidden files               A  Files ready for archiving S System files               -  Prefix meaning not /B         Uses bare format (no heading information or summary). /C         Display the thousand separator in file sizes. This is the default. Use /-C to disable display of separator. /D         Same as wide but files are list sorted by column. /L         Uses lowercase. /N         New long list format where filenames are on the far right. /O         List by files in sorted order. sortorder   N  By name (alphabetic)       S  By size (smallest first) E By extension (alphabetic)  D  By date/time (oldest first) G Group directories first    -  Prefix to reverse order /P         Pauses after each screen full of information. /Q         Display the owner of the file. /S         Displays files in specified directory and all subdirectories. /T         Controls which time field displayed or used for sorting timefield  C  Creation A Last Access W Last Written /W         Uses wide list format. /X         This displays the short names generated for non-8dot3 file names. The format is that of /N with the short name inserted before the long name. If no short name is present, blanks are displayed in its place. /4         Displays four-digit years

Switches may be preset in the DIRCMD environment variable. Override preset switches by prefixing any switch with - (hyphen)--for example, /-W.

C:\>

Examples
The DIR command on its own list the contents of the current working directory. D:\wikibooks>dir Volume in drive D has no label. Volume Serial Number is 3007-C863

Directory of D:\wikibooks

06/11/2008 12:44    . 06/11/2008 12:44              .. 06/11/2008 12:46              Documents 06/11/2008 12:44              Messages 0 File(s)             0 bytes 4 Dir(s) 163,700,375,552 bytes free

D:\wikibooks> The  switch list the subdirectories and their contents. D:\wikibooks>dir /S Volume in drive D has no label. Volume Serial Number is 3007-C863

Directory of D:\wikibooks

06/11/2008 12:44    . 06/11/2008 12:44              .. 06/11/2008 12:46              Documents 06/11/2008 12:44              Messages 0 File(s)             0 bytes

Directory of D:\wikibooks\Documents

06/11/2008 12:46    . 06/11/2008 12:46              .. 06/11/2008 12:45                 0 Speech.txt 1 File(s)             0 bytes

Directory of D:\wikibooks\Messages

06/11/2008 12:44    . 06/11/2008 12:44              .. 0 File(s)             0 bytes

Total Files Listed: 1 File(s)             0 bytes 8 Dir(s) 163,700,375,552 bytes free

D:\wikibooks>