[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

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";
}        
0
georgia
Asked:
georgia
  • 2
1 Solution
 
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
 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now