How to filter recordset for a copy file script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BigSky
    New Member
    • Jul 2012
    • 2

    How to filter recordset for a copy file script

    Created perl script to query oracle db to get directory path and file name. The script will form an UNC path to copy file to appropriate directory via a while statement and foreach loop. Script works just fine which copies one file at a time. I think it would be more efficient if I can identify if I had multiple files that go to same directory, which would be the case 99.9% of the time. Sometimes I would have 1000+ files that need to copy to one directory (dir1) and maybe 200 files to another directory (dir2) and maybe 100 files to another directory (dir3) in one instance.
    I query the db, use a fetchrow_array in a while statement that strips off one row at a time.
    How can I filter the recordset to get the files for dir1, dir2 or dir3?
  • BigSky
    New Member
    • Jul 2012
    • 2

    #2
    I believe I figured it out. I just need to interweave code to original script.
    Code:
    while ( $fpn = $sthpathnames->fetchrow_arrayref) {
    	push @pathfiles, [ @$fpn ];
    }	
    die "Fetch failed due to $DBI::errstr" if $DBI::err;
    
    foreach $pathfile ( @pathfiles ) {
    	push @filepaths, @$pathfile[0];
    	#print "@$pathfile[0]\n";
    }
    my %unqpath = ();
    my @unqpath = grep { ! $unqpath{ $_ }++ } @filepaths;
    foreach $unqpath ( @unqpath ) {
    	foreach $pathfile ( @pathfiles ) {
    		if ( $unqpath eq @$pathfile[0] ) {
    			#print "$unqpath and @$pathfile[1]\n";
    			my $filequote = '"' . @$pathfile[1] . '" ';
    			push @filequotes, $filequote;
    		}		
    	}
    	foreach ( @filequotes ){
    		print "$_\n";
    	}
    	print "path $unqpath and files @filequotes\n";
    	@filequotes = ();
    }

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Mind you I have only given this a minute or so of thought, but here is what I think. You could make a hash of arrays. The keys of the hash can be the path's and the values for each key can be an array of file names that you would push new values on to. Once completed, you could take process each key/value set using the key as the path (which it is) and the array of values as the file names to move.

      Regards,

      Jeff

      Comment

      Working...