I've got a set of folders with each contain many files. I need to extract a specific file extention(.sof) from each of these folders to a new directory(or the same directory in a new folder).
Any help is appreciated.

If I may ask, it is the file with extension you want? i.e Iterate through all the file in a particular directory, search all the folders and copy or move all the file with a particular extension to another folder. Is that what you want? Wanting to be sure I understand your question.

Hi caven.chunyen,
I will take my initial assumption and write a code that works. The script uses a recursive subroutine, to parse through all the folders in a particular directory, and copy to another folder, files with designated extention. As given by the user.
Please note, that the recusive subroutine does could easily be done by the module File::Find, with all the details hidden under the hood of the module. However, the purpose of walking through directories with different folders. I used this.

use warnings;
use strict;
use Carp qw(croak);
use File::Spec::Functions qw(catfile);
use File::Copy qw(copy);
use Cwd qw(abs_path);

croak " Usage: perlscript.pl <directory_to_scan>" unless defined $ARGV[0];

#create a folder for files
my $folder = catfile( $ENV{USERPROFILE}, 'Desktop', 'File_Copied_into' );
mkdir $folder;

my $filepath = $ARGV[0];

#user input file extention of choice
print "Enter the Extention of the file you want to copy:";
chomp( my $extention = <STDIN> );

#regex compilation, using qr{}
my $file_extention = qr{\Q$extention\E$};

#call function scan_disk, with
#subroutine reference
    sub {
        copy( $_[0], $folder );

sub scan_disk {
    my ( $path, $coderef ) = @_;
    if ( -d $path ) {
        $path = abs_path($path);
        chdir $path or croak " Directory path doesn't exit: $!";
        opendir my $dh, $path or croak "can't open directory: $!";
        while ( defined( my $read_path = readdir($dh) ) ) {
            chomp $read_path;
            next if $read_path eq '.' or $read_path eq '..';
            if ( $read_path =~ m{$file_extention} ) {
            scan_disk( "$path/$read_path", $coderef );    # subrotine recursion

Please, note that this script will only copy the file with the extention specify, by the user. If, you want to totally relocate the file then you might have to change the copy function of the module File::Copy to move. Please, check perldoc File::Copy for detailed info.
In using this, one might have to run the script with the desired directory as a CLI arugment.
Peruse this code lines and see if it makes you happy! It should!!!

Thousand thanks. It's works like I intended. Very much appreciate it. :)

Really appericiating work by you, no doubt about your ability.


commented: spam -2