Submitted By: David Gray

A vastly improved version of "Save File Statistics for a Folder," which I submitted the week the Community-Submitted Scripts Center opened for business back when dinosaurs roamed Earth. Unlike its predecessor, this version optionally scans all subdirectories of the specified directory.

Visual Basic
Edit|Remove

#!/usr/bin/perl

# Name:                         P6FileStatsWin

# Synopsis:                     Create an index of file statistics on a
directory suitable
#                                       for importing into Microsoft Excel.

# Author:                       David A. Gray for P6 Consulting,
#
# Written:                      Wednesday, 27 June 2001,
#
# Updated:                      January 2009,
#
# Dependencies:         This script uses the following modules, all of which
are
#                                       included in the standard Perl
distribution.
#
#                                       - File::Basename        to parse the
filespec so we can
#
determine whether it's fully qualified.
#
#                                       - Cwd                   to get the
current working directory.
#
# Portability:          Variable fileparse_set_fstype, used by the File
module, is
#                                       set for the Win32 file system.
Change it, and this script
#                   should work on any platform that supports Perl.
#
# Revision History
#
# Date       Version Author Synopsis
# ---------- ------- ------
----------------------------------------------------
# 2001/06/27 1.00    DAG    Initial release.
#
# 2008/12/20 2.00    DAG    Add support for subdirectories, eliminate
wasteful
#                           system call to delete the output file, and clean
up
#                           the documentation and help messages.

#
----------------------------------------------------------------------------
#       Import required modules, all of which are in the Perl distribution.
#
----------------------------------------------------------------------------

use Cwd ;
use File::Basename ;

#
----------------------------------------------------------------------------
#       Display standard program BOJ signature.
#
----------------------------------------------------------------------------

print STDERR "P6FileStatsWin version 2.00\n" ; print STDERR "Copyright 2001, P6 Consulting, 2008, WizardWrx. All rights reserved world wide.\n" ; print STDERR "Both P6 Consulting and WizardWrx are trade marks of Simple Soft Services, Inc.\n\n" ; ( $sec , $min , $hour , $mday , $mon , $year , $wday , $yday , $isdst) = localtime ( time ) ; $thiswkday = ( Sunday , Monday , Tuesday , Wednesday , Thursday , Friday , Saturday ) [ ( localtime ) [ 6 ] ] ; $thismonth = ( January , February , March , April , May , June , July , August , September , October , November , December ) [ ( localtime ) [ 4 ] ] ;

$year = $year + 1900 ;
printf ( STDERR "%1s %02d %1s %04d - %02d:%02d:%02d\n\n", $thiswkday , $mday , $thismonth , $year , $hour , $min , $sec ) ;

$cmd = 'dir /b' ;                               # By default, confine the
report to a single directory.

# Figure out what directory to read and, coincidentally, where to put the statistics file.

if ( $#ARGV > -1) { # Output the page here.
        if ( $ARGV [ 0 ] =~ /\/|-s/i )
        {
                $cmd = 'dir /b /s' ;    # Check for subdirectory switch.
                shift ;
        }

        $outfile = $ARGV [ 0 ] ;
        shift ;

        if ( $ARGV [ 0 ] =~ /\/|-s/i )
        {
                $cmd = 'dir /b /s' ;    # Check again for subdirectory
switch.
                shift ;
        }
} else {
        print STDERR "Usage P6FileStatsWin NameOfIndexPage.CSV\n\n" ;
        print STDERR "Assuming that PERL.EXE is on a search path and\nthe .PL extension is associated with it:\n\n" ;
        print STDERR "1) Open a command prompt in the directory\n   for
which you want to create an index of GIF files.\n" ;
        print STDERR "2) Enter the command P6FileStatsWin followed by\n the name you want to give to the index file.\n" ;
        print STDERR "   If you enter a fully qualified file name for the
index,\n" ;
        print STDERR "   the script will index the same directory.\n" ;
        print STDERR '3) When the command prompt window answers "Done!"' ;
        print STDERR "\n   your index is ready. It should be on display\n
in Microsoft Excel." ;
        print STDERR "\n\n" ;
        print STDERR "You can easily view your new index by typing its file name\nat the command prompt.\n" ;
        print STDERR "The index will open in your default spreadsheet program.\n\n" ;
        print STDERR "Now, have fun!" ;
        exit ;
}

#
----------------------------------------------------------------------------
#       Get CWD for display.
#
----------------------------------------------------------------------------

$currdir = cwd ( ) ;
fileparse_set_fstype ( 'MSWin32' ) ;

( $filespec , $filepath , $dummy ) = fileparse ( $outfile ) ;

if ( $filepath =~ /^\./ )
{
        $filepath = $currdir ;
} else {
        chdir ( $filepath ) || die "ABORTING! -- Unable to change to directory $filepath\n" ; }

@a = `$cmd` ; # Get the directory before we add our file to it.

open ( OFN ,">$outfile" ) || die "ABORTING! - Unable to open output file $outfile.\n"  ;

$dev = $ino = $mode = $nlink = $uid = $gid = $rdev = $size = $atime = $mtime = $ctime = $blksize = $blocks = "" ;

#
----------------------------------------------------------------------------
#       Print field name header record.
#
----------------------------------------------------------------------------

print OFN <<"PART1" ;
"FileName","Drive_#","Access_Mode","Size","Last_Access","Last_Modify","Creat
ed"
PART1

# Print detail for each file in the directory.

foreach ( @a )
{
        # Note: One chop only. Though the documentation doesn't say so, Perl for
        #               Win32 treats the CR and the LF as if it were a
single character so
        #               that the behavior of the program is consistent on
Unix where a LF by
        #               itself is sufficient and on Win32 where CR/LF pairs
are used.

        chop ;
        $fn = $_ ;
        if ( -d $fn ) {
                print STDERR "$fn is a directory-skipped.\n" ;
        } else {
                if ( $fn eq $outfile ) {
                        print STDERR "$fn is the report file-skipped.\n" ;
                } else {
                        ( $dev , $ino , $mode , $nlink , $uid , $gid , $rdev , $size , $atime , $mtime , $ctime , $blksize , $blocks ) = stat $fn ;
                        ( $sec , $min , $hour , $mday , $mon , $year , $wday , $yday , $isdst) = localtime ( $atime ) ;
                        $mon++ ;
                        $year = $year + 1900 ;
                        $showatime = sprintf ( "%04d/%02d/%02d %02d:%02d:%02d" , $year , $mon , $mday , $hour , $min , $sec ) ;
                        ( $sec , $min , $hour , $mday , $mon , $year , $wday , $yday , $isdst) = localtime ( $mtime ) ;
                        $mon++ ;
                        $year = $year + 1900 ;
                        $showmtime = sprintf ( "%04d/%02d/%02d %02d:%02d:%02d" , $year , $mon , $mday , $hour , $min , $sec ) ;
                        ( $sec , $min , $hour , $mday , $mon , $year , $wday , $yday , $isdst) = localtime ( $ctime ) ;
                        $mon++ ;
                        $year = $year + 1900 ;
                        $showctime = sprintf ( "%04d/%02d/%02d %02d:%02d:%02d" , $year , $mon , $mday , $hour , $min , $sec ) ;

                        print OFN
"\"$fn\"\,\"$dev\"\,\"$mode\"\,\"$size\"\,\"$showatime\"\,\"$showmtime\"\,\"
$showctime\"\n" ;
                }       # End of ELSE block, if ( -d $fn )
        }       # End of ELSE block, if ( -d $fn )
}       # End of foreach ( @a )

#
----------------------------------------------------------------------------
#       Clean up.
#
----------------------------------------------------------------------------

close OFN ;

print STDERR "Done!\n\n" ;
print STDERR "Launching $outfile in Microsoft Excel.\n\n" ; $cmd = "$outfile" ; `$cmd` ; print STDERR "Thank you for using P6FileStatsWin!\n" ; if ( $filepath =~ /^\./ ) {
        chdir ( $currdir ) || die "ABORTING! -- Unable to change to directory $filepath\n" ; }

1 ;