Newbie
Newbie

Reputation: 35

Copy file that have specified name in the file name

I know simple copy like below,it able to copy file from one directory to another.

Code:

use File::Copy;

copy ("C:\Test\abc.txt","C:\Test2\abc.txt") or die "Copy failed: $!";

What I trying to do here is i got one file that user consistent update it and they will add the latest date behind the file name EG. abc_20141111.txt. Can I just ignore the date and copy the files?

Expected result: Copy the file that have name abc inside the file name to another location. I wanted to ignore the date behind the underscore. What can I change to get expected result?

Eg. File name - abc_20141111.txt

Upvotes: 0

Views: 103

Answers (2)

ssr1012
ssr1012

Reputation: 2589

use File::Copy;
my $file = 'abc_20141114.txt';
my $actualpath = 'D:\test';
my $destinpath = 'D:\newtest';
my $newfile = $file;
$newfile=~s/^([^\_]*)\_([^\.]*)\.txt$/$1.txt/i;
copy("$actualpath\\$file", "$destinpath\\$newfile") && print "Success\n";

Upvotes: 0

Newbie
Newbie

Reputation: 35

Use Perl's glob operator to get the list of files you need to open:

use strict;
use warnings;
use File::Copy;

my $old_dir = glob "C:/Modules/sharepoint/abc_*.xlsx";
my $new_dir = "C:/Modules/sharepoint/testing/";

copy ($old_dir,$new_dir);

Upvotes: 1

Related Questions