Is there any software for Windows which will crawl through a directory and subdirectories and print all the source code files it finds into a single document, preferably indexed? For example I have a mix of Python and .BAT files and HTML and CSS templates.


You can use the following line in a CMD command prompt to get all the source code into one text file:

for /r %a in (*.py *.bat *.htm *.html *.css) do (echo ********* Filename: %a ********* & type %a) >> SourceFileContents.txt

The command will get files based on their filename extensions (like .py for Python and .bat for batch files, etc..) so you can change or add any file type you want.

This will give you a plain text file with file names and contents like this:

********* Filename: D:\Test\file.htm ********* 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<meta charset="UTF-8">

If you want the result in a PDF file with an index bookmark for each file name, there is a way to do that using the LEADTOOLS ePrint printer driver (Disclaimer: I work for the vendors of ePrint):

  1. Use the following batch file to obtain a formatted HTML file with all the source files listed in it. Each file name will be written before its contents. The file names will have a different font type (Arial) to use them for indexing in step 3 below:

    @echo off
    set outputFile=SourceFileContents.tmp
    set FinalOutputFile=SourceFileContents.htm
    del %FinalOutputFile%
    echo ^<html^>  > %outputFile%
    echo ^<body^> >> %outputFile%
    echo ^<font face="Arial"^> >> %outputFile%
    for /r %%a in ((*.py *.bat *.htm *.html *.css)) do (
    find /i "</xmp>" "%%a" > nul
    if errorlevel 1 (
    echo ^<hr^> File: "%%a" 
    echo ^<xmp^> 
    type "%%a"
    echo ^</xmp^>
    ) else echo ^<hr^>**** Skipped File: "%%a" **** ^<br^>
    ) >> %outputFile%
    echo ^</font^> >> %outputFile%
    echo ^</body^> >> %outputFile%
    echo ^</html^> >> %outputFile%
    ren %OutputFile% %FinalOutputFile%
  2. Open the HTML file in a browser like FireFox and print it to LEADTOOLS ePrint, in order to save it as PDF.
  3. Before saving, in the Enhanced PDF options dialog of ePrint, specify one level of bookmarks, and set the font for that level to the same font used in the batch file in step 1, which is Arial. These options are explained in this help page.

The resulting PDF will look like this: Result PDF screenshot

Note that the generated HTML file uses the "xmp" element. This is a mostly obsolete html tag, but it makes creating the file much easier using a batch file. Also note that some of the source files being listed may already contain an "xmp" element, which would ruin the output for that file. Although this is unlikely, the batch file checks for this element before listing the file. So if a file contains <\xmp> in it, the batch file skips it from the output. That’s why the test output above skips the batch file that was used to create it.

If you would like to try ePrint, you can download a free evaluation from this page.

| improve this answer | |

Note I solved this for myself by customizing this GitHub to my needs: https://github.com/konginyan/code2word/projects

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.