Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 848
user561716 Avatar asked Sep 19 '25 12:09

user561716


2 Answers

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.

like image 79
Dennis van Gils Avatar answered Sep 21 '25 06:09

Dennis van Gils


Try Dir /s /b /ad

which is subdirectories bare and directory attribute

like image 44
PeterI Avatar answered Sep 21 '25 06:09

PeterI