Monday 8 June 2020

perl program to copy files from one directory to another directory

Perl script copies all the files with a given extension from one directory to another. The source and destination directories are passed as command-line arguments, along with the file extension.

Method 1:

#!/usr/bin/perl

use File::Copy;

my ($src_dir, $dest_dir, $ext) = @ARGV;

opendir my $dh, $src_dir or die "Couldn't open directory $src_dir: $!";

while (my $filename = readdir $dh) {

    next unless $filename =~ /\.$ext$/; # Match extension

    my $src_path = "$src_dir/$filename";

    my $dest_path = "$dest_dir/$filename";

    copy $src_path, $dest_path or warn "Couldn't copy $filename: $!";

}

closedir $dh;

Save the script as copyfiles.pl and run it with perl copyfiles.pl sourcedir destdir txt. This will copy all the files with the extension "txt" from "sourcedir" to "destdir".

Method 2:Using File::Find

The File::Find module provides a way to recursively traverse a directory tree and perform actions on files that match certain criteria.

#!/usr/bin/perl

use File::Find;

use File::Copy;

my ($source_dir, $dest_dir, $file_ext) = @ARGV;

find(sub {

    return unless -f $_; # Skip directories

    return unless /\.$file_ext$/; # Match file extension

    my $source_file = $File::Find::name;

    $source_file =~ s/^\Q$source_dir\E//; # Remove source directory from path

    my $dest_file = "$dest_dir$source_file";

    mkpath($dest_file); # Create parent directories if necessary

    copy($File::Find::name, $dest_file) or warn "Couldn't copy $File::Find::name: $!";

}, $source_dir);

Save the script as copy_files_method2.pl and run it with perl copy_files_method2.pl /path/to/source/dir /path/to/dest/dir txt. This will copy all files with the extension "txt" from the source directory to the destination directory and preserve the directory structure.

Method 3: Using system()

The system() function in Perl allows you to execute shell commands. You can use the cp command to copy files from one directory to another.

#!/usr/bin/perl

my ($source_dir, $dest_dir, $file_ext) = @ARGV;

my $command = "cp $source_dir/*.$file_ext $dest_dir/";

system($command) == 0 or die "Couldn't execute command: $command";

Save the script as copy_files_method3.pl and run it with perl copy_files_method3.pl /path/to/source/dir /path/to/dest/dir txt. This will copy all files with the extension "txt" from the source directory to the destination directory using the cp command.

Method 4: Using opendir() and readdir()

This method uses the opendir() and readdir() functions to read the contents of a directory and then copy the files matching the specified file extension.

#!/usr/bin/perl

use File::Copy;

my ($source_dir, $dest_dir, $file_ext) = @ARGV;

opendir(DIR, $source_dir) or die "Can't open $source_dir: $!";

my @files = readdir(DIR);

closedir(DIR);

foreach my $file (@files) {

    next unless $file =~ /\.$file_ext$/; # Match file extension

    my $source_file = "$source_dir/$file";

    my $dest_file = "$dest_dir/$file";

    copy($source_file, $dest_file) or warn "Couldn't copy $file: $!";

}

Save the script as copy_files_method4.pl and run it with perl copy_files_method4.pl /path/to/source/dir /path/to/dest/dir txt. This will copy all files with the extension "txt" from the source directory to the destination directory.

Method 5: Using glob()

The glob() function returns a list of filenames that match a specified pattern. You can use this function to get a list of files matching the specified file extension and then copy them to the destination directory.

#!/usr/bin/perl

use File::Copy;

my ($source_dir, $dest_dir, $file_ext) = @ARGV;

foreach my $file (glob("$source_dir/*.$file_ext")) {

    my $dest_file = "$dest_dir/" . (split /\//, $file)[-1];

    copy($file, $dest_file) or warn "Couldn't copy $file: $!";

}

Save the script as copy_files_method5.pl and run it with perl copy_files_method5.pl /path/to/source/dir /path/to/dest/dir txt. This will copy all files with the extension "txt" from the source directory to the destination directory.

Method 6: Using Path::Tiny

The Path::Tiny module provides a simple interface to manipulate file and directory paths. It also provides a copy() function that can be used to copy files from one directory to another.

#!/usr/bin/perl

use Path::Tiny;

my ($source_dir, $dest_dir, $file_ext) = @ARGV;

foreach my $file (path($source_dir)->children(qr/\.${file_ext}$/)) {

    my $dest_file = path($dest_dir, $file->basename);

    $file->copy($dest_file) or warn "Couldn't copy $file: $!";

}

Save the script as copy_files_method6.pl and run it with perl copy_files_method6.pl /path/to/source/dir /path/to/dest/dir txt. This will copy all files with the extension "txt" from the source directory to the destination directory.

The Path::Tiny module also provides other useful functions for working with files and directories, such as move(), rename(), and unlink(). It's worth exploring if you need to do more complex file manipulation in your Perl scripts.

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home