use strict; use warnings; use feature 'say'; use File::Copy; use File::Spec::Functions; use File::Path qw(make_path); use Cwd; # getting the "source" directory my $pwd = cwd(); my $source_dir = catdir($pwd,'source'); my @files; opendir(my $dir,$source_dir) or die "Unable to open directory '$source_dir': $!"; # search for files in the specified directory while (my $item = readdir($dir)) { # skip . and .. next if ($item =~ m/^\./); # skip directories next if (-d $item); # if this is a file, add it to the @files array my $test = catfile($source_dir,$item); if (-f $test) {push @files,($item);} } closedir($dir); # process the files found foreach my $file (@files) { # get the file's extension my ($extension) = ($file =~ m/.+\.(.+)/); my $subdir = catdir($source_dir,$extension); # if the folder for the extension does not exist, create it if (!(-d $subdir)) {make_path($subdir);} my $source = catfile($source_dir,$file); my $target = catfile($subdir,$file); # copy the file to the appropriate folder copy($source,$subdir); # move the file to the appropriate folder #move($source,$target); }