buttonsrtoys
buttonsrtoys

Reputation: 2771

Batch file to list files and folders in a simple format

I need a batch file that recursively list folders and their files (with dates) to a text file. I've found several online, like this one

echo off
for /d %%a in (*.*) do dir "%%a" >> Report.txt

But none in the simple format that I need. The batch file would be run from the top search directory. I'd like to full path to the search folder listed at the top, followed by the folder and subfolder names, even if they're empty, without their paths, each followed by their files accompanied with a time stamp. So, something like this:

FullPathToTopFolder 
    FolderName1
        DateStamp   FileName1.txt      
        DateStamp   FileName2.txt      
        DateStamp   FileName3.txt      
    FolderName2
        DateStamp   FileName4.txt      
        DateStamp   FileName5.txt       
    FolderName3
        DateStamp   FileName6.txt      
    FolderName3
        /SubFolderName1
            DateStamp   FileName7.txt      
            DateStamp   FileName8.txt      
    FolderName4 
        /SubFolderName2
        /SubFolderName3
        /SubFolderName4
            DateStamp   FileName9.txt      
            DateStamp   FileName10.txt  

Upvotes: 0

Views: 13923

Answers (1)

dbenham
dbenham

Reputation: 130819

This script allows the top folder to be specified as the first parameter. If not provided, then uses the current directory as the top folder.

Edited to redirect output to a file

@echo off
setlocal disableDelayedExpansion
pushd %1
set "tab=    "
set "indent="
call :run >report.txt
exit /b

:run
for %%F in (.) do echo %%~fF

:listFolder
setlocal
set "indent=%indent%%tab%"
for %%F in (*) do echo %indent%%%~tF   %%F
for /d %%F in (*) do (
  echo %indent%.\%%F
  pushd "%%F"
  call :listFolder
  popd
)
exit /b

Upvotes: 3

Related Questions