Re: Move a file to another directory based on regex match
by haukex (Bishop) on Feb 25, 2021 at 14:57 UTC
|
The error message is strange, but I do see one issue with your code: readdir returns just the filenames without the directory name. I'd recommend you use File::Spec::Functions qw/catfile/; and move( catfile($InDir,$Infile), $OutDir ). If that fixes the issue, then perhaps the error message is just wrong (is this Windows or some OS other than *NIX?), and if it's doesn't fix the issue, please let us know Update: along with more information about $InDir and $OutDir. Also, Use strict and warnings!
| [reply] [d/l] [select] |
|
In fact, the documentation about readdir mentions the fact that you need to prepend the directory again.
Just in the (very unlikely) case that the files actually exist, the error message suggests that the OP is trying to move them across mount points. This would happen for example if you have two disks mounted below /mount:
/mount/hda/dir1
/mount/hdb/dir2
Then, moving files from /mount/hda/dir1 to /mount/hdb/dir2 would mean actually copying the bytes between the two devices. At least the mv program (and thus, maybe also the rename() syscall?) on my Debian system does such copying nowadays... | [reply] [d/l] [select] |
|
actually copying the bytes between the two devices. At least the mv program (and thus, maybe also the rename() syscall?) on my Debian system does such copying nowadays...
It's a good thing OP is using File::Copy, since that actually does copy the bytes (and only when necessary, AFAIK) - I belive this is what the OS's mv does too. I just tried rename across filesystems and did in fact get the Invalid cross-device link error.
| [reply] [d/l] [select] |
|
So maybe the detection of the sameness of the device doesn't work correctly in MSWin?
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
| [reply] [d/l] |
|
| [reply] |
|
| [reply] [d/l] [select] |
Re: Move a file to another directory based on regex match
by hippo (Chancellor) on Feb 25, 2021 at 14:56 UTC
|
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.
| [reply] [d/l] [select] |
|
| [reply] |
|
#!/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";
}
}
| [reply] [d/l] |
|
Re: Move a file to another directory based on regex match
by Marshall (Canon) on Feb 25, 2021 at 21:35 UTC
|
Start simple:
I may be wrong, but I think your regex is wrong.
'+' is not a valid file name character.
Start by printing the proposed files to be moved:
I don't know what \+ means?
#!/usr/bin/perl
use strict;
use warnings;
use File::Copy;
my ($inDir, $outDir) = @ARGV;
opendir (DIR, $inDir) or die "Bad Input Directory $inDir $!\n";
while (my $infile = readdir(DIR))
{
if ($infile =~ 'F\d{8}\.\d{4}\+\d{4}\-\d{4}\+\d{4}_.*')
#like F12345678.1234+1234-9876+3456.....
{
print "$inDir/$infile\n";
}
}
| [reply] [d/l] |
|
# https://www.youtube.com/watch?v=iqu132vTl5Y
perl -e 'q[x+v] =~ m{\+} and print q[I saw the sign]'
| [reply] [d/l] [select] |
|
| [reply] |
|
| [reply] [d/l] |
|
| [reply] |
Re: Move a file to another directory based on regex match
by Anonymous Monk on Feb 25, 2021 at 15:46 UTC
|
I would start by printing the values of $Infile and $Outdir so that you can be sure of what you are asking File::Copy to do. Also be sure that the destinations are not symbolic links to other devices. File::Copy is supposed to first attempt a rename, then fall back to a move. What are the characteristics of the actual things when this error happens? | [reply] |