×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Copying Only Certain Images to Another Folder

Copying Only Certain Images to Another Folder

Copying Only Certain Images to Another Folder

(OP)
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

RE: Copying Only Certain Images to Another Folder

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
www.studiosoft.co.uk

RE: Copying Only Certain Images to Another Folder

(OP)
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.

RE: Copying Only Certain Images to Another Folder

change the value 4 to 5 and change the line

CODE

if($ext eq '.JPG'){ 
to

CODE

if($ext eq 'A.JPG'){ 

Keith
www.studiosoft.co.uk

RE: Copying Only Certain Images to Another Folder

Use a glob

CODE

use File::Copy;

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

- Miller

RE: Copying Only Certain Images to Another Folder

(OP)
Thanks all. I think I have it understood now. Appreciate it.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close