CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Copy and Delete File

 



jhoang
New User

May 23, 2013, 12:55 PM

Post #1 of 6 (516 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);


Laurent_R
Veteran / Moderator

May 23, 2013, 3:20 PM

Post #2 of 6 (503 views)
Re: [jhoang] Copy and Delete File [In reply to] Can't Post

You want to move files to another file? Please explain. To another folder, perhaps?


jhoang
New User

May 24, 2013, 5:55 AM

Post #3 of 6 (495 views)
Re: [Laurent_R] Copy and Delete File [In reply to] Can't Post

Yes sorry, that was a typo. Search through folder A, find all the files with ABC in their file names, and move them to folder B.


BillKSmith
Veteran

May 24, 2013, 6:40 AM

Post #4 of 6 (492 views)
Re: [jhoang] Copy and Delete File [In reply to] Can't Post

The code you provided does not even compile error free. With the obvious errors corrected, it appears to copy .csv files from a folder called \dir to one called \dir1. It appends a time stamp from the old file to the name of the new one. It would become confused by a filename with an extension longer than three characters, the first three of which are 'csv'.

With your knowledge of other languages, and this much hint, you should be able to figure it out yourself. Why don't you tell us what statements you do not understand.
Good Luck,
Bill


FishMonger
Veteran / Moderator

May 24, 2013, 6:42 AM

Post #5 of 6 (492 views)
Re: [jhoang] Copy and Delete File [In reply to] Can't Post

If you want to move the files, then you should be using the move function, not the copy function.


recruiter
User

May 24, 2013, 8:44 AM

Post #6 of 6 (480 views)
Re: [FishMonger] Copy and Delete File [In reply to] Can't Post

Sticking to simple, here is an option.


Code
use strict;                                       #use strict.. 
use warnings; #use warnings..
use File::Copy;

my $old_path = '/path/to/old/dir'; # Path to your directory of files
my $new_path = '/path/to/new/dir'; # Path to the new directory

opendir my $dh, $old_path or die "failed: $!\n"; # Opening directory of files here
my @wanted = grep /(?i)ABC/, readdir $dh; # Match for filenames with ABC

foreach my $file (@wanted) { # Loop through list of matched files
if (-f "$old_path/$file" ) { # we found and do something...
# do something...
}
}

closedir $dh;



(This post was edited by hwnd on May 24, 2013, 10:25 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives