Windows 批处理脚本/文件和目录管理
外观
本章涵盖以下命令
- ATTRIB - 显示或更改文件属性。
- CD、CHDIR - 显示当前目录的名称或更改当前目录。
- COPY - 将一个或多个文件复制到另一个位置。
- DEL - 删除一个或多个文件。
- DIR - 显示目录中文件和子目录的列表。
- ERASE - 删除一个或多个文件。
- MD、MKDIR - 创建目录。
- MOVE - 将一个或多个文件从一个目录移动到另一个目录。
- RMDIR - 删除目录。
- REN、RENAME - 重命名文件或文件。
- POPD - 恢复由 PUSHD 保存的先前当前目录值。
- PUSHD - 保存当前目录然后更改它。
- RD - 删除目录。
- REPLACE - 替换文件。
- TREE - 以图形方式显示驱动器或路径的目录结构。
- XCOPY - 复制文件和目录树。
- 打开一个 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>
在上面的示例中,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>
创建多个目录。
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>
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 /?
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>