in reply to Re: Convert date into day and month
in thread Convert date into day and month

Thank you everyone, I got so many good replied. The Date.csv file is pasted below. What am I trying to achieve. I need to read just the first line, actually just the date and find out the date, month, and then year. Then I need to create the folder using something like:  system("md $Month$Day$Year"); I need to read the file just for reading the first line, get the day, month, and year and then create a folder
Oct 31 08:30,/home/imanager/cppprod/AR_LM.csv Oct 31 07:32,/home/imanager/cppprod/AddressDevCHUBldgProj.csv Oct 31 08:06,/home/imanager/cppprod/AddressDevCHUBldgProj_LM.csv Oct 31 07:44,/home/imanager/cppprod/BalDue.csv Oct 31 08:07,/home/imanager/cppprod/BalDue_LM.csv Oct 31 07:50,/home/imanager/cppprod/Charges.csv Oct 31 08:10,/home/imanager/cppprod/Charges_LM.csv Oct 31 08:45,/home/imanager/cppprod/Dem_A_C.csv Oct 31 07:51,/home/imanager/cppprod/MonthlyEviction.csv Oct 31 08:11,/home/imanager/cppprod/MonthlyEviction_LM.csv

Replies are listed 'Best First'.
Re^3: Convert date into day and month
by Kenosis (Priest) on Nov 01, 2013 at 16:48 UTC

    Using kcott's Time::Piece built-in module suggestion, consider the following:

    use strict; use warnings; use Time::Piece; <> =~ /([^,]+)/ and my $dir = Time::Piece->strptime( "$1 2013", '%b %d %H:%M %Y' )-> +mdy('') or die "Unable to capture date string."; if ( !-e $dir ) { mkdir $dir or die "Unable to create directory $dir: $!"; print "Created dir: $dir\n"; } else { print "Directory $dir already exists.\n"; }

    Usage: perl inFile

    Since the date doesn't contain the year, that's hard coded. The in-line <> notation (short for <ARGV>) gets the first line of the file sent to the script. Next, a regex is used to capture that first line's date information (held in $1), and the date/year information is used by Time::Piece to return a 'mmddyyyy' string for the directory name. Finally, mkdir is used to create the directory if it doesn't already exist ( !-e $dir ).

    Hope this helps!