简体   繁体   中英

Batch file to list directories recursively in Windows as in linux

I want to create a batch file that lists directories recursively in Windows but in a linux-like format.For example consider the following batch file:

@echo off
tree /f /a C:\Windows\Boot\dvd>%userprofile%\desktop\tree_output.txt
exit

This batch file echoes or saves the output of the tree command in a text file named tree_output.txt in the user's desktop folder ( the /f argument lists all files in subdirectories as well and the /a argument displays the output of tree command in ASCII text format and not in a graphical format). The tree_output.txt contains the following output:

Folder PATH listing for volume System
Volume serial number is 4E00004E 545D:650E
C:\WINDOWS\BOOT\DVD
+---EFI
|   |   BCD
|   |   boot.sdi
|   |   
|   \---en-US
|           efisys.bin
|           efisys_noprompt.bin
|           
\---PCAT
     |   BCD
     |   boot.sdi
     |   etfsboot.com
     |   
     \---en-US
           | bootfix.bin

However, the ls -R command in linux would have listed out directories recursively like this:

C:\WINDOWS\BOOT\DVD\
C:\WINDOWS\BOOT\DVD\EFI
C:\WINDOWS\BOOT\DVD\EFI\BCD
C:\WINDOWS\BOOT\DVD\EFI\boot.sdi
C:\WINDOWS\BOOT\DVD\EFI\en-US
C:\WINDOWS\BOOT\DVD\EFI\en-US\efisys_noprompt.bin
C:\WINDOWS\BOOT\DVD\PCAT
C:\WINDOWS\BOOT\DVD\PCAT\BCD
C:\WINDOWS\BOOT\DVD\PCAT\boot.sdi
C:\WINDOWS\BOOT\DVD\PCAT\etfsboot.com
C:\WINDOWS\BOOT\DVD\PCAT\en-US
C:\WINDOWS\BOOT\DVD\PCAT\en-US\bootfix.bin

Is there any way to get the above output in a text file in Windows by creating a batch file? What I have tried to do so far can be summarised as:

  • Use the for command to check the text file created by the above mentioned batch file to check the presence of the \\ character to identify directories (as you can see in the output text file) along with the findstr command. This method failed partly because the directories in the root of the directory whose tree is being done ( C:\\WINDOWS\\BOOT\\DVD\\ in this case) are identified by the + character (as you can see in the output text file).

What I have understood so far is that this cannot be achieved by using any in-built commands with any parameters or arguments from cmd. My idea to solve this is to first get the tree using tree /f /a command in a batch file and then sort it out using any other batch file or maybe even a c++ program to get the output as in linux .

I think you're looking for

dir /a:d /s /b /o:n

The /s is used for recursion, /b for bare format, so without filesizes etc, /a:d is used to only display directories, and /o:n is to order by name.

Try Dir /s /b /ad

which is subdirectories bare and directory attribute

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM