Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

read file names in a folder of files in order

Posted on 2011-09-08
11
Medium Priority
?
288 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
10 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans
Suggested Courses

564 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