Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Move a file to another directory based on regex match

by hippo (Chancellor)
on Feb 25, 2021 at 14:56 UTC ( #11128783=note: print w/replies, xml ) Need Help??


in reply to Move a file to another directory based on regex match

Best guess without data: whatever you have chosen for $OutDir is either an invalid link to a different device, or one of its parents is. Try using /tmp as $OutDir and see if it works there.


🦛

Replies are listed 'Best First'.
Re^2: Move a file to another directory based on regex match
by DAN0207 (Acolyte) on Feb 25, 2021 at 17:02 UTC

    I used /tmp as $OutDir .But still the same error "move failed: Invalid cross-device link"

      How odd. Try this SSCCE which works fine for me.

      #!/usr/bin/env perl use strict; use warnings; use File::Copy 'move'; my $indir = '/tmp/foo'; my $outdir = '/tmp/bar'; mkdir $indir; mkdir $outdir; for my $f (qw/a b c/) { open my $out, '>', "$indir/$f" or die "Opening $f: $!"; print $out 'Woo-hoo!'; close $out; } opendir my $dir, $indir or die "Bad Dir $indir: $!"; while (my $infile = readdir ($dir)) { if ($infile =~ /b/) { print "Moving '$infile' ...\n"; move ("$indir/$infile", $outdir) or die "move failed: $! moving $indir/$infile to $outdir" +. " while we are in $ENV{PWD}\n"; } else { print "Skipping '$infile' ...\n"; } }

      🦛

        Thank you very much!!.It worked.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11128783]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2021-05-14 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (148 votes). Check out past polls.

    Notices?