1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Rep Power

    Cant copy files from ls

    I am basically chdir to a directory with folders named A thru Z and then 'ls thru them to create an array and then use that array content to distribute the files to new directories using File::Copy.

    If I hard code the file names into my variables, everything works as I want it to, but if I let my script load the variables, it breaks and File::Copy hangs. I have tried to figure it out, but I don't know what is going on with it.

    Basically, I am taking image files from lettered directories and if the image file name is 3 numbers (hundreds), I take the first number of the filename and copy the file to the first number corresponding directory in hundreds. If the image file name is 4 numbers, then I copy it to the corresponding thousands directory.

    Thanks for help figuring this out.

    I just found this in the error logs:
    Insecure dependency in open while running with -T switch at /usr/lib/perl5/5.8.8/File/Copy.pm line 133

    OK, I figured it out.. I needed to un-taint the input from the 'ls...

    This site has helped me figure out my own problems several times... THANKS!!

    It must be beyond what I know. Here is my code:
    @letters = (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z);
    my $logodir = "/var/www/vhosts/xxx.com/httpdocs/logos";
    foreach (@letters) {
    foreach $file (@files) {
      $ls = `ls $file`;
      @ls = split(/\s+/,$ls);
      foreach $temp_file (@ls) {
      ###  $temp_file = "L123.jpg";
        $mov_file = "$temp_file";
        if ($temp_file =~ /(\w+)\/L(\d+)(-?)(.*)jpg/) {
          if (length $2 < 4) {
            $new_dir = substr($2,0,1) . "00";
            $mov_file =~ s/\w+\///ig;
            use File::Copy qw(copy);
            copy ("$temp_file", "$logodir/temp/$new_dir/$mov_file")  || print $TEMP "ERROR:$! Failed Copy $temp_file to $logodir/temp/$new_dir/$mov_file\n";;
          else {
            $new_dir = substr($2,0,2) . "00";
            $mov_file =~ s/\w+\///ig;
            copy ("$temp_file", "$logodir/temp/$new_dir/$mov_file")  || print $TEMP "ERROR:$! Failed Copy $temp_file to $logodir/temp/$new_dir/$mov_file\n";;
    But if I hard code:
    $temp_file = "L123.jpg";
    $mov_file = "L123.jpg";
    Then it works like a champ....

    I don't know what is up with it....

    Last edited by additude; July 25th, 2016 at 06:47 AM. Reason: Updated

IMN logo majestic logo threadwatch logo seochat tools logo