Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying Only Certain Images to Another Folder

Status
Not open for further replies.

BeginnerJoe

Programmer
Mar 8, 2014
3
US
I have a directory with three types of images (ex: gm0013a.jpg) all ending in either a.jpg b.jpg or c.jpg, depending upon their size. My question is, how would I copy only the files ending in a.jpg to a separate directory using perl. Should I slurp them all into an array or is there a better way. Any examples would be helpful. Thanks
 
There are many ways to do it but here is a snippet I use.
It could be simplified but I use on dirs. where some users save image names in a mixture of upper and lower case.
You need to declare the vars before it will work
Code:
opendir DIR, "directory"  or die;
while ($bfile = readdir DIR) {
	$start=length($bfile)-4;
	$ext=uc(substr($bfile,$start,4));
	if($ext eq '.JPG'){

	# Copy file here

	}
}

Keith
 
The problem with this is that it returns all the .jpg files. I know how to do that. I only want the aforementioned *a.jpg files.
 
Use a glob

Code:
use File::Copy;

for my $file (<YOUR_DIRECTORY/*a.jpg>) {
    move($file, 'NEW_DIRECTORY');
}

- Miller
 
Thanks all. I think I have it understood now. Appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top