in reply to Perl - Copy files to a newly created folder
The basic idea is to create a hash of the .ext's and then use that to either print that table or create a directory for each .ext.
Read the directory(s) all at once, create a hash table and use that to do the copies.
#!usr/bin/perl -w use strict; my $directory = 'C:/PerlTemp'; opendir (DIR, $directory) or die "unable to open $directory\n"; my %ext2files; #hash of array for each extension #.pl => name, .txt => name etc. foreach my $file (readdir DIR) { next if ($file =~ /^\./); #skip . and .. directories my ($ext) = ($file =~ /\.(\w+)$/)[0]; #get "xxx/yyy.ext" next unless defined $ext; # no ".extension" #now keep track of .ext to files... push @{$ext2files{$ext}},$file; } foreach my $ext (sort keys %ext2files) { # this prints the .ext ending and the number of files. # More than one syntax can do this, but I hope that # this is understandable... print "$ext \t", scalar @{$ext2files{$ext}}," files\n"; #it is possible to use the printf() formatting strings #in Perl. I didn't use that here, but it is possible. } __END__ PL 3 files SCP 1 files TXT 1 files bak 1 files bat 4 files bin 1 files c 28 files cav 1 files cmdline 1 files cpp 1 files csv 4 files dat 8 files db 4 files dbi 1 files doc 4 files exe 23 files h 2 files html 9 files ico 1 files ini 1 files jpg 1 files new 1 files out 2 files p 1 files pdf 3 files pl 1113 files pll 2 files pm 2 files script 1 files stackdump 3 files temp 1 files txt 173 files txtC 1 files xls 1 files xml 6 files zip 2 files Process completed successfully
|
|---|