#!/usr/bin/perl -w
#
# $Revision: 1.4 $ $Date: 2012-07-26 17:11:49-04 $
# $Source: /home/vogelke/bin/RCS/tree2excel,v $
# $Host: sys7.com $
# $UUID: 1f381b44-c0f6-3ec7-a7dc-90d73052651a $
#
#<tree2excel: read output from find, print comma-separated filetree
# usage: find . -print | grep -v '/_vti_cnf' | sort -f | tree2excel

use strict;

my $orig;    # original filename.
my $base;    # filename minus directories.
my $ft;      # filetype: directory or anything else.
my $pre;     # whatever precedes the filename, if anything.
my $rest;    # rest of the line.
my $out;     # output record.

# This tends to mess up Excel files.
## print <<'EndHeader';
## Field 1: number of times this link was actually clicked.
## Field 2: "Folder" if it's a directory, "-" if otherwise.
## Anything else is the filename, indented to show its place in the filetree.
## 
## EndHeader

# We're writing DOS-formatted records.
$\ = "\r\n";

# Read output from find.
while (<>) {
    chomp;
    next unless length;
    next if $_ eq '.';

    # Do we have something like a file-count and tab
    # before the filename?

    $pre = '';
    if (/\t/) {
        ($pre, $rest) = split(/\t/);
        $_ = $rest;
    }

    # Filetype?
    $ft  = -d $_ ? '"Folder"' : '"-"';

    # Handle the directory part of the filename.

    $orig = $_;

    if (m![^/]*/([^/]*)$!) {
        $base = $1;
        s![^/]*/([^/]*)$!,[#FILE#]!s;
    }

    # Escape any double-quotes in the filename.

    $base =~ s/"/""/g;

    # Handle the base part of the filename.
    # After this, $_ holds the indentation plus basename
    # and $orig holds the complete path.

    s![^/]*/!,!sg;
    s!\[#FILE#\]!"$base"!;

    # Generate and print the final CSV record.

    $out  = length($pre) ? $pre : '';
    $out .= "$ft,$_,";
    print "$out";
}

exit(0);
