Program to demonstrate how to interigate files without opening them.
The program below will demonstrate how information can be obtained about a file without actually opening the file.
#! /usr/bin/perl -w
# Program used to test files
#
# Written by Sam Watson Jan 13, 2008
#
# Set up the following variables
# $stars Used in the header and footer just to make things look pretty.
#
$stars = "\n*****\n";
# Get the file name
if ($#ARGV == -1) { # Check if the name was supplied
START: # Return point for reentering the file name
print $stars, "Enter the path and file name for the file to be tested\nFile: ";
$file = ;
}
else {$file = $ARGV[0];}
print "\nInformation about $file\n";
# Remove the CR character
chomp $file;
# Check for Symbolic Link
if (-l $file) {
print "This is not a file it is a symbolic link. Try another filename? (y/n) ";
goto ASK
}
# Check for Directory not a file
if (-d $file) {
print "This is not a file it is a directory. Try another filename? (y/n) ";
goto ASK
}
if (-e $file) { # Does the file exist
print "The file was found.\n";
if (-z $file) {print "The file is empty.\n";} # Is it an empty file
else {print "The file is ", (-s $file), " bytes.\n";} # Get the file size
if (-r $file) {print "The file can be read.\n";} # Is the file readable
else {print "The file is not readable.\n"}
if (-w $file) {print "The file can be written.\n";} # Is the file writable
else {print "The file is not writeable.\n";}
print "The file was last modified ", int (-M $file), " days ago.\n"; # Modified ?? days ago
if (-T $file) {print "The file appears to be a text file.\n";} # Is the file a text file
else {print "The file is not a text file.\n";}
if (-B $file) {print "The file appears to be a binary file.\n";} # Is the file binary
else {print "The file is not a binary file.\n";}
}
else {
print $file, "\ncould not be found try another filename? (y/n) ";
ASK:
$yn = ;
chomp $yn;
$yn = lc($yn);
if ($yn eq "y") {goto START;}
}
print $stars;
close (TF);