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
 
OK, here's my meager attempt to fix the problem. The reason it wasn't finding it was because it was looking for the directory PLUS the file. So, I tried this but it didn't work:

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

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

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

print "$file\n";

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

else {
next;
}

}

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

What did I miss?

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Sorry, that's just a habit of mine. In all of my scripts I use a module called strict. It mandates that variables created in one block are not the same in another block of code. That way, I can use $file all over my code and not worry if I need to undefine it first.

If you don't use the strict module, then you don't have to worry about this, however, I suggest using it as it makes your code a lot cleaner.

- Rieekan
 
Code:
$myfile=mtorbin.dat;
opendir DIR "/where/ever";
@files=readdir DIR;
closedir DIR;
foreach (@files) {
  if (lc($_)) eq lc($myfile)) {
    print "Found it";
  }
}
In Perl there is always more than one way to do it

--Paul
 
OK, I just modified my code to try what Paul had posted and I got an error. Here is the code:

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

$myfile=mtorbin.dat;
opendir DIR "/var/@files=readdir DIR;
closedir DIR;

foreach (@files) {
if (lc($_)) eq lc($myfile)) {
print "Found it";
}
}

I'm still waking up this morning, so if I missed something obvious, I'm sorry about that. The last part of this that I want to add is that if the file is found go to, for the sake of arguing, pageA.html. If the the file is not found, go to pageB.html.

Thanks for the help.

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
LOl... I guess it would. Sorry about that. I was getting a 500 error which could be anything. Unfortunately, I don't have access to my error logs right now so I have to do this blindly.

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
put this line in your code at the top.

It's not faultless, but it might help

Code:
use CGI::Carp (fatalsToBrowser);

BTW, why the Taint flag?
-wT might be an idea to get the script running before putting in the T flag
--Paul
 
#!/usr/bin/perl

use warnings;
use strict;

use CGI qw/:standard/;

print header, start_html("title");

my $directory = "/abs/olute/path/"; # or whatever

my $filename = "image.gif"; # or whatever

if (-e $directory$filename) # or is it -f?
{
# do this
}

print end_html;
exit;
 
Thanks all...

I've found that -wT makes a lot of my scripts run smoother. I don't know why, but it just seems to work.

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Ok... I gave SpyderMonkey's code a run and nothing... gave me an error 500. Wow, this is turning out to be much harder than I thought.

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Did you try the CGI::Carp (fatalsToBrowser); line?
That might give you more information on the error, if you don't have access to the logs.

BTW-What web host are you with if you don't have access to your logs?

--Paul
 
OK, I'm going to try and contain some of this because it seems to have taken a life of its own.

First, Paul's Question: - OK, so you called my bluff. I ... um... forgot where they were and the sysAdmin wasn't here. But I got that fixed today.

[embarrassment fades...]

Second, I think we might have gotten off track here because I have three different solutions and I'm not sure which one will work the best for me. I think we need to combine them into one easy simple solution.

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Spyder's solution is best because you know the location, and name of the file.

Rieekan's solution and mine both require all the relevant files in the directory, so are overkill.Spyder's simply checks to see if the file exists ina given location.

Back to error messages, Matt, we're trying to help, but without a specific error message that's not going to be easy...

--Paul
 
Well, now that I know where those error logs are, that shouldn't be a problem.

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Wow, this was the first time I've been praised for an answer-- that's pretty awesome :) Thanks. I had to do this a number of times, just curious. Why is "unlink" the command to delete a file? I always wondered that because "delete "$file";" or "rem "$file"; seems to be a better choice of words.
 
unlink comes from the Unix - it's probably got some really obscure relevance like most *nix commands

allegedly cat which is the *nix command to type a file was named after a developer's dog

It's a language, not english, after all

Praise where priase is due, Spyder, have a star

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top