http://qs321.pair.com?node_id=1150122


in reply to question regarding File::Copy, and hashes that contain file paths

Welcome Leetsauce,

If your script works for 1 ( then it will work for many ), then you only need to show us the following:

my %compHash = ( "e:/pervasive/staging/summit/C01" => "e:/pervasive/test/APEX01201 +5/C01", ); foreach my $key(keys %compHash) { my $value = $compHash{$key}; my $ret = move($key , $value); if ( $ret ) ## If $ret == 1, then success { print "Successfuly moved |$key| to |$value| .\n"; } else { print "|$key| not moved to |$value|: $!\n"; } }
plus any additional output you might expect. ( Notice I put '|' before and after the '$key' and '$value', to catch any spaces )

From the documentation from 'File::Copy', it states 'move' works on files. Are the keys and values you are working with directories?

Regards...Ed

"Well done is better than well said." - Benjamin Franklin

  • Comment on Re: question regarding File::Copy, and hashes that contain file paths
  • Download Code

Replies are listed 'Best First'.
Re^2: question regarding File::Copy, and hashes that contain file paths
by Leetsauce (Novice) on Dec 12, 2015 at 16:43 UTC
    Yes they are directories, and they have things inside them as well. Think that could be causing an issue? The for each that was posted before yours basically had the same result, with exception it was then telling me the new directory didn't exist.
      Yes they are directories, and they have things inside them as well.

      Take a look at File::Copy::Recursive

      #!/usr/bin/perl use strict; use File::Copy::Recursive qw(dircopy dirmove); use Time::Piece; my $MDY = localtime->mdy(''); print "$MDY \n\n"; my $src = 'e:/pervasive/staging/summit/'; my $dest = 'e:/pervasive/test/'; my @f = qw(01 02 03 04 05 06 07 08 30 40 41 42 43 44 44 50 80 85 99 ); my %compHash; for my $n (@f[0]){ # replace with @f if it works for first $compHash{'C'.$n} = 'APEX'.$n.'2015/C'.$n; $compHash{'C'.$n.'-2015'} = 'APEX'.$n.'2015/CSW'; }; foreach my $key (sort keys %compHash) { my $from = $src.$key; my $to = $dest.$compHash{$key}; print "Moving $from $to\n"; dirmove ( $from,$to ) or die "$!"; }
      poj
        I used the hash from this, ultimately. It worked perfectly. I try to stay away from hardcoding stuff as much as I can, but in this situation it was absolutely merited. The way you did this was perfect for me, and I so greatly appreciate your help with this.
        How would I check to see if the file exists with this? So we have @f defined as the list of company numbers (C## - which represents company ID ) and for each of those, we're renaming the directory and moving it, but it breaks when it hits the key that does not exist in the folder (company 6 is not inside my zip, but it is a possible company that CAN be in the zip) so I'm looking for a quick way to check if the actual file, that corresponds with the key in our @f, even exists because if it doesn't, i need to skip it. I think this will involve next, or exists, or maybe both. Any insight?

      Leetsauce,

      You can test for file or directory by:

      if ( -f $file ) { .... } ## $file is a file elsif ( -d $file ) { ... } ## also could use '_' for speed else { ,,, } ## Not what we're looking for
      Also you may wish to look at 'glob' for getting information about directory contents ( files and/or directories ).

      Good Luck...Ed

      "Well done is better than well said." - Benjamin Franklin

Re^2: question regarding File::Copy, and hashes that contain file paths
by Leetsauce (Novice) on Dec 12, 2015 at 16:44 UTC
    Now that I think about it, I could just create a new copy2 sub, and make it specifically deal with directories. I see the reference you're talking about now.
Re^2: question regarding File::Copy, and hashes that contain file paths
by Leetsauce (Novice) on Dec 16, 2015 at 00:22 UTC
    Thank you for pointing this out. Sorry about posting as much of the script as I did. I wanted to be able to get my point across, but, as you stated, if it worked for 1 it would work for whatever the cap on a foreach loop is in perl =p Thanks for your help! I ended up using your Foreach because it handled it the way I wanted.

      Dear Leetsauce,

      My point wasn't to criticize your elaborate script, but to help you get better answers in the future. I'm glad you found a viable solution, but you should think of this as the start of your journey. Perl will allow you to do some amazing things, but you have a lot to learn, and it shouldn't be painful.

      Thank you for letting me know I helped you!

      Good Luck and Best Regards...Ed

      "Well done is better than well said." - Benjamin Franklin