Solved

read file names in a folder of files in order

Posted on 2011-09-08
11
260 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTP Error 502.2 - Bad Gateway 3 222
transpose into pipe delemited 8 72
get the last nth lines from a an array if nth element is xxx 2 74
Call Shell Script from Perl Script 6 133
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

840 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