This is the way I'd do it, which only uses core modules.
#!/usr/bin/perl use warnings; use strict; use File::Copy; use File::Path qw(make_path); use File::Spec::Functions; use File::Basename; my $dir = 'C:/dev'; my @files = grep { ! -d } <$dir/*>; FILE: foreach my $file (@files) { my ($name, $path, $suffix) = fileparse($file, qr/\.[^.]*/); $suffix or do { warn "$name does not have an ext\n"; next FILE; }; $suffix =~ s/\.//; # (optional) strip leading . my $subdir = catdir($path, $suffix); make_path($subdir) unless -d $subdir; move($file, $subdir) or warn "Failed to move $file to $subdir <$!> +\n"; }
EDIT: If you have spaces in the source path, you will probably need to use quotes when building @files.
Change grep { ! -d } <$dir/*> to grep { ! -d } <"$dir/*">
In reply to Re: Perl - Copy files to a newly created folder
by fishmonger
in thread Perl - Copy files to a newly created folder
by Benichouniev
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |