LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-22-2012, 02:36 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
Perl read dir script - regex gif|jpg


I worte a script that moves all the gif from my public_html directory to the images directory.

it works.

Code:
#!/usr/bin/perl

$dir_name = "/home/casper/public_html"; 

opendir (DIR, $dir_name) || die "Error in opening dir $dirname\n"; 
while ( ($filename = readdir (DIR))) { 
        if ($filename =~ /\w+\.gif/){
                system("cp $filename /home/casper/public_html/images/"); 
        }       
        sleep 1;  
}
however when i combines the jpg with the gif i got errors:

Code:
#!/usr/bin/perl

$dir_name = "/home/casper/public_html"; 

opendir (DIR, $dir_name) || die "Error in opening dir $dirname\n"; 
while ( ($filename = readdir (DIR))) { 
        if ($filename =~ /\w+\.gif|jpg/){
                system("cp $filename /home/casper/public_html/images/"); 
        }       
        sleep 1;  
}
I just put an or bracket in the regex - it should work - but it does not.

Is there some other way to ake the regex?
 
Old 02-22-2012, 11:45 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your regex looks fine. Do the filenames contain whitespace? If so, you will need to enclose them in quotes in your system() commandline.
BTW, when you say 'I got errors', it would be more helpful to show specifically what errors you got.
--- rod.

Last edited by theNbomr; 02-22-2012 at 11:46 AM.
 
Old 02-22-2012, 11:47 AM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Also --

Quote:
Originally Posted by casperdaghost
however when i combines the jpg with the gif i got errors
What errors?

You should have the use warnings pragma in your Perl script.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Postscript to .gif or .jpg conversion program? stf92 Slackware 7 08-23-2011 10:42 PM
[SOLVED] GIF and JPG file formats. stf92 Linux - Newbie 2 08-19-2011 02:58 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 02:10 AM
[SOLVED] Read file names in a Dir and its Sub Dir in Perl sagarkha Programming 2 04-28-2010 05:58 PM
jpg to gif conversion damonj Linux - General 2 11-03-2008 07:13 AM


All times are GMT -5. The time now is 01:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration