Kevman has asked for the wisdom of the Perl Monks concerning the following question:


Hi All - Nother Question about Globbing
I can happily use a system call to mv /xxx/*.pl /yyy/*.pl
but this is not really my preferred approach. As a work around, I came up with :-
#!/usr/local/bin/perl -w use strict; use File::Copy; use File::Basename; my @files = glob "/xxx/*.pl"; my $in = "/xxx/"; my $out = "/yyy/"; @files = map { basename($_) } @files; move "$in/$_", "$out/$_" for @files;
Has anyone out there actually got the glob command to work with the move command (in one line)?

(And before anyone tells me, I know should be checking for failures with the move :)

Replies are listed 'Best First'.
Re: Globbing and Moving Together
by wog (Curate) on Jun 29, 2001 at 19:47 UTC
    If you read File::Copy's docs carefully you'll note that move will move the file to a directory if that's what the last argument is (hence an advantage over rename.) So, you can use something like:

    move $_, $out for glob("$in/*.pl");
      Now that really is the "Bees Knees"!!!!
      I had tried using File::Glob as it suggests in "programming perl",
      but gave up on that for the straight "glob" approach.

      Many Thanks

      Kevman
      aaah. sorry... give me a sec... *switch* ...right, that's my brain on now. ++2u

      "Argument is futile - you will be ignorralated!"

Re: Globbing and Moving Together
by larryk (Friar) on Jun 29, 2001 at 19:45 UTC
    The move command? Do you mean rename?

    "Argument is futile - you will be ignorralated!"

Re: Globbing and Moving Together
by particle (Vicar) on Jun 29, 2001 at 20:11 UTC
    this works for me...
    #!/usr/bin/perl -w use strict; use File::Basename; use File::Copy; my @sourceD = "i:/tmp"; my @targetD = "c:/temp"; move @{$_} foreach map { [ "$sourceD/$_", "$targetD/$_" ] } map {basen +ame $_} <$sourceD/*.pl>;
    i can break it down a bit...
    the important statement, obviously, is the last. map statements are best read backwards. so let's do that.

    ~the end <$sourceD/*.pl> is the glob. this creates a list of files.
    ~the first map (working backwards) map {basename $_} gets the basename of the files, in a list
    ~the second map map { [ "$sourceD/$_", "$targetD/$_" ] } creates a list of anonymous arrays with the source path/filename, and target path/filename
    ~the foreach lets us iterate over each member in the list of anonymous arrays
    ~the funny looking @{$_} takes the anonymous array reference in $_ and evaluates it as an array, which is fed to move.

    err... i didn't check the output of move, either...

    ~Particle