Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Searching a file system

by Dabuel (Initiate)
on Dec 22, 2010 at 04:54 UTC ( #878433=perlquestion: print w/ replies, xml ) Need Help??
Dabuel has asked for the wisdom of the Perl Monks concerning the following question:

I have run into an issue with a quick script im writing to search a file system for .nsf files. For the most part the script works. But when it find's a .nsf file and tries to print it out, it prints out the path to the file before it then the file itself. Example: C:\random.txt\A.nsf instead of C:\A.nsf Here is the code, i hope someone can help:
#!/usr/bin/perl use strict; use warnings; my ($file, $prefix); walk("C:\\"); sub walk { $file = shift; $prefix = shift; #($file, $prefix) = @_; # You can also use shift. my $file = + shift; my $prefix = shift; $prefix ||= ""; # Set $prefix to "" if undefined ( +first case) #print "$file\n"; if ("$file"=~m/.nsf$/){ print "Prefix: $prefix\nFile:$file\n"; # print current + file. } if(-d $file) { # is this a directory? chdir $file; opendir(DIR, "."); my @files = grep {$_ !~ /^\.\.?$/} readdir(DIR); # Ignore + . and .. closedir DIR; walk($_, "$prefix$file\\") foreach @files; # recur +sive call chdir ".."; } }

Comment on Searching a file system
Download Code
Re: Searching a file system
by sflitman (Hermit) on Dec 22, 2010 at 05:28 UTC
    The best thing about Perl is you don't have to Reinvent the Wheel, which I'm afraid is what you are doing there. Why not use File::Find? It's much more concise and you don't have to deal with all the same bugs. Your code is probably not portable since it is going to rely on Windows paths with backslashes.

    SSF

Re: Searching a file system
by fisher (Priest) on Dec 22, 2010 at 13:33 UTC

    I agree with previous orator. In case you need your own wheel, let's examine provided code.

    • First of all, you need to check for directory _before_ matching against /.nsf/. Is it safe to assume file before actual check?
    • Second, I personally dislike the idea to change directory using chdir twice. You can work indtead with full path filenames.

    right now I have no time, sorry, but I can give a link to similar code sample.
Re: Searching a file system
by toolic (Chancellor) on Dec 22, 2010 at 13:36 UTC
    I believe your problem is related to variable scoping. Try moving all your variables into your sub:
    sub walk { my ($file, $prefix);

    Unrelated, but you probably want to escape the . in your regex because it has special meaning. Also, there is usually no need to quote a scalar variable:

    if ("$file"=~m/.nsf$/){

    should probably be:

    if ($file =~ m/\.nsf$/) {
Re: Searching a file system
by eff_i_g (Curate) on Dec 22, 2010 at 15:10 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2014-09-16 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (158 votes), past polls