It is not clear if the files should be renamed sequentially then moved to the new directory, or if the files should be moved then renamed.
Anyway, here is one way you might do the former... if you need the latter this can easily be adapted. This looks for all the .jpg files in the specified directory then only proceeds if there is a corresponding .xml file. The sort on @files is not strictly necessary and could be removed; glob() returns file names in OS sorting order. If you need some other order though, that is a good place to specify it.
Note: As this is written, if there are files already in one of the destination directories with the same name as the renamed jpeg/xml files, they will be clobbered.
use warnings; use strict; use File::Basename; use File::Copy; my $dir = 'ParticleAcquisitionDataImages/'; # or whatever my @files = glob "${dir}*.jpg"; my $rename_index = 1; for my $file ( sort @files ){ next if -d $file; my($filename, $directory, $suffix) = fileparse($file, '.jpg'); next unless -e "${dir}$filename.xml"; $filename = changename($filename); open my $fh, '<', "${dir}$filename.xml" or die "$!"; seek($fh, 498, 0); my $newdir; read($fh, $newdir, 3); close $fh; substr $newdir, 1, 1, ''; mkdir "$dir$newdir" unless -e "$dir$newdir"; for my $ext ( qw/.jpg .xml/) { move "$dir$filename$ext", "$dir$newdir/$filename$ext" or die $ +!; } } sub changename { my $filename = shift; for my $suffix ( qw/.jpg .xml/) { rename "$dir$filename$suffix", "$dir$rename_index$suffix" or d +ie $!; } return $rename_index++; }
In reply to Re: Renaming files in a directory in sequence
by thundergnat
in thread Renaming files in a directory in sequence
by Max79
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |