Windows Batch Scripting/File and Directory Management

From Wikibooks, open books for an open world
Jump to navigation Jump to search

2. File and Directory Management[edit | edit source]

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[edit | edit source]

  • 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:
C:\>CD
C:\

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

  • Type DIR:
C:\>DIR
TODO: Get the output for a computer with a mnimal set of directories.

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:

C:\>MD cmd_guide

C:\>

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

C:\>CD cmd_guide

C:\cmd_guide>

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.

C:\cmd_guide>CD
C:\cmd_guide

C:\cmd_guide>

DIR Usage and Options[edit | edit source]

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

C:\cmd_guide>DIR
 Volume in drive C is MyCDrive
 Volume Serial Number is 64A5-9C22

 Directory of C:\cmd_guide

31/12/2008  13:31    <DIR>          .
31/12/2008  13:31    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  146,065,076,224 bytes free

C:\cmd_guide>

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 .

C:\cmd_guide>DIR .
 Volume in drive C is MyCDrive
 Volume Serial Number is 64A5-9C22

 Directory of C:\cmd_guide

31/12/2008  13:31    <DIR>          .
31/12/2008  13:31    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  146,065,100,800 bytes free

Compare the outputs in the listings for DIR and DIR .

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

TODO.

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

C:\cmd_guide>MD HeadQuarters

C:\cmd_guide>DIR /B
HeadQuarters

C:\cmd_guide>DIR /S /B
C:\cmd_guide\HeadQuarters

C:\cmd_guide>
C:\cmd_guide>MD HeadQuarters

C:\cmd_guide>DIR /B
HeadQuarters

C:\cmd_guide>DIR /S /B
C:\cmd_guide\HeadQuarters

C:\cmd_guide>

MD Usage[edit | edit source]

Creating multiple directories.

C:\cmd_guide>MD Nottingham Derby Newark Lincoln

C:\cmd_guide>DIR /S /B
C:\cmd_guide\Derby
C:\cmd_guide\HeadQuarters
C:\cmd_guide\Lincoln
C:\cmd_guide\Newark
C:\cmd_guide\Nottingham

C:\cmd_guide>

Creating nested directories.

C:\cmd_guide>MD Grantham\Menus\Season

C:\cmd_guide>DIR /S /B
C:\cmd_guide\Derby
C:\cmd_guide\Grantham
C:\cmd_guide\HeadQuarters
C:\cmd_guide\Lincoln
C:\cmd_guide\Newark
C:\cmd_guide\Nottingham
C:\cmd_guide\Grantham\Menus
C:\cmd_guide\Grantham\Menus\Season

C:\cmd_guide>

Filtering/Searching with the DIR Command[edit | edit source]

C:\cmd_guide>DIR /S /B *t*a*
C:\cmd_guide\Grantham
C:\cmd_guide\Nottingham

C:\cmd_guide>DIR /S /B *t?a*
C:\cmd_guide\Grantham

C:\cmd_guide>

Use of "?".
C:\cmd_guide>DIR /S /B *t*a*
C:\cmd_guide\Grantham
C:\cmd_guide\Nottingham

C:\cmd_guide>DIR /S /B *t?a*
C:\cmd_guide\Grantham

C:\cmd_guide>

Match an exact number of letters.

C:\cmd_guide>DIR /B ??a*
Grantham
HeadQuarters

C:\cmd_guide>DIR /B *a*
Grantham
HeadQuarters
Newark
Nottingham

C:\cmd_guide>

DIR Text From Book Page - Needs Reviewing[edit | edit source]

This command displays the contents of directories. To get help on the dir command type dir /?:

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[edit | edit source]

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    <DIR>          .
06/11/2008  12:44    <DIR>          ..
06/11/2008  12:46    <DIR>          Documents
06/11/2008  12:44    <DIR>          Messages
               0 File(s)              0 bytes
               4 Dir(s)  163,700,375,552 bytes free

D:\wikibooks>

The /S 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    <DIR>          .
06/11/2008  12:44    <DIR>          ..
06/11/2008  12:46    <DIR>          Documents
06/11/2008  12:44    <DIR>          Messages
               0 File(s)              0 bytes

 Directory of D:\wikibooks\Documents

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

 Directory of D:\wikibooks\Messages

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

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

D:\wikibooks>