1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/perl use v5.12; use warnings; use File::Copy qw(copy); my $src = '/media/sdcard/bilder'; #quellordner my $dst = '/home/stefan/meine_bilder'; #zielordner #um aus ordner statt array die files zu lesen #opendir (img, $src) || die $!; #my @files = readdir(img); #close img; my @files = qw(1.gif 2.gif 2a.gif 3.gif 4.gif 4a.gif 4b.gif 5.gif); my $i = 1; for my $file (sort @files) { say "$src/$file\t-> $dst/$i.gif"; # copy "$src/$file", "$dst/$i.gif"; $i++; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
use 5.020; use warnings; use File::Copy; my $in_dir = '/tmp/foo'; my $out_dir = '/tmp/bar'; opendir(my $dir_handle, $in_dir) or die "Can't open directory '$in_dir': '$!'\n"; my @files = readdir($dir_handle); closedir $dir_handle; # Eliminate entries starting with '.' @files = grep { $_ !~ /^\./ } @files; # Split the file names in their numeric and non-numeric part my @unsorted = map { [/^(\d+)(.*)/] } @files; # Sort by numbers first, non-numeric part second my @sorted = sort { $a->[0] <=> $b->[0] or $a->[1] cmp $b->[1] } @unsorted; # ...and re-build the filenames, re-using our old array @files = map { $_->[0] . $_->[1] } @sorted; # Finally, copy the stuff my $file_number = 1; for my $file(@files) { copy("$in_dir/$file", "$out_dir/${file_number}.gif") or die "The copy operation for '$file' failed: '$!'"; $file_number++; }