in reply to Processing data in on dir and copying, then processing into another dir

Here is one way, by using grep on your dir and file lists. It's a start that should show the way. The logic is greatly simplified by separating the subdir and file search. I'm sure this code can be condensed, but your list of requirements is so big, you are better off breaking it into small steps to aid in debugging.
#!/usr/bin/perl use warnings; use strict; # goes from current dir, and looks thru # a subdir named 7, for subdirs starting with # Jan_2009_, then goes into that subdir and finds # files begiining with a or b or c # making your particular regexes is up to you my $topdir = '7'; my @subdirs = get_sub_dirs($topdir); sub get_sub_dirs { my $dir = shift; opendir my $dh, $dir or die "Error: $!"; my @dirs = readdir $dh; @dirs = grep /^Jan_2009_(.*)$/,@dirs; closedir $dh; return @dirs; } print "@subdirs\n"; my @files; foreach my $dir(@subdirs){ opendir my $dh, "$topdir/$dir" or die "Error: $!"; @files = readdir $dh; @files = grep /^(a|b|c)(.*)$/,@files; closedir $dh; } print "@files\n"; #Now you can regex your files, etc

I'm not really a human, but I play one on earth Remember How Lucky You Are
  • Comment on Re: Processing data in on dir and copying, then processing into another dir
  • Download Code

Replies are listed 'Best First'.
Re^2: Processing data in on dir and copying, then processing into another dir
by lomSpace (Scribe) on Jan 08, 2009 at 19:48 UTC
    I want to opendir "../Data/Sequencing_Results/Results 2009" for reading, then check subdirs "../Data/Sequencing_Results/Results 2009/*". Ex. "../Results 2009/Jan 2009". So is $topdir = "../Data/Sequencing_Results/Results 2009" ?
      If you want to open dir "../Data/Sequencing_Results/Results 2009", it means you are jumping up one directory. You should substitute your dirs in the example I gave, and see what you get. Using relative pathnames can always be tricky, so if in doubt, use the full system path, then you don't need to worry about the dot's
      #!/usr/bin/perl use File::Spec $filename= $0; $abs_path = File::Spec->rel2abs($filename); print "$abs_path\n"; $filename = File::Spec->abs2rel($abs_path); print "$filename\n";
      So in my example, say you were in a dir named 6, your topdir would be "../7" , as you suggest. But setup a test script that just does printouts with no copying, and see how your syntax works. Usually it's NOT a good idea to jump up dirs with .., so I would go with full path names and that will solve your problem.

      I'm not really a human, but I play one on earth Remember How Lucky You Are
        I agree about the relative pathnames and only used them for the example. I am using the full pathnames. I am currently setting up testdir1, testdir2 and cp actual data into them so that I can test the script.
Re^2: Processing data in on dir and copying, then processing into another dir
by lomSpace (Scribe) on Jan 09, 2009 at 20:03 UTC
    ok so I have just added system("mv @files $newdir"); but nothing is being mv to $newdir?
      mv won't work on an array like that, you need (untested and havn't checked if you $newdir is an absolute path or not)
      foreach my $file(@files){ system( "mv $file $newdir/$file"); }
      and you might want to check to see if the newfiles were actually moved, by looping thru @files again, and see if they are in $newdir.

      I'm not really a human, but I play one on earth Remember How Lucky You Are
        Hi Zentara, I am using absolute paths. I have set up to testdir for testing this script. When I run the script the stdout is: GLOB(0x804cbdc) . .. RESULTS 2008 RESULTS 2009 RESULTS 2007 The glob should be the files that match from grep, but I nothing is being moved from 'testdir1' to 'testdir2'.
        #!/usr/bin/perl use warnings; use strict; # goes from current dir, and looks thru # a subdir named 7, for subdirs starting with # Jan_2009_, then goes into that subdir and finds # files begiining with a or b or c # making your particular regexes is up to you #opendir ( my $rs, "tarfs-orion/orion/Data/Sequencing_Results/RESULTS +2009"); my $topdir = "/home/mgavi/testdir1"; my @subdirs = get_sub_dirs($topdir); my $newdir = "/home/mgavi/testdir2"; sub get_sub_dirs { my $dir = shift; opendir my $dh, $dir or die "Error: $!"; print "$dh\n"; my @dirs = readdir $dh; print "@dirs\n"; @dirs = grep /^Results(.*)$/,@dirs; closedir $dh; return @dirs; } print "@subdirs\n"; my @files; foreach my $dir(@subdirs){ opendir my $dh, "$topdir/$dir" or die "Error: $!"; @files = readdir $dh; print "@files\n"; @files = grep /^(\d+\D\d)_(LacZ|pgK|SD|SU)(.*)$/,@files; foreach my $file(@files){ system( "mv $file $newdir/$file"); } closedir $dh; } print "@files\n";