Test to see if a file exists or is empty

The code below always tells me the file does not exists... I can't seem to tell what is wrong?  


if (-s $DirPath.$FileName_loc){
open(INFILE, $DirPath.$FileName_loc);
print "File $DirPath.$FileName_loc exists.\n";
}
  else
{
print "File $DirPath$FileName_loc doesn't exist or empty\n";
}        
georgiaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guadalupeCommented:
Place the dir/name in quotes line this:

if (-s "$DirPath$FileName_loc")

or this:

if (-s "$DirPath/$FileName_loc")

depending on if the variables end/start with a slash or not...

If your on Unix most OSs will let you put in an extra / (ex:  /usr//bin) and not complain... windows is not so forgiving...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
"$DirPath$FileName_loc"
is the same as
$DirPath.$FileName_loc
Does your print statement report the correct filename?

You could also check the open:
open(INFILE, "<$DirPath/$FileName_loc" ) or die "can't open $DirPath/$FileName_loc because $!";
to return an error message if the file does not exist.
0
guadalupeCommented:
ozo while your right about the twqo forms of concatenation I have found that these file operators seem to want quotes and no bare path/filenames.  At least thats what I've found...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.