perl script to list size and modified name of files in folder

Hello. I have a folder with about 3600 files in it. 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

I would like to get a csv file which consists of the modified names of the files and their file sizes (either mb or kb is ok). By modified name, I just want the year, month and day so that for example
11-05-26_08-00-01_sina.csv  
would become
11-05-26
and if this file was 25.2 mb the entry for this file would be
11-05-26, 25.2
Can anyone help me with this?
THANK YOU.
onyourmarkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddaCommented:
What is there are several files for single day, like in your example? 11-05-26 has 4 files.
0
onyourmarkAuthor Commented:
Hi. Thanks for the reply!

Even if there are several files a day there can be a separate row for each one.

So it could look like

11-05-26, 25.2
11-05-26, 30.2
11-05-26, 20.8
11-05-26, 35.5

or something like that.

Thanks again.

0
farzanjCommented:
How about something like this
use warnings;
use strict;

my @files = <*csv>;
foreach my $file (@files)
{
    my $size = (stat($file))[7];
    $file    =~ s/_.*//;
    print '"', join('","', ($file, $size )) , '"', "\n";
}

Open in new window

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

onyourmarkAuthor Commented:
Hi. Thanks. To try this I should make it a file called something like sizes.pl and put it in the same directory as the files and then run it like
cd C:/appropriate directory
perl sizes.pl

Is this correct?

Thanks.
0
farzanjCommented:
Yes.  Or if you want to run it from a different directory, you will have to change <*csv> part.
0
ozoCommented:
print /([^_]*)/,", ",(-s)/1024,"\n" for <*.csv>;
0
onyourmarkAuthor Commented:
how can I have the result written to a file called sizes.csv?
Thanks
0
farzanjCommented:
You can redirect the output by using >

script.pl > sizes.csv
0
onyourmarkAuthor Commented:
Hi. Thanks again.
I think I may have specified this incorrectly.

A file like 11-05-26_08-00-01_sina.csv
would be 2011/5/26

but it is reading it as  11/5/2026.

is this hard to rearrange?
0
ozoCommented:
open STDOUT,">sizes.csv" or die $!;
printf "20%02d/%d/%d, %.3f\n",/(\d+)-(\d+)-(\d+)/,(-s)/1000 for <*.csv>;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
onyourmarkAuthor Commented:
THANKS!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.