in reply to Re^3: Using an hash ref to match files with directories
in thread Using an hash ref to match files with directories
#!/usr/bin/perl -w use strict; use DateTime; use File::Copy; use File::Find; use File::stat; use Time::localtime; use Data::Dumper; use File::Glob ':glob'; my $now = DateTime->now(time_zone => 'floating'); my $yesterday = $now->subtract(days=>1); my $year = $yesterday->year; my $month = $yesterday->month; my $month_abbr = uc($yesterday->month_abbr);# uppercase string chars my $day = $yesterday->day; my $year_abbr = substr "$year",2,2; # location of files to be processed. my $root="C:\\ab3730\\SeqData\\RUN$month/$day/$year_abbr"; print $root,"\n"; # location of seq customer directories #map drive for RESULTS directory. Unable to reference this directory d +irectly. system ("net use i: /delete"); my $err=system("net use i: \"\\\\Data\\Sequencing_Results\\RESULTS $ye +ar\\New Version $year\""); if ($err!=0){ $LOGGER->logdie("unable to map RESULTS directory on orion."); } # This hash reference holds the names of the sequencing core cust +omers. # The values should match the initials of the user at the # beginning of the sequence file. After the match is made then # the file/s are copied to the users folder my %sequser = ("MJ"=>"Michael Jordan", "LJ"=>"Lebron James", "KB"=>"Ko +be Bryant"); my $seq_user_dirs ="i:"; my %files=(); find(\&wanted, $root); #check the arrays in the hash looking for seqs with atleast a file foreach my $k (keys(%files)){ $LOGGER->debug("working on $k"); #get the array from the array reference. my @arr=@{$files{$k}}; #skip unless we have atleast 1 file in the array + next unless scalar @arr >= 1; my $seq_user_base = seq_customer_dirs($k); if (!defined $seq_user_base || $seq_user_base eq ''){ $LOGGER->error("Unable to find seq user dir for: $k"); next; } my $results_dir=$seq_user_base."\\"."Results_".$month."-".$day."-".$ +year; $LOGGER->debug("Creating Results dir: $results_dir"); mkdir $results_dir, 754; #now iterate through the array, copying the files to the folder base +d on # the name foreach my $f (@arr){ copy("$f","$results_dir") or $LOGGER->info("unable to copy $f"); $LOGGER->info("copying $f"); } ################################ Subroutines ######################## +######## sub wanted { # This method traverses $root and locates the sequence files # It processes them based on date modified, in our case less than a da +y my $targetfile = $File::Find::name; # Here we locate the files to + be copied return if (-M $targetfile > 1.0); # return the files that are +less than a day old my ($seq) = $targetfile=~ /.*\/(\D+)_.*/; # capture the initials f +rom the $targetfile my $seq = $seq; # capture the intials ($seq) from $targetfile # create a hash of $targetfile keys and the captured $seq values + push @{$files{$seq}}, $targetfile ; } sub seq_customer_dirs{ # Here we look to match initials with seq customer directories. Key va +lue pairs are set up in # a hash so that the initials can match the value and then copy to the + correct directory based # on the key. If we find a match then we return it to the main program +ming for processing. my $seq=shift; my @dirs = glob($seq_user_dirs."\\*"); foreach my $dir(@dirs) { if (-d $dir){ if ($dir=~m%$seq%){ return $dir; } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Using an hash ref to match files with directories
by NetWallah (Canon) on Aug 12, 2009 at 06:09 UTC | |
by lomSpace (Scribe) on Aug 13, 2009 at 16:22 UTC | |
|
Re^5: Using an hash ref to match files with directories
by ww (Archbishop) on Aug 12, 2009 at 06:51 UTC | |
by lomSpace (Scribe) on Aug 13, 2009 at 16:25 UTC |