Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File::Find search problem on windows

Status
Not open for further replies.

alex452

Technical User
Joined
Sep 7, 2006
Messages
7
Location
SE
Hi everybody,
i hope somebody can help me with a little perl problem

I wrote a perl windows script based on File::Find
to do a local search. Well it does search ok as long
as the map is not C: I really
fail to understand why since i verified $File::Find::dir
is actually C: so why does it search where all of my scripts are C:\PERL\apache\cgi-bin instead.

Kind regards,
Alexander
 
post your code otherwise we can only guess.
 
ok, its this part that doesnt work the way i want, its basically the thing i took from here and changed slightly. @startpoint and $apoint in this case only have 1 value C: but i wanted to extend it so i can send differnet search locations as startpoints.


while (@startpoints) {
$apoint = shift(@startpoints);

find( sub
{
return if($_ =~ /^\./);
return unless($_ =~ /$pattern/i);
stat $File::Find::name;
return if -d;
print $File::Find::name . "<BR>";

},$apoint);

}
 
Code:
foreach my $apoint (@startpoints) {
        find( sub
        {    
         return if($_ =~ /^\./);
         return unless($_ =~ /$pattern/i);
         stat $File::Find::name;
         return if -d;
         print $File::Find::name . "<BR>";
        
        },$apoint);

}
 
that didnt help, other suggestions?

/Alex
 
as far as I know it should work, if it doesn't, I don't know why. Maybe someone else will know.
 
Kevin:
but you only changed the way the loop itirates,

is it something you use on windows? could you please tell me how you use it and if you are able to search in C: ?

thanks
Alex
 
Yes, I changed the loop because 'foreach' is really the tool for this task. You want to process all the elements of an array, so you use 'foreach'. You could also use 'for'. You can process an arry using 'while' but it is less efficient and might return unexpected results.

I tried the code on windows and it works fine. I had to comment out one line:

return unless($_ =~ /$pattern/i);

becuase I don't know what $pattern is. I also commented out:

stat $File::Find::name;

because your script does nothing with the return value of the stat() function and it will slow the script way down when poking through a large directory like the c:/ drive. So this is the actual code I used to test with:


Code:
#!perl
use strict;
use warnings;
use File::Find;
use CGI;
my $q = CGI->new;
print $q->header,'<plaintext>';

my @startpoints = ('c:/','d:/');
foreach my $apoint (@startpoints) {
        find( sub
        {    
         return if($_ =~ /^\./);
#         return unless($_ =~ /$pattern/i);
#         stat $File::Find::name;
         return if -d;
         print $File::Find::name . "\n";
        
        },$apoint);

}

I tested it as a CGI because I assumed you are doing that too, but if not just remove all the CGI stuff. What is $pattern in your script?
 
Thanks Kevin,

i guess the problem is somethere else, because
i run your script and it runs perfectly for d:
but for c: once again it does the same thing,
it prints all the contents of the folder
C:\PERL\apache\cgi-bin where all my scripts are and
files in 2 maps (backup of old scripts) which are under that map. I run indigoperl which comes with apache
but there really havent been any problems with it
so i doubt the problem is indigoperl-specific.
So my perl distribution lies in C:\PERL\perl
and all scripts lies in C:\PERL\apache\cgi-bin

/AL
 
It might have something to do with whatever version of windows you are running. I tested on an old W98 with apache, and activestate perl.

 
ok, yes i guess it can have to do with windows version,
i have Win XP Professional.
 
can you just do a straight opendir() command on the 'c' drive?

opendir(DIR,'c:/') or die "can't open 'c' drive: $!";
 
Hi,
I testedthe above and i have not got "can't open 'c' drive.." running my testscript so there is nothing wrong with C:

can it be that some kind of environment variable is set differently for indigoperl than other versions of perl?

Alex
 
I guess it's possible but I don't know.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top