Generate an HTML list of files from a directory and all its subdirectories
Generating an alphabetically sorted HTML list out of file names (not folder names) only, without path, from a directory and all its subdirectories in Windows Command Prompt. The list is then printed into an HTML document also generated during the process.
Code for .BAT file:
@ECHO OFF
SETLOCAL EnableDelayedExpansion
:: Set directory to list
SET "DIR=C:\TEST\"
:: Creating a temporary file for an interim storage
SET "TMP=%TEMP%\FILES_%RANDOM%.TMP"
:: Generating the top part of HTML document
ECHO ^<^^!DOCTYPE html^> > files.htm
ECHO ^<html^> >> files.htm
ECHO ^<head^> >> files.htm
ECHO ^<title^>%DIR%^</title^> >> files.htm
ECHO ^</head^> >> files.htm
ECHO ^<body^> >> files.htm
:: Generating an unordered list
ECHO ^<ul^> >> files.htm
FOR /R "%DIR%" %%G IN (*) DO (
ECHO ^<li^>%%~NXG^</li^>
) >> "%TMP%"
SORT "%TMP%" >> files.htm && DEL "%TMP%"
ECHO ^</ul^> >> files.htm
:: Generating the bottom part of HTML document
ECHO ^</body^> >> files.htm
ECHO ^</html^> >> files.htm
Operating systems
- Windows