Effects of dir and tree commands in batch files (.bat)

Table of contents

dir command

usage

operate

Effect

dir /?

dir

 say D:\111\111_3

dir D:\111 *.mp4

 you D:\111 /ad

you D:\111 /ar

say D:\111 /s

dir D:\111\111_3 >1bat.txt

dir D:\111 >>1bat.txt 

tree command

usage

operate

Effect

tree /?

tree

tree D:\111\111_3

tree D:\111 *.mp4

tree D:\111 >1tree.txt

tree D:\111 >>1bat.txt 

tree D:\111\111_3 /F

tree D:\111\111_3 /A

tree D:\111\111_3 /A /F

tree D:\111\111_3 /H


dir command

usage

In a batch file (.bat), dircommands are used to list files and subdirectories in a specified directory. Here are some common dircommand usages and their options:

Basic usage:

dir [目录路径]

This will list all files and subdirectories in the specified directory. If no directory path is provided, the contents of the current directory dirwill be listed by default.

List files with a specific file extension:

dir [目录路径] *.扩展名

This will list files with a specific file extension in the specified directory. For example, dir C:\Users\UserName\Documents *.txt all .txt files in the specified directory will be listed.

List read-only files:

dir [目录路径] /ar

This will list read-only files in the specified directory. Read-only files are files that cannot be modified or deleted.

List directories without files:

dir [目录路径] /ad

This will list subdirectories within the specified directory, excluding files.

Output the results to a file:

dir [目录路径] > 输出文件.txt

This will list the contents of the specified directory and save the results to a text file named "OutputFile.txt".

List file and subdirectory details:

dir [目录路径] /s

This will list all files and subdirectories in the specified directory and display their details including file size, date and time, etc.

Note that you can use commands in batch files dirto automate file operations. Depending on your needs, dirthe command can be combined with other commands to perform specific file processing tasks.

operate

Create a new txt document

enter

echo on

dir D:\111\111_3

pause

Change the extension to .bat and double-click to run. 

Effect

dir /?

D:\111>dir /?
Displays a list of files and subdirectories in the directory.

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

  [drive:][path][filename]
              specifies the drive, directory, and/or file to list.

  /A displays files with specified attributes.
  Properties D Directory R Read-only files
               H Hidden files A Files ready for archiving
               S System files I Contentless index files
               L Reparse points O Offline files
               - prefix
  / for "no" B Use empty format (no header information or summary) .
  /C Display thousands separator in file size. It's the default value. Use /-C to
              disable separator display.
  /D Same as wide, but files are listed in columns.
  /L Use lowercase letters.
  /N New long list format with filenames on the far right.
  /O List files in sorted order.
  Sort order N By name (alphabetical) S By size (smallest to largest)
               E By extension (alphabetical) D By date/time (first to last)
               G Group directory first - reverse the order of prefix
  /P in each Pause after information screen.
  /Q Displays the file owner.
  /R Displays the alternate data stream for the file.
  /S displays files in the specified directory and all subdirectories.
  /T controls the time character field displayed or used for classification.
  Time period C Creation time
              A Last access time
              W Last writing time
  /W Use wide list format.
  /X Displays the short name generated for non-8dot3 filenames. The format is /N format,
              with the short name inserted in front of the long name. If there is no short name, a blank is displayed in its place
              .
  /4 displays the year as four digits

The switch can be preset in the DIRCMD environment variable.
Replace preset switches by prefixing them with - (dash) . For example, /-W.

dir

The volume in D:\111>dir
 drive D is the Data
 volume and the serial number is FCF4-6E98.

 Directory of D:\111

2023/10/14 09:16 <DIR> .
2023/10/14 09:15 72 1.bat
2023/05/15 16:41 12,175 1.docx
2023/05/15 16:39 0 1.pptx
2023/ 05/15 16:39 6,609 1.xlsx
2023/07/02 10:58 786 11.ps1
2023/05/16 09:52 1,885 11.txt
2023/10/14 09:16 <DIR> 111_1
2023/10/ 07 14:41 <DIR> 111_2
2023/10/14 09:09 <DIR> 111_3
2023/05/18 20:41 135,401 13.jpg
2023/05/16 09:52 455 13.ps1
2023/05/25 19 :32 9,299,494 output11.mp4
               9 files 9,456,877 bytes
               4 directories 18,804,510,720 available bytes

 say D:\111\111_3

D:\111>dir D:\111\111_3
 The volume in drive D is the Data
 volume and the serial number is FCF4-6E98

 Directory of D:\111\111_3

2023/10/14 09:09 <DIR> .
2023/10/14 09:16 <DIR> ..
2023/08/24 10:53 633 11.ps1
2023/07/18 16:43 521 111.ps1
2023 /07/18 16:43 58 111.txt
2023/07/18 16:34 6,609 111.xlsx
2023/07/18 17:12 718 12.ps1
2023/08/24 11:03 730 14.ps1
2023/07 /18 17:12 12 output.txt
2023/07/18 17:13 822 Requirement description.txt
               8 files 10,103 bytes
               2 directories 18,804,510,720 available bytes

dir D:\111 *.mp4

D:\111>dir D:\111 *.mp4
 The volume in drive D is the Data
 volume and the serial number is FCF4-6E98

 Directory of D:\111

2023/10/14  09:16    <DIR>          .
2023/10/14  09:15                72 1.bat
2023/05/15  16:41            12,175 1.docx
2023/05/15  16:39                 0 1.pptx
2023/05/15  16:39             6,609 1.xlsx
2023/07/02  10:58               786 11.ps1
2023/05/16  09:52             1,885 11.txt
2023/10/14  09:16    <DIR>          111_1
2023/10/07  14:41    <DIR>          111_2
2023/10/14  09:09    <DIR>          111_3
2023/05/18  20:41           135,401 13.jpg
2023/05/16  09:52               455 13.ps1
2023/05/25  19:32         9,299,494 output11.mp4

 Directory of D:\111

2023/05/25 19:32 9,299,494 output11.mp4
              10 files 18,756,371 bytes
               4 directories 18,804,510,720 available bytes

 you D:\111 /ad

D:\111>dir D:\111 /ad
 The volume in drive D is the Data
 volume and the serial number is FCF4-6E98

 Directory of D:\111

2023/10/15 07:46 <DIR> .
2023/10/12 18:12 <DIR> ..
2023/10/14 09:16 <DIR> 111_1
2023/10/07 14:41 <DIR> 111_2
2023 /10/14 09:43 <DIR> 111_3
               0 files 0 bytes
               5 directories 12,416,651,264 available bytes

you D:\111 /ar

There is a read-only file in the folder.

D:\111>dir D:\111 /ar
 The volume in drive D is the Data
 volume and the serial number is FCF4-6E98

 Directory of D:\111

2023/10/14 10:09 482 1.bat
               1 file 482 bytes
               0 directories 12,416,651,264 available bytes 

say D:\111 /s

D:\111>dir D:\111 /s
 The volume in drive D is the Data
 volume and the serial number is FCF4-6E98

 Directory of D:\111

2023/10/14 09:16 <DIR> .
2023/10/14 09:15 72 1.bat
2023/05/15 16:41 12,175 1.docx
2023/05/15 16:39 0 1.pptx
2023/ 05/15 16:39 6,609 1.xlsx
2023/07/02 10:58 786 11.ps1
2023/05/16 09:52 1,885 11.txt
2023/10/14 09:16 <DIR> 111_1
2023/10/ 07 14:41 <DIR> 111_2
2023/10/14 09:09 <DIR> 111_3
2023/05/18 20:41 135,401 13.jpg
2023/05/16 09:52 455 13.ps1
2023/05/25 19 :32 9,299,494 output11.mp4
               9 files 9,456,877 bytes

 Directory of D:\111\111_1

2023/10/14 09:16 <DIR> .
2023/10/14 09:16 <DIR> .. 2023/05/21
19:17 1,239 11.ps1
2023/05/22 08:23 1,304 12.ps1
2023 /04/19 20:10 846,367 Screenshot_2023-04-19-20-10-49-98_e39d2c7de19156b0683cd93e8735f348.jpg
               3 files 848,910 bytes

 Directory of D:\111\111_2

2023/10/07 14:41 <DIR> .
2023/10/14 09:16 <DIR> ..
2023/10/14 09:16 <DIR> Create new folder
               0 files 0 bytes

 D:\111\111_2\Directory of the new folder

2023/10/14 09:16 <DIR> .
2023/10/07 14:41 <DIR> ..
2023/07/18 14:46 2,750 0n1p5_d_4k_result1.log
2023/10/07 14:08 559 111.ps1
2023 /10/07 14:42 640 Batch delete file name 231007.bat
               3 files 3,949 bytes

 Directory of D:\111\111_3

2023/10/14  09:09    <DIR>          .
2023/10/14  09:16    <DIR>          ..
2023/08/24  10:53               633 11.ps1
2023/07/18  16:43               521 111.ps1
2023/07/18  16:43                58 111.txt
2023/07/18  16:34             6,609 111.xlsx
2023/07/18  17:12               718 12.ps1
2023/08/24  11:03               730 14.ps1
2023/07/18  17:12                12 output.txt
2023/07/18  17:13               822 需求说明.txt
               8 个文件         10,103 字节

     所列文件总数:
              23 个文件     10,319,839 字节
              13 个目录 18,804,510,720 可用字节

dir D:\111\111_3 >1bat.txt

生成1bat.txt文件保存在.bat文件路径下,内容:

 驱动器 D 中的卷是 Data
 卷的序列号是 FCF4-6E98

 D:\111\111_3 的目录

2023/10/14  09:09    <DIR>          .
2023/10/14  09:23    <DIR>          ..
2023/08/24  10:53               633 11.ps1
2023/07/18  16:43               521 111.ps1
2023/07/18  16:43                58 111.txt
2023/07/18  16:34             6,609 111.xlsx
2023/07/18  17:12               718 12.ps1
2023/08/24  11:03               730 14.ps1
2023/07/18  17:12                12 output.txt
2023/07/18  17:13               822 需求说明.txt
               8 个文件         10,103 字节
               2 个目录 18,795,081,728 可用字节

dir D:\111 >>1bat.txt 

内容追加到1bat.txt之后。

tree命令

用法

在批处理文件(.bat)中,tree命令用于生成目录树结构,以显示指定目录及其子目录和文件的层次结构。以下是tree命令的基本用法:

tree [目录路径]

这将在命令提示符中以树状结构显示指定目录的内容。如果未提供目录路径,则tree将默认使用当前目录。

如果您想将tree命令的输出保存到文件中,可以将输出重定向到文本文件,如下所示:

tree [目录路径] > 输出文件.txt

这将生成目录树并将结果保存到名为“输出文件.txt”的文本文件中。

您还可以使用一些选项来自定义tree命令的输出,例如:

/F:显示每个目录中的文件名称。
/A:使用ASCII字符绘制树状结构,而不是默认的扩展字符。

操作

新建txt文档

输入

echo on

tree D:\111\111_3

pause

更改拓展名为.bat,双击运行。 

效果

tree /?

D:\111>tree /?
以图形显示驱动器或路径的文件夹结构。

TREE [drive:][path] [/F] [/A]

   /F   显示每个文件夹中文件的名称。
   /A   使用 ASCII 字符,而不使用扩展字符。

tree

D:\111>tree
卷 Data 的文件夹 PATH 列表
卷序列号为 FCF4-6E98
D:.
├─111_1
├─111_2
│  └─新建文件夹
└─111_3

在 D:\111路径下执行“tree”,效果与“tree D:\111”一致。

tree D:\111\111_3

D:\111>tree D:\111\111_3
卷 Data 的文件夹 PATH 列表
卷序列号为 FCF4-6E98
D:\111\111_3
没有子文件夹

tree D:\111 *.mp4

D:\111>tree D:\111 *.mp4
参数太多 - *.mp4

tree D:\111 >1tree.txt

生成1bat.txt文件保存在.bat文件路径下。

tree D:\111 >>1bat.txt 

内容追加到1bat.txt之后。

tree D:\111\111_3 /F

D:\111>tree D:\111\111_3 /F
卷 Data 的文件夹 PATH 列表
卷序列号为 FCF4-6E98
D:\111\111_3
    11.ps1
    111.ps1
    111.txt
    111.xlsx
    12.ps1
    14.ps1
    output.txt
    需求说明.txt

没有子文件夹

或者

D:\111>tree D:\111\111_3 /F
folder PATH list of volume Data
The volume serial number is FCF4-6E98
D:\111\111_3
│ 111.xlsx
│ output.txt
│ Requirement description.txt

└─ Create a new folder
        11.ps1
        111.ps1
        111.txt
        12.ps1
        14.ps1 

tree D:\111\111_3 /A

D:\111>tree D:\111\111_3 /A
folder PATH list of Volume Data The
volume serial number is FCF4-6E98
D:\111\111_3
\---New folder

tree D:\111\111_3 /A /F

D:\111>tree D:\111\111_3 /A /F
Folder PATH list of volume Data The
volume serial number is FCF4-6E98
D:\111\111_3
| 111.xlsx
| output.txt
| Requirement description.txt
|
\---New folder
        11.ps1
        111.ps1
        111.txt
        12.ps1
        14.ps1 

tree D:\111\111_3 /H

D:\111>tree D:\111\111_3 /
HInvalid switch - /H 

Guess you like

Origin blog.csdn.net/weixin_56337147/article/details/133821081