Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Checking for Files

by akrrs7 (Acolyte)
on Nov 18, 2011 at 13:37 UTC ( [id://938819]=perlquestion: print w/replies, xml ) Need Help??

akrrs7 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a text file which has a list of files. I need to check a certain path to see if each of these files exist and if they do, then get the filesize and time and print these parameters into a new text file. This is my code:
my $thisPath = cwd; my $inputFile = "${thisPath}${ps}inputFileInfo.txt"; my $pathToCheck = "c:${ps}temp"; my $outputFile = "${thisPath}${ps}resultsFile_$timestamp.txt"; open theInputFile, $inputFile or die "Could not read from $inputFi +le, program halting. \n"; while (<theInputFile>) { chomp; my $fileBeingChecked = "${pathToCheck}${ps}$_"; open (theOutputFile, '>', $outputFile) or die "Could not open +$outputFile. \n"; if (-e $fileBeingChecked) { my $fileSize = -s $fileBeingChecked; my $date_string = ctime(stat($fileBeingChecked)->mtime); print theOutputFile "Yes, $fileSize, $date_string \n"; } else { print theOutputFile "No \n"; } } close (theOutputFile) close (theInputFile)
Only getting a single line output in the OutputFile and the results - size of file and date do not match Thanks...

Replies are listed 'Best First'.
Re: Checking for Files
by RichardK (Parson) on Nov 18, 2011 at 13:47 UTC

    You haven't opened the output for write.

    it's clearer to use the 3 parameter version of open like this :-

    open ( my $fh,'>',$filename) or die "$filename : $!";

    Check out the help for open

      Hello Richard, That works - thanks... However, I am only getting the results for the first line in my input file. Thanks.

        You're opening your output file inside your while loop, so it's getting closed and then wiped out and reopened for each line of your input file. So you're actually getting the size and date of the last file listed in your input file, not the first. Open your output file outside your while loop, as you do with your input file, and that should fix that.

        Aaron B.
        My Woefully Neglected Blog, where I occasionally mention Perl.

        Also - the values that I am getting for filesize and date modified do not match
Re: Checking for Files
by graff (Chancellor) on Nov 19, 2011 at 04:00 UTC
    The problem that aaron_baugher pointed out is the main thing, but once you get past that, the script will take somewhat longer to run than it needs to, because you're calling the "stat" function multiple times on each file. I'd do it like this (not tested):
    use strict; use warnings; use POSIX; use File::Spec; my $inputFile = "inputFileInfo.txt"; my $outputFile = strftime( "resultsFile_%Y-%m-%d_%H-%M-%S.txt", localt +ime(time())); my $pathToCheck = File::Spec->catfile( 'c:', 'temp' ); open( IN, '<', $inputFile ) or die "$inputFile: $!\n"; open( OUT, '>', $outputFile ) or die "$outputFile: $!\n"; while ( <IN> ) { chomp; my $checkFile = File::Spec->catfile( $pathToCheck, $_ ); my @stats = stat $checkFile; if ( @stats ) { printf OUT ( "%s: %d, %s\n", $checkFile, $stats[7], scalar( localtime( $stats[9] ))); } else { print OUT "$checkFile: stat failed (file not readable or not f +ound)\n"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://938819]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found