Ignore:
Timestamp:
2012-05-10T20:01:31+12:00 (12 years ago)
Author:
ak19
Message:

Newly added mv_dir_contents subroutine was not complete: it did not move the directory contents of subdirectories which I thought was implicit. Added explicit recursion on subfolders and handling of special cases where folders are empty.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/util.pm

    r25554 r25572  
    193193   
    194194    # construct absolute paths
     195    my @full_path_files = ();
    195196    foreach my $file (@files) {
    196197        # process all except . and ..
    197         unless($file eq "." || $file eq "..") {         
    198             $file = &util::filename_cat($src_dir, $file); # this is sufficient to overwrite this file's name in @files
    199                     # don't seem to need a separate array to keep the updated absolute path versions of the filenames
    200         }       
    201     }
    202     &util::mv(@files, $dest_dir);
     198        unless($file eq "." || $file eq "..") {
     199            my $tailname = $file;
     200            $file = &filename_cat($src_dir, $file);
     201               
     202            if(-d $file) { # recursion on directories
     203                #print STDERR "**** $file is a directory, to be copied to $dest_dir\\$tailname\n";
     204                &mv_dir_contents($file, &filename_cat($dest_dir, $tailname));
     205               
     206                # now all content is moved across, delete empty dir in source folder
     207                if(&is_dir_empty($file)) {
     208                    &rm_r($file);
     209                } else { # error
     210                    print STDERR "ERROR. util::mv_dir_contents: subfolder $file still non-empty after moving contents to $dest_dir\n";
     211                }
     212            } else { # process files with a simple move
     213                push(@full_path_files, $file);
     214            }           
     215        }
     216    }
     217   
     218    if(!&dir_exists($dest_dir)) { # create target toplevel folder or subfolders if they don't exist
     219        &mk_dir($dest_dir);
     220    }
     221   
     222    #print STDERR "@@@@@ Copying files to: $dest_dir\n";
     223    if(@full_path_files) { # if non-empty, there's something to copy across
     224        &mv(@full_path_files, $dest_dir);
     225    }
    203226}
    204227
Note: See TracChangeset for help on using the changeset viewer.