Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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 ".."; } }

Replies are listed 'Best First'.
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.


Re: Searching a file system
by toolic (Bishop) 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
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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://878433]
Approved by sflitman
[Corion]: Also, a good application to test my API to rate limit things
[choroba]: Dangers of WFH: a mailman rings, brings a parcel for my wife, I need to go outside, take my son with me, sign a paper *and* into the postman's mobile app, get back. Guess what the soup was doing meanwhiles?

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2017-10-18 11:32 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (244 votes). Check out past polls.