Check for Existing File in Perl
To see
if a file already exists:
if (-e "../cities.txt") {
print "File exists!";
}
else {
print "File does not exist.";
}
-We use
-e to check for the file.
-We enclose the path to the file in quotation marks. In this instance, one directory up is where we want to look for our file
cities.txt so we use ../
-We print the results of our checking.
Copy and Paste Perl Code:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
if (-e "../cities.txt") {
print "File exists!";
}
else {
print "File does not exist.";
}
exit;
Resources:
To See if a Directory Exists in Perl
If, Elsif, and Else in Perl
Click HERE to comment or discuss at iLoveTheCode GOOGLE Group