跳轉到內容

Windows 批處理指令碼/檔案和目錄管理

來自華夏公益教科書,開放的書籍,用於開放的世界

2. 檔案和目錄管理

[編輯 | 編輯原始碼]

本章涵蓋以下命令

  • ATTRIB - 顯示或更改檔案屬性。
  • CD、CHDIR - 顯示當前目錄的名稱或更改當前目錄。
  • COPY - 將一個或多個檔案複製到另一個位置。
  • DEL - 刪除一個或多個檔案。
  • DIR - 顯示目錄中檔案和子目錄的列表。
  • ERASE - 刪除一個或多個檔案。
  • MD、MKDIR - 建立目錄。
  • MOVE - 將一個或多個檔案從一個目錄移動到另一個目錄。
  • RMDIR - 刪除目錄。
  • REN、RENAME - 重新命名檔案或檔案。
  • POPD - 恢復由 PUSHD 儲存的先前當前目錄值。
  • PUSHD - 儲存當前目錄然後更改它。
  • RD - 刪除目錄。
  • REPLACE - 替換檔案。
  • TREE - 以圖形方式顯示驅動器或路徑的目錄結構。
  • XCOPY - 複製檔案和目錄樹。

DIR、CD 和 MD 命令

[編輯 | 編輯原始碼]
  • 開啟一個 cmd.exe 視窗(參見第 1 章)。

當前目錄命令有兩個名稱,CD 和 CHDIR。這兩個命令的工作方式完全相同,從現在起我們將使用較短的名稱 CD。CD 命令顯示當前目錄的名稱。

  • 輸入 CD
C:\>CD
C:\

沒有引數的 DIR 命令列出目錄的內容。

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

建立目錄命令有兩個名稱,MD 和 MKDIR。這兩個命令的工作方式完全相同,同樣,我們在示例中將使用較短的形式。

我們首先建立一個新目錄

C:\>MD cmd_guide

C:\>

現在我們將使用帶引數的 CD 命令,並將“C:\cmd_guide”設為當前目錄。

C:\>CD cmd_guide

C:\cmd_guide>

請注意,當前目錄顯示在命令提示符中。

您已經瞭解了 CD、DIR 和 MD 命令的基本知識。現在我們將更深入地研究它們。

現在沒有引數的 CD 命令顯示 C:\cmd_guide 目錄為當前目錄。

C:\cmd_guide>CD
C:\cmd_guide

C:\cmd_guide>

DIR 用法和選項

[編輯 | 編輯原始碼]

在上面的示例中,MD 命令用於建立一個單個目錄。我們將使用 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,076,224 bytes free

C:\cmd_guide>

DIR 命令的標準輸出顯示目錄 C:\cmd_guide 為空,但它包含兩個目錄引用“.”和“..”。一個點“.”表示當前目錄,“..”是父目錄。DIR 命令是 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

比較 DIR 和 DIR . 的列表中的輸出。

命令 DIR .. 將列出父目錄的內容。

TODO.

建立一個目錄,使用 DIR /B 和 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 用法

[編輯 | 編輯原始碼]

建立多個目錄。

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>

建立巢狀目錄。

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>

使用 DIR 命令篩選/搜尋

[編輯 | 編輯原始碼]
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>

匹配確切的字母數。

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

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

C:\cmd_guide>

書頁中的 DIR 文字 - 需要審查

[編輯 | 編輯原始碼]

此命令顯示目錄的內容。要獲取有關 dir 命令的幫助,請鍵入 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:\>

單獨的 DIR 命令列出當前工作目錄的內容。

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>

/S 開關列出子目錄及其內容。

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>
華夏公益教科書