Created
May 29, 2015 15:32
-
-
Save ultimape/ae65a9dae6967cc79246 to your computer and use it in GitHub Desktop.
A utility to emulate MS-Dos's tree command that I wrote while in college while learning Perl. Caution, may launch all the nukes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# FILE : vtree | |
# AUTHOR : Nicholas Perry | |
# EMAIL : [email protected] (defunct) | |
# DATE CREATED : 03/08/2005 | |
# DATE MODIFIED : N/A | |
# DESCRIPTION : Perl: Recursively finds and displays the filetree. | |
print "\n<INIT: '$0'>\n"; | |
# BEGIN CODE HERE. | |
# Start command line argument parse. | |
# Set Defaults. | |
$showall = "standard"; | |
# Get Args. | |
foreach(@ARGV){ | |
if($_ eq "--out" && !defined $fileout) # Open output file | |
{ | |
open (FILEOUT,">vtreelist.txt") | |
or die "error outputting to vtreelist.txt"; | |
$fileout = "true"; | |
} | |
if($_ eq "--hide"){ | |
$showall = "hidemode"; | |
} | |
if($_ eq "--all"){ | |
$showall = "allmode"; | |
} | |
if($_ eq "--help"){ | |
if(defined $fileout){ | |
close FILEOUT; | |
} | |
print "usage:\n"; | |
print " --out creates a file vtreelist.txt\n"; | |
print " --hide hides \# and ~ backup files\n"; | |
print " --all shows hidden files aswell\n"; | |
exit(); | |
} | |
} | |
# Verbose. | |
print "(writing to vtreelist.txt)\n" if(defined $fileout); | |
print "(diplay mode = \"$showall\")\n"; | |
# Clean up. | |
shift @ARGV while($ARGV[0] =~ /^--/); | |
# End command line argument parse. | |
$depth = 0; # This doesn't really do anything, but it helped while debugging. | |
if($ARGV[0] =~ /~\//) | |
{ | |
&listdir("$ARGV[0]",""); # Pass ARGV[0] since it is a full path. | |
} else { | |
&listdir("./$ARGV[0]",""); # Add a ./ because it is relational based. | |
} | |
# Warning, Recursive! | |
sub listdir { | |
my $curdir = $_[0]; # Get current directory. | |
shift @_; # Remove current directory from list. | |
my @position; # Stores what to print in front of filename. | |
push @position,@_; # Place rest of crap onto position, initially nothing. | |
my @files; # List of files in current directory. | |
my @unsortfiles; # Temporary list of files to be sorted into @files. | |
my $numfiles = 0; # Number of file in current directory being processed. | |
my $totfiles = 0; # Total number of files in current directory. | |
# Get list of files from directory. | |
if($showall eq "allmode") | |
{ | |
open ( FILE_LIST , "/bin/ls -a $curdir |") | |
or die "ls did not open for \"$dir\" at:"; | |
} else { | |
open ( FILE_LIST , "/bin/ls $curdir |") | |
or die "ls did not open for \"$dir\" at:"; | |
} | |
@unsortfiles = <FILE_LIST>; | |
chomp @unsortfiles; | |
# Sort folders to be done last in current directory. | |
foreach (@unsortfiles) | |
{ | |
if ($_ ne "." && $_ ne "..") | |
{ | |
if( $showall ne "hidemode" || | |
$_ !~ m/^\#/ && | |
$_ !~ m/\~$/ ) | |
{ | |
if(-d "$curdir/$_") | |
{ # If its a directory. | |
push @files, $_; # Put on end. | |
} else { | |
unshift @files, $_; # Else put on beggining. | |
} | |
$totfiles++; | |
} | |
} | |
} | |
push @position, " +---"; # Add standard file pointer line to offset files. | |
foreach(@files) | |
{ # for each file in directory. | |
$numfiles++; | |
if($numfiles == $totfiles) | |
{ | |
pop @position; | |
push @position," \\---"; # Replace standard pointer with ender. | |
} | |
if(-d "$curdir/$_") | |
{ # If its a directory. | |
push @position,"[d]"; # Temp append the directory tag. | |
# Display line with file. | |
print FILEOUT join(/""/,@position),"$_\n"; | |
print join(/""/,@position),"$_\n"; | |
pop @position; | |
} else { | |
# Display line with file. | |
print FILEOUT join(/""/,@position),"$_\n"; | |
print join(/""/,@position),"$_\n"; | |
} | |
if(-d "$curdir/$_"){ # If its a directory. | |
$depth++; | |
pop @position; # Remove file pointer. | |
if($numfiles == $totfiles) # Pad end of pointer. | |
{ | |
push @position, " "; # Use if ended current directory. | |
} else { | |
push @position, " | "; # Use if not ended. | |
} | |
&listdir("$curdir/$_",@position); # Go down directory. (RECURSIVE) | |
pop @position; | |
push @position, " +---"; # Restore file pointer | |
$depth--; | |
} | |
} | |
if($fileout eq "true"){ | |
close FILE_LIST; | |
} | |
} | |
# END CODE HERE. | |
print "<END>\n\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment