FInd文件并使用Perl移动到已命名的目录

时间:2013-04-21 03:15:12

标签: perl file directory rename

我有一个@file_list,其中包含目录中的文件列表。 我有@name_list包含ID列表。 @file_list中列出的每个文件都以@name_list中的ID开头。 在代码中,使用了两个循环。 外循环遍历@file_list,拉出文件名。 该文件名与@name_list中的名称匹配,在内部循环中使用正则表达式。 匹配后,我需要将文件移动到一个新目录,进入一个已经带有名称$ name的文件夹。

FILE: for my $file (@file_list) {

  for my $name (@name_list) {

    if ($file =~ /^\Q$name\E/) {
      rename "/Users/path/to/file/I/need/to/move/$file", "/Users/path/to/directory/i/need/to/move/file/to/$name/$file" or die "rename failed because: $!\n";
     next FILE;
    }
  }
  print "no match for $file\n";
}

如果我理解正确,我不需要在这里使用File :: Copy,因为重命名是核心的一部分。此代码不在正则表达式之外......

@file-list包含以下内容:

1700018L02Rik_k2.5kDown
1700018L02Rik_Prime5UTR
4430402I18Rik_Exons
4430402I18Rik_Intron
4430402I18Rik_k2.5kDown

@name_list包含以下内容:

1700018L02Rik
1700018L02Rik
4430402I18Rik
4430402I18Rik
4430402I18Rik

我已经为@name_list中的每个元素创建并命名了空白目录。

守则:

 #!/usr/bin/perl
use strict; use warnings;


#######
# makeDirectories.pl
#######

# to take the list of total Reference genes and make a directory for each gene name

my $usage = "usage: enter name of file with reference genes inside and the directory with the SNP data\n";
die $usage unless @ARGV == 2;

open( my $FILE, "$ARGV[0]") or die $!;

my @gene_list = (); # to collect ref gene names with blanks removed (do not want to create dirs with blank names)


while (my $line = <$FILE>) 
    {
     chomp $line;
     my @current_line = split( "\t", $line );
     #print "@current_line\n";
     #print scalar @current_line."\n";

     for ( my $i = 0; $i < scalar @current_line ; $i ++ )
         {
          if ( $current_line[ $i ] =~ m/^\s*$/ )
             {
              #print "$current_line[$i] is blank\n"; 
             }

         else 
             {
              push( @gene_list, $current_line[ $i ] );   
             }    

         } #for


    } # while

#print scalar @gene_list."\n";


#####
# now make the gene list only contain unique gene names
#####

my %unique = ();
foreach my $gene_name (@gene_list)
    {
    $unique{$gene_name} ++;
    }
  my @unique_gene_list = keys %unique; # collects list without repeat instances of the same name

#print scalar @unique_gene_list."\n";

print "@unique_gene_list\n";
    open(OUTFILE, ">unique.csv") or die $!;
    my @transposedunique = ();
    for ( my $i = 0; $i < scalar @unique_gene_list ; $i++ )
        {
         push( @transposedunique, "$unique_gene_list[ $i ]\n");   
        }
    print OUTFILE @transposedunique;

     #####
    # now to use unique list of names and make directories with them
     #####

    foreach my $name ( @unique_gene_list )
        {
        #print "$name\n";
        chomp $name; 

     #unless(mkdir $name, 0700) 
         #{
         # die "Unable to create directory called $name\n";
         #}
     }     

     #####
    # now fill directories with each gene's set of SNP files
     #####


    opendir(DIR, $ARGV[1]);                             
    my @file_list = grep ! /^\./, readdir DIR;
    closedir(DIR); 
    #open(OUTFILE2, ">file_list.cvs") or die $!;
    #my @transposed_files = ();
    #for ( my $i = 0; $i < scalar @file_list ; $i++ )
     #   {
      #   push( @transposed_files, "$file_list[ $i ]\n");   
       # }
    #print OUTFILE2 @transposed_files;



    #for ( my $i = 0; $i < scalar @transposed_files ; $i++ )
     #   {
     #   print "crap!\n";
      #  for ( my $j = 0; $j < @transposedunique ; $j++ )
       #     {
       #     if ( $transposed_files[ $i ] =~ m/^$transposedunique[ $j ]/ )
      #       {
       #       print "$transposed_files[ $i ] goes into directory $transposedunique[ $j ]\n";
        #      print "i see this\n"; 
         #    } 
         #else
          #   {
           #   #print "no match\n";   
            # }    

        # }
     # }    

    #use File::Copy;
    FILE: for my $file (@file_list) {
    chomp $file;
    for my $name (@unique_gene_list) {
    #print "i see this $name\n";
    chomp $name;
    if ($file =~ /^\Q$name\E/) {
      print "$file goes with $name\n";
      #my $movethisfile = "/Users/ericschmidt/Desktop/Charles/SNPouput/$ARGV[1]/$file"; 
      #print "$movethisfile\n";    
      #move( "$movethisfile", "/Users/ericschmidt/Desktop/Charles/SNPouput/$name/$file" );
      print -f "/Users/ericschmidt/Desktop/Charles/SNPouput/ch19test/$file"; 
      print -d "/Users/ericschmidt/Desktop/Charles/SNPouput/$name";

          rename "/Users/ericschmidt/Desktop/Charles/SNPouput/ch19test/$file", "/Users/ericschmidt/Desktop/Charles/SNPouput/$name/$file" or die "rename failed because: $!\n";
          next FILE;
        }
      }
      print "no match for $file\n";
    }

    #use Data::Dumper;
    #print Dumper \@unique_gene_list, \@transposed_files;

错误是:

Use of uninitialized value in print at makeDirectories.pl line 130, <$FILE> line 19.
Use of uninitialized value in print at makeDirectories.pl line 131, <$FILE> line 19.

0 个答案:

没有答案