Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Identifying Directory

by Anonymous Monk
on Jul 11, 2006 at 13:04 UTC ( [id://560438]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

How could a make a directory print different if found? Here are more details
All that has to be done is to give a path to be search on $dir, and this code will print a directory tree out of it, but I would like to see if inside of the given path on $dir more directories are found I would like to see them highlighted for further display options.
Here is the code:

#!/perl/bin/perl use warnings; use strict; use File::Slurp::Tree; use CGI qw(:header); use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; # The tree datastructure is a hash of hashes. The keys of # each hash are names of directories or files. Directories # have hash references as their value, files have a scalar # which holds the contents of the file. my $dir = "c:/progra~1/apache~1/apache2/cgi-bin/test"; my %tree; my $tree = slurp_tree($dir); my $depth = 0; print header(); print_keys($tree); sub print_keys { my $href = shift; $depth++; foreach ( keys %$href ) { if(-d $_){ print "<b>Dir exists= $_</b><br>";} print '****' x $depth, "<input type=checkbox> --$_<br>\n"; print_keys( $href->{$_} ) if ref $href->{$_} eq 'HASH'; } $depth--; }

Thanks a lot!

Replies are listed 'Best First'.
Re: Identifying Directory
by VSarkiss (Monsignor) on Jul 11, 2006 at 13:58 UTC
Re: Identifying Directory
by gasho (Beadle) on Jul 11, 2006 at 19:24 UTC
    This will work on Windows
    ###################################################################### +####### # It will return an array of @DirectoryPaths. # $LocationOfThePostDir='C:\Post'; # Usage: @DirPaths=extractDirectoryPaths($LocationOfThePostDir); # For example if you have Underneath of C:\Post \A \B\C # It will return array members like: # C:\\Post\\A # C:\\Post\\B # C:\\Post\\B\\C sub extractDirectoryPaths #(C:\\Post) { my ($RootDir)=@_; my(@A,$e,$C,@B,$b,$a,$c,@E,@D,$j,@F,$y,$x,$ff,@C,@DirectoryPaths); #Purpose is to extract all paths to directories and subdirectories @A=`dir $RootDir /s/w` ; #@A=`dir $_[0] /s/w` ; foreach $e (@A){ $C=substr($e,0,10); #Extract only string that starts with Directory if($C eq " Directory"){ push(@B,$e); } } # Directory of C:\cr1_qc\crnqcV3\CM\Bering\Results\object_test_ +mssql\Root\DirectorySuite\GroupsandRolesSuite\OutputPages # I want to start from C: foreach $a (@B){ push(@C,substr($a,14)); } #Put backslash \in front of\ so it will be C:\\cr1_qc\\ foreach $b (@C){ @D=split(/\\/,$b); $c=join ("\\\\",@D); push(@E,$c); } #Remove lash char that is whitespace @F=trim(@E); # foreach $j (@E) # { # $y=substr($j,0,-1); # push(@F,$y); # } foreach $x (@F){ $ff=substr($x,1,1); if($ff eq ":"){ push(@DirectoryPaths,$x); } } return @DirectoryPaths; }#EndOfextractDirectoryPaths

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://560438]
Approved by holli
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-20 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found