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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching For a File

Status
Not open for further replies.

mtorbin

Technical User
Nov 5, 2002
369
US
Hey all,

Ok, I promise I'll post whatever code I finish since you all helped me write it. The piece that I'm now working on is this:

I need to have a perl script search a very specific directory for a file and return whether or not it finds it.

Well, where do I begin?

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
I'm going to probably give you more than you want, but here's a little sub-routine that I wrote to recurse through a directory tree.

Code:
sub recursetree
{
    foreach (glob "$_[0]/*")
    {
        # recurse into subdirectories
		-d && recursetree($_) unless -l;
	# Do something
    }
}

recursetree('d:/mydir');

- Rieekan
 
Rieekan,

Thanks. Um... could you explain it to me. I'm looking at it and I don't quite get it all.

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Sure. I'll add some more comments in to help clear it up.

Code:
sub recursetree
{
	my $directory = $_[0];
	#for everything returned from globbing the directory
	foreach my $file (glob "$directory/*")
	{
		# recurse into subdirectories if the file is a directory and not . or ..
		-d && recursetree($file) unless -l;
		# Add your code here for what you're looking for.
	}
}

# start the sub-routine.  You need to feed it the directory you want to check
recursetree('d:/mydir');
 
You're right... I think that's a bit more than I needed because I don't even know how to apply it. I'm not a complete perl novice, but I'm not an expert either.

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
It's really just a cut and paste job, adding in your code to find the exact file you're looking for. Then, every time you want to find a file in that script, just call the sub-routine.

- Rieekan
 
I'll probably need something very basic, as in:

1) Go to this directory
2) Look for this file
3) IF it exists... DO THING 1
IF it exists... DO THING 2

The file name will be coming into the code via a variable name. Is t here an easier way to write that? I just looked up glob and I think it's way more than I need, or is it?

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Try this then:

Code:
my $directory = 'd:/mydir';
my $my_file = 'file.ext';

    foreach my $file (glob "$directory/*")
    {
       if ($file eq $my_file)
       {
            #do something
        }
    }

- Rieekan
 
There we go. Thanks! That is so much better because I can understand it. It does the same thing, right?

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Yes, you just skip the ability to look in sub-directories for the file.

i.e. - This will only look in c:\windows and not in c:\windows\system32

- Rieekan
 
That's fine because it only has to look in the one directory.

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Morning...

OK, so this is what I have so far:

#!/usr/bin/perl -wT
print "Content-type:text/html\n\n";

my $directory = '/var/my $my_file = 'mtorbin.dat';

foreach my $file (glob "$directory/*") {

if ($file eq $my_file) {
#do something
}
}

For right now, just to make sure that the code is working properly, I want the "do something" to go to a page called "answer.html". However, I can't find anything about a navigator object in Perl. What do I put there?

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
You'll need to send a redirect statement to the browser to accomplish what you're looking for. This can be accomplished in a few ways, the easiest by using the CGI Module and just passing a print redirect command to it.

- Rieekan
 
OK, so this is what I have so far. But I keep getting the "else" option. Is the code written right or am I doing something wrong? I know the file is there.

#!/usr/bin/perl -wT
print "Content-type:text/html\n\n";

my $directory = '/var/my $my_file = 'mtorbin.dat';

foreach my $file (glob "$directory/*") {

if ($file eq $my_file) {
print "I found it";
}

else {
print "no chance pal!";
}

}

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Well, the code is written correctly, but for every file that is not mtorbin.dat, you're going to get the else statement. For example, if the directory has the following files:

.
..
file1.dat
file2.dat
file3.dat
file4.dat
file5.dat
file6.dat
file7.dat
mtorbin.dat
xfile8.dat
xfile9.dat
xfile10.dat
xfile11.dat
xfile12.dat

You're going to get the results 'no chance pal' 12 times; 9 times before your file and 5 times after, with your results in between.

Best bet is that if you don't care about the files that don't match, just change the else statement to the following:

Code:
else
{
    next;
}

- Rieekan
 
OK, odd thing is happening. Here is what I currently have:

#!/usr/bin/perl -wT
print "Content-type:text/html\n\n";

my $directory = '/var/my $my_file = 'mtorbin.dat';

foreach my $file (glob "$directory/*") {

if ($file eq $my_file) {
print "I found it";
}

else {
next;
}

}

print "OK, nothing happened. WHAT IS UP WITH THAT?";


But it seems like nothing is being found. I double-checked the spelling on the file and the location. What gives?

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
I'd put in some debug code in there to see what you do get. A simple print "$file\n"; would work sufficiently for this purpose I think.

- Rieekan
 
Rieekan,

I'll do that but before I continue, I just want to thank you for giving me your help. It really does mean a lot.

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
That's no problem. Let me know if you run into any snags!

- Rieekan
 
What is the my part? Is that like the "this." in JavaScript? Is it needed to declare a variable?

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top