in reply to How do I search for a specific directory on a remote PC?

i've gotten some mileage from this code at nt shops in the past. you need to be careful to use the proper case in file/directory names, as 'Blah' and 'blah' are different in find, even though NT doesn't differentiate.

#!/usr/bin/perl use strict; use warnings; # use -w instead for perl < 5.6.0 use File::Find; # the dir you're 'find'ing from my $dir = shift || '.'; # the file you're looking for my $file = 'blah'; # the results you want my @results; # the function you perform on each find value sub looking_for_blah { # return the full path name if the filename is blah push(@results, $File::Find::name) if(-f $_ && $_ eq $file); }; # the result you want, using File::Find::find() # (not the external 'find' program) find \&looking_for_blah, $dir; # print your results print("my results are: @results\n");

~Particle

Replies are listed 'Best First'.
Re: How do I search for a specific directory on a remote PC?
by Anonymous Monk on Jan 07, 2002 at 09:30 UTC

    If you find case sensitivity to be a problem (and on Win*, it doesn't make sense to not do this) then you may want to change this line:

    push(@results, $File::Find::name) if(-f $_ && $_ eq $file);

    into:

    push(@results, $File::Find::name) if((-f $_) && ($_ =~ /^$file$/i));

    I prefer extra parens for readability, though they aren't vital

    Bill
      Regexes are overkill for case insensitive comparisons... how about:
      if (lc $_ eq lc $file and -f $_)
      for speed, clarity and correctness... (your regex has issues with special chars and trailing newlines...)

      -Blake