16 Comments

      • The following is the most basic Powershell script I could think of (in a quick & dirty way). If I find some time I will expand it to the level of your script:

        clear-history
        clear-Host
        
        $a = "" + (pwd)
        $a = $a.Replace("\", "/")
        
        "\dirtree{%"
        ".1 " + $a + "."
        
        $folders = gci | ? {$_.PSisContainer}
        
        foreach ($i in $folders){
             ".2 " +$i.name.Replace("\", "/") +"."
        }
        "}"
      • Hi Uwe,

        Thanks for taking some time and providing the script above. I really appreciate it!
        I’ll give it a try the next time I get hold of a Windows computer.

        Thanks again!
        Tom.

  1. The Perl script below should work on both Unix, Mac and Windows in order to generate the argument to \dirtree. It expects the top directory as argument on the command line; it does not need the external find command.

    #!/usr/bin/perl -w
    use strict;
    use File::Find;
    
    my $top = shift @ARGV;
    die "specify top directory\n" unless defined $top;
    chdir $top or die "cannot chdir to $top: $!\n";
    
    find(sub {
        local $_ = $File::Find::name;
        my @F = split '/';
        printf ".%d %s.\n", scalar @F, @F==1 ? $top : $F[-1];
    }, '.');
    • AntonG

      Thanks for the script, it worked great for me under windows. The only problem was underscores in the filenames.
      It would be cool to escape them with backslash right away, so that latex doesn’t throw errors on generated text.

      • Hi Anton,

        Thanks for your suggestion, I haven’t thought of that. Rather than replacing “_” with “\_” in batch or bash, you could also load the underscore package in the preamble of your document…

        \documentclass{article}
        \usepackage{underscore}
        \begin{document}
        under_score
        \end{document}
      • The underscore package causes problems when you import files with underscores in them. I prefer to just escape them in the “regular” way. Out this in the script to take care of that easily.

        $_ =~ s/_/{\\_}/g;
    • Grigory

      Thanks a lot for the article and script!
      If only the directories should be printed, the script should be modified next way:

      #!/usr/bin/perl -w
      use strict;
      use File::Find;
      
      my $top = shift @ARGV;
      die "specify top directory\n" unless defined $top;
      chdir $top or die "cannot chdir to $top: $!\n";
      
      find(sub {
              #$File::Find::name;
          my @F = split '/', $File::Find::name;
          printf ".%d %s.\n", scalar @F, @F==1 ? $top : $F[-1] if -d;
      }, '.');

Leave a Reply to Uwe ZiegenhagenCancel reply