CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
copy files based on value of a hash key

 



StarkRavingCalm
User

Oct 21, 2015, 6:55 AM

Post #1 of 4 (1021 views)
copy files based on value of a hash key Can't Post

I have a hash that contains the following:
filename prefix -> destination directory

I am able to use the values to create a directory structure but have not been able to have it copy the files into that structure (once POC is done I will change it to a move command)

I would think it would work in a similar fashion to the mkdir section.

Relavent code:


Code
for my $value (values %maphash) { 
print "$value\n";
mkdir $value;

}

for my $key (key %maphash) {
copy ($key, $value);


}


Additionally, how can a wildcard be used for this?
If doing manually, I would do:
cp FILENAMEPREFIX-01* /data/documents/1979
cp FILENAMEPREFIX-02* /data/documents/1980
etc...


Thanks in advance


(This post was edited by StarkRavingCalm on Oct 21, 2015, 6:56 AM)


FishMonger
Veteran / Moderator

Oct 21, 2015, 7:27 AM

Post #2 of 4 (1018 views)
Re: [StarkRavingCalm] copy files based on value of a hash key [In reply to] Can't Post

The $value var in the for loop that does the copy was not declared/defined. If you are using the strict and warnings pragmas like you should be doing, perl would have told you about that error.

Which module are you using to do the file copy?


StarkRavingCalm
User

Oct 21, 2015, 7:30 AM

Post #3 of 4 (1015 views)
Re: [FishMonger] copy files based on value of a hash key [In reply to] Can't Post

Using
File::Copy qw(move);
File::Copy;
And also have File::Basename; loaded if relevant


FishMonger
Veteran / Moderator

Oct 21, 2015, 7:44 AM

Post #4 of 4 (1011 views)
Re: [StarkRavingCalm] copy files based on value of a hash key [In reply to] Can't Post

There's no need to load File::Copy twice.

File::Copy only works on single files. If you want to use a wildcard to copy multiple files, then you'll need to use the glob() function to retrieve the list of files and loop over that list.

The other option is to make a system call to the cp command instead of using the module.


(This post was edited by FishMonger on Oct 21, 2015, 7:45 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives