Reputation: 1
I have developed a Perl script for data movement from one folder to another. I have one folder in D drive (D:\projectx\MUX4TO1_TB
). Inside that folder, I have to create a new folder named newfolder
. Then I want to search for .ps
files inside MUX4TO1_TB
, and if any are found, I want to move them to newfolder
.
The code I have developed is 70% working, but data movement is not happening from MUX4TO1_TB
to newfolder
. Please help me to complete it.
#!/usr/bin/perl
use warnings;
use File::Copy 'move';
my $srcdir = "d:\\projectx\\MUX4TO1_TB";
mkdir("D:\\projectx\\MUX4TO1_TB/Newfolder") || die "Unable to create directory <$!>\n";
my $dest = "D:\\projectx\\MUX4TO1_TB\\Newfolder";
opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
$filterstring = ".ps";
foreach my $filename (readdir(DIR)) {
if ($filename =~ m/$filterstring/) {
$cfile = $srcdir . $filename;
print "\n moving $cfile from $srcdir to $dest \n";
move($cfile, $dest) or die "The move operation failed: $!";
}
}
closedir(DIR);
Upvotes: 0
Views: 3997
Reputation: 1849
You did not provide a valid path of the final target
$cfile= $srcdir. '\\' . $filename;
You missed \\
backslashes while assigning a new value to $cfile
.
Full code:
use warnings;
use File::Copy 'move';
my $srcdir= "d:\\projectx\\MUX4TO1_TB";
mkdir("D:\\projectx\\MUX4TO1_TB/Newfolder") || die "Unable to create directory <$!>\n";
my $dest = "D:\\projectx\\MUX4TO1_TB\\Newfolder";
opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
$filterstring=".ps";
foreach my $filename (readdir(DIR))
{
if ($filename =~ m/$filterstring/) {
$cfile= $srcdir. '\\' . $filename; ## You missed '\\';
print "\n moving $cfile from $srcdir to $dest \n";
move($cfile,$dest)or die "The move operation failed: $!";
}
}
closedir(DIR);
Upvotes: 1