Windows / Create txt files of subfolders with file names in it

batch filefile managementwindows

I have a folder on my HDD that contains like 1000 or so subfolders. These subfolders contain files and sometimes more subfolders.
Now what I want to have is a script that creates a .txt file for each folder on the first level. These then contain a list of file names and eventually the names of the subfolders and subfiles.
It is important to not cram it all into one file but into separate ones.

It should look like this

Name of the first folder.txt
Name of the second folder.txt
Name of the third folder.txt
Name of the fourth folder.txt
Name of the fifth folder.txt
Name of the sixth folder.txt

And Name of the first folder.txt should contain a list like this

Name of the first file.xyz
Name of the second file.zzz
Name of the third file.xyz
Name of the fourth file.zzz
Name of the fifth file.xyz

Name of Subfolder 1
  Name of file.zzz
  Name of another file.zzz

Name of Subfolder 2
  Name of file.xyz

  Name of Subsubfolder 1
    Name of file.xyz
    Name of file2.zzz

Best Answer

Quick solution using the treecommand to print the directory structure.

@echo off

:: for each directory...
for /d %%D in (*) do (
  :: we'll go into it...
  cd %%~nxD
  :: use the 'tree' command to output its 
  :: structure in a nice way...
  tree /a /f > ..\%%~nxD.txt
  :: go back...
  cd ..

  :: remove the first 3 (useless) lines from the 'tree' output
  echo %%~nxD > stackoverflowrules.tmp
  for /f "skip=3 delims=*" %%a in (%%~nxD.txt) do (
    echo.%%a >> stackoverflowrules.tmp
  )
  copy /y stackoverflowrules.tmp %%~nxD.txt
  del /f /q stackoverflowrules.tmp  
)
Related Question