lomSpace has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use strict; use DateTime; use File::Copy; use File::Find; use File::stat; use Time::localtime; use Data::Dumper; use File::Glob ':glob'; use Log::Log4perl qw(get_logger :levels); Log::Log4perl->init("find_file_log.conf"); our $LOGGER = Log::Log4perl->get_logger(); $LOGGER->info("my app started"); 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: \"\\\\tarfs-orion\\Data\\Sequencing_Results +\\RESULTS $year\\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 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 customers folder my %sequser = {Abigail McPherson => AM, Ana Kostic => AK, Anna Zengin +=> AZ, Anu Jain => AJ, Ashwin Ricci=> AR, Brinda Prasad=>BP, Carlos Arrecubieta=> CA, Catherine +Walsh=> CW, Charleen Hunt=> CH, Claudia Canasto=> CC, Daniel Brims => DBr, Darya Burakov=> DBu, David Heslin + => DH, Dawn Thurston => DT, Dipali Nimbalkar => DN, Erin Allison => EA, Evana Sirabian => ES, Evangelos Pe +fanis => EP, Evin Feldman => EF, Frank Delfino => FD, Gleb Sagitov => GS, Gregg Warshaw => GW, Hans Gartner +=> HG, Inessa Rivkis => IR, Jamie Orengo => JO, Jason Pitts => JP, Jason Yasenchak => JY, Jaya Ahuja = +> JA, Jean Yanolatos => JYa, Jee Kim => JK, Jennifer Griffiths => JG, Jie Cao => JC, Jody Ann Well +er => JAW, Joelle Carlo => JoC, John McWhirter => JMcW, Joseph Quispe => JQ, Joyce McClain => JMcC, Julie Ritc +he => JR, Kara Olson => KA, Katie Yang => KY, Kieran Feeley => KF, Kihwa Kang => KK, Kristen Bloom = +> KB, Leela Raj => LR, Maggie Zhong => MZ, Mansi Parikh => MPa, Marc Morra => MM, Matthew Pendlet +on => MPe, Michel Naum => MN, Min Gao => MG, Nina Schilt => NS, Peter Gasparini => PG, Peter Lengye +l => PL, Qin Liu => QL, Richard Corpina => RC, Richard Welsh = > RW, Rishi Mahajan => RM, Robert Babb + => RB, Ron Deckelbaum => RD, Roxanne Ally => RA, Sarah Hatsell => SH, Sharon Okenquist => SO, Steven Ir +vin => SI, Tara Young => TY, Uzodinma Uche => UU, Vera Voronina => VV, Vincent Idone => VI, Yang Wei => +YW, Yu Zhao => YZ}; 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","$user") 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: Using an hash ref to match files with directories
by NetWallah (Canon) on Aug 08, 2009 at 00:03 UTC | |
by lomSpace (Scribe) on Aug 10, 2009 at 18:43 UTC | |
by NetWallah (Canon) on Aug 11, 2009 at 05:53 UTC | |
by lomSpace (Scribe) on Aug 11, 2009 at 21:09 UTC | |
by NetWallah (Canon) on Aug 12, 2009 at 06:09 UTC | |
| |
by ww (Archbishop) on Aug 12, 2009 at 06:51 UTC | |
|