On 3 Jan 2007 at 8:02, Tom Messmer wrote:

> Hello everyone,

Hello and welcome,

> Just joined this list and I have a doozie I've been working on for a  
> bit here to no avail. The entire problem is this; I have a list of  
> files, say that they are named "flynn.foo, flynn_something.foo,  
> flaherty.foo flaherty_something.foo" and so forth. Each of these  
> files must live(be moved to)  an individual directory named for the  
> author(flynn, flaherty, etc) and then be symlinked to an entirely  
> different directory in another part of the filesystem identical to  
> the first one(flynn, flaherty...) If I was doing this on the command  
> line I'd do
> 
> cp /usr/blah/flaherty.foo /usr/blah/blahagain/flaherty/ &&
>   ln -s /usr/blah/blahagain/flaherty/flaherty.foo /usr/blah/ 
> blahoncemore/flaherty.foo
> 
>   So far I've gotten to the point where I can strip out the  names  
> from the files and create the two sets of directories, but I'm  
> stumped on how to them copy each of these files into the correct  
> directory and then symlink from the storage directory to the other  
> name directory. Now I know how to copy files and symlink files with  
> perl, but the logic involved in doing this to 30 files is beyond me  
> at the moment. Anyone have a clue?


This is a hard one to test. I haven't created the file structure you 
described so YOU MUST TEST IT. I think you'll get better suggestions 
from the list in time but I thought I'd have a shot anyway (I await 
the rebukes).

I'll leave the symlink for you (perldoc -f symlink). Try this... it 
might get you started.
Good luck,
Dp.


#!/usr/bin/perl # or where ever your perl is installed.

use strict;
use warnings;
use File::Copy;
use File::Basename;

my $source_dir = '/usr/blah';
my $distin_dir = '/usr/blany/blanagain/';

# Open the dir for reading or die.
opendir(DIR,$source_dir) or die "Can't open $source_dir: $!\n";

# Collect the author directories (and names).
my @auth_dir = grep { -f "$source_dir/$_" } readdir(DIR);

foreach my $i (@auth_dir) {

# Get the filename of the file from /usr/blab/flynn.foo
        my $pathname = "$source_dir/$i";

# Grab the author from flynn.foo, splitting on the period.

# method 1 via regex
        my ($author,$file) = ($i =~ /(\w+)\.(\w+)/);
# method 2 via split
        my @f = split(/\./,$i);


# Going with method 2 for the moment.
        my $dist = $distin_dir.$f[0];
        print "Copying $pathname-> $dist.$f[1]\n";

#Keep commented until your sure.
#       copy($i,$dist) or die "Can't copy $i -> $dist: $!\n";

}

print "Done...\n";






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to