Solved

read file names in a folder of files in order

Posted on 2011-09-08
11
244 Views
Last Modified: 2012-06-27
Can anyone give me a perl script to check to see which files are missing from a folder? The folder is located in H:\Renamed

The files have names like
11-05-26_08-00-01_sina.csv
11-05-26_12-00-01_sina.csv
11-05-26_16-00-01_sina.csv
11-05-26_20-00-01_sina.csv
11-05-27_00-00-02_sina.csv
11-05-27_04-00-01_sina.csv

and so on. The first number is the year, second is the month, third the day, fourth the hour. I am not sure what the fifth number is but I think it can be ignored.
I would like to know which files are missing in the sequence. So if I specify the first date, with for example, 09-10-31_00-00-01_sina.csv
and then the last file with say for example,
11-07-19_16-00-01_sina.csv,
then I would like to I would like to know which if any are missing. For example maybe there is no
11-04-06_00-00-01_sina.csv
or no
11-04-06_20-00-01_sina.csv.

Thank you.
0
Comment
Question by:onyourmark
  • 7
  • 3
11 Comments
 
LVL 9

Expert Comment

by:parparov
ID: 36506682
#!/usr/bin/perl

use strict;
use warnings;

use POSIX qw(mktime strftime);
	
my $dir = shift || die "Usage: $0 directory start_file end_file";
my $start_file = shift || die "Usage: $0 directory start_file end_file";
my $end_file = shift || die "Usage: $0 directory start_file end_file";
die "Filename must be of format YY-MM-DD_HH-NN-NN_sina.csv"
	unless $start_file =~ /^(\d\d)\-(\d\d)\-(\d\d)_(\d\d)\-\d\d\-\d\d_sina\.csv/;
my ($sy, $sm, $sd, $sh) = ($1, $2, $3, $4);
$sy += 100 if $sy < 20; $sm--;
die "Filename must be of format YY-MM-DD_HH-NN-NN_sina.csv"
	unless $end_file =~ /^(\d\d)\-(\d\d)\-(\d\d)_(\d\d)\-\d\d\-\d\d_sina\.csv/;
my ($ey, $em, $ed, $eh) = ($1, $2, $3, $4);
$ey += 100 if $ey < 20; $em--;
my $step = 4 * 60 * 60; # 4 hours

my $start_time =  mktime(0, 0, $sh, $sd, $sm, $sy, 0, 0);
my $end_time = mktime(0, 0, $eh, $ed, $em, $ey, 0, 0);

chdir $dir or die "Dir $dir is unaccessible: $!\n";
for (my $t = $start_time; $t <= $end_time; $t += $step) {
	my $file_name_pattern = strftime("%y-%m-%d_%H-*-*_sina.csv", localtime($t));
	print "File matching with $file_name_pattern is missing\n" unless @matched;
}

Open in new window

0
 

Author Comment

by:onyourmark
ID: 36509132
Thanks very much


I got this message:

C:\Users\Bill\Desktop>perl anyMissing.pl
Global symbol "@matched" requires explicit package name at anyMissing.pl line 27.
Execution of anyMissing.pl aborted due to compilation errors.


also
Can I ask
for these 3 lines
my $dir = shift || die "Usage: $0 directory start_file end_file";
my $start_file = shift || die "Usage: $0 directory start_file end_file";
my $end_file = shift || die "Usage: $0 directory start_file end_file";

do I hard code in the directory, the start file and end file or does it take these as arguments at the prompt?
Thanks again.
0
 
LVL 9

Accepted Solution

by:
parparov earned 500 total points
ID: 36511233
Correction - an important line got wiped during a cleanup, sorry.
The dir, start and end are arguments at the prompt.

#!/usr/bin/perl

use strict;
use warnings;

use POSIX qw(mktime strftime);
        
my $dir = shift || die "Usage: $0 directory start_file end_file";
my $start_file = shift || die "Usage: $0 directory start_file end_file";
my $end_file = shift || die "Usage: $0 directory start_file end_file";
die "Filename must be of format YY-MM-DD_HH-NN-NN_sina.csv"
        unless $start_file =~ /^(\d\d)\-(\d\d)\-(\d\d)_(\d\d)\-\d\d\-\d\d_sina\.csv/;
my ($sy, $sm, $sd, $sh) = ($1, $2, $3, $4);
$sy += 100 if $sy < 20; $sm--;
die "Filename must be of format YY-MM-DD_HH-NN-NN_sina.csv"
        unless $end_file =~ /^(\d\d)\-(\d\d)\-(\d\d)_(\d\d)\-\d\d\-\d\d_sina\.csv/;
my ($ey, $em, $ed, $eh) = ($1, $2, $3, $4);
$ey += 100 if $ey < 20; $em--;
my $step = 4 * 60 * 60; # 4 hours

my $start_time =  mktime(0, 0, $sh, $sd, $sm, $sy, 0, 0);
my $end_time = mktime(0, 0, $eh, $ed, $em, $ey, 0, 0);

chdir $dir or die "Dir $dir is unaccessible: $!\n";
for (my $t = $start_time; $t <= $end_time; $t += $step) {
        my $file_name_pattern = strftime("%y-%m-%d_%H-*-*_sina.csv", localtime($t));
	my @matched = glob($file_name_pattern);
        print "File matching with $file_name_pattern is missing\n" unless @matched;
}

Open in new window

0
 

Author Comment

by:onyourmark
ID: 36514785
Thanks very much again. I tried it but I get this

C:\Users\Bill\Desktop>perl anyMissing.pl
Usage: anyMissing.pl directory start_file end_file at anyMissing.pl line 8.
0
 

Author Comment

by:onyourmark
ID: 36526738
Duh, I asked you about how to use it and then I did not even try it that way. Sorry. Actually I am not that used to how to run the files. Thanks for the code. It works perfectly!!!!!!!!!!!!!!!!!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:onyourmark
ID: 36527317
I've requested that this question be closed as follows:

Accepted answer: 0 points for onyourmark's comment http:/Q_27297535.html#36514785

for the following reason:

Duh, I asked you about how to use it and then I did not even try it that way. Sorry. Actually I am not that used to how to run the files. Thanks for the code. It works perfectly!!!!!!!!!!!!!!!!!<wbr />
0
 
LVL 9

Expert Comment

by:parparov
ID: 36526782
Would you mind assigning the points when closing?
0
 

Author Comment

by:onyourmark
ID: 36527313
No sure what is going on. I want to give all the points to parparov. Thank youl
0
 

Author Comment

by:onyourmark
ID: 36527318
No sure what is going on. I want to give all the points to parparov. Thank you.
0
 

Author Comment

by:onyourmark
ID: 36529269
Hello. I hope that parparov has been awarded full points for this.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now