
jhoang
New User
May 23, 2013, 12:55 PM
Post #1 of 6
(1253 views)
|
Copy and Delete File
|
Can't Post
|
|
Hi there. Perl is a new language I am exploring and I want to create a script that will search through a folder and find all files that contain "ABC" in the file name, and move them into another file. When I say move, I mean copy from original location, paste in new location, and delete from original location. I found a script doing something very similar to this but I dont quite understand the syntax yet. Does anyone mind commenting the this script to let me know exactly what is going on? Thanks in advance. use strict; use File::Copy; use File::stat; use POSIX qw(strftime); my $dirname = "\\dir\\"; my $dirname2 = "\\dir1\\"; my $filterstring = "\\.csv"; opendir ( DIR, $dirname ) || die "Error in opening dir $dirname\n"; foreach my $filename (readdir(DIR)) { if ($filename =~ m/$filterstring/) my $len=length($filename); my $v=$len-4; my $str=substr $filename, 0, $v; my $old_file = $dirname . $filename; my $new_file = $dirname2 . $str . '_' . strftime("%Y%m%d", localtime(stat($old_file)->mtime)).'.'.'csv'; print " $new_file\n"; copy($old_file, $new_file); } } closedir(DIR);
|