in reply to Re^2: Open a folder
in thread Open a folder
Below code process each files in the /tmp directory. If any directory is inside the /tmp directory will not be processed
Add additional checking on file, like file size and readable permissions to avoid error.
#!/usr/bin/perl use strict; use warnings; my $tmp_dir="/tmp/"; opendir (DIR, $tmp_dir) or die $!; while (my $file_name = readdir(DIR)) { my $abs_path = $tmp_dir.$file_name; unless(-d $abs_path) #Ignore if it is a directory { &Process_EachFile($abs_path); # Do call a function and process + each file } else { print "$abs_path is a directory\n"; } } closedir(DIR); sub Process_EachFile { my ($filename) = @_; print $filename; open (FH,'<',"$filename") or die $!; while(my $each_line=<FH>) { print $each_line."\n"; #Read file content here and do your calculation. } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Open a folder
by Dr Manhattan (Beadle) on Jan 09, 2013 at 12:04 UTC | |
by vinoth.ree (Monsignor) on Jan 09, 2013 at 13:24 UTC |