LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-16-2010, 10:14 AM   #1
New_user_perl
LQ Newbie
 
Registered: Aug 2010
Posts: 7

Rep: Reputation: 0
Question Problem with a Perl script


Hi,
I have a perl script which reads a list of files from a directory. The next step I have to do is to invoke a command line argument for an apllication and pass these files one by one to that argument. In detail.

The files I am reading are image files. Next I need to process these images using an application called FITS. This application tell whether this is a valid image or not. The command line argument we need to use is

FITS.BAT -i "location of the file followed by file name".

The below is the script I did till now. I am reading the image files from the directory. And one important thing is this command takes only one file at a time. I have an idea that we can use SYSTEM command to invoke an application. I tried it but it is not invoking anything.

#!/usr/local/bin/perl

$log=$ARGV[0];


# create a list of all *.JPG files in
# the current directory
opendir (DIR, 'C:\Documents and Settings\user\Desktop\IMD025350802\IMD025350802\IMAGES');
@files = grep(/\.jpg$/,readdir(DIR));
closedir(DIR);

# print all the filenames in our array
foreach $file (@files) {
print "$file\n";
}

Thanks,
Pallavi

EMERGENCY PLEASE !
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 08-16-2010, 10:31 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402
Hi,

Would this help:

Code:
#!/usr/local/bin/perl

$log = $ARGV[0] ;

# create a list of all *.JPG files in
# the current directory
opendir( DIR, 'C:\Documents and Settings\user\Desktop\IMD025350802\IMD025350802\IMAGES' ) ;
@files = grep( /\.jpg$/, readdir( DIR ) ) ;
closedir( DIR ) ;

# print all the filenames in our array
foreach $file ( @files ) {
#   print "$file\n" ;
   @args = ( "FITS.BAT", "-i", "$file" ) ;
   system( @args ) == 0 or die "system @args failed: $?";
I don't know too much about windows, maybe the full path to FITS.BAT is needed.

Hope this helps.

Last edited by druuna; 08-16-2010 at 11:02 AM. Reason: corrected #!
 
1 members found this post helpful.
Old 08-16-2010, 11:01 AM   #3
New_user_perl
LQ Newbie
 
Registered: Aug 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Hi,
I tried to execute the above script. But it is neither executing nor showing any error message. When I ran this script the command line came back to the path where I need to execute the script.

Thanks
 
Old 08-16-2010, 11:04 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402
Hi,

I just edited my previous post.

My perl is found in /usr/bin/perl, yours in /usr/local/bin/perl.

Change: #!/usr/bin/perl to #!/usr/local/bin/perl

Uncomment the # print "$file\n" ; line to see output.
 
Old 08-16-2010, 11:13 AM   #5
New_user_perl
LQ Newbie
 
Registered: Aug 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Hi,
Thank you for the reply. Now I could see some output but not the one I am expecting. The Fits.BAT -i is working but not processing the images because the input file should be with its location. For example

FITS.BAT -i " C:\Documents and Settings\user\Desktop\IMD025350802\IMD025350802\IMAGES\IMD020405_0001.JPG"

If it is declared as above then it will start processing. So is there a way I could get filenames with their path into "$file"


Thanks
 
Old 08-16-2010, 11:14 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
try to call FITS.BAT with this form instead:
Code:
C:\windows\system32\cmd.exe /c FITS.BAT -i ...
Edit: I guess this is no longer needed since you're already able to properly run FITS.BAT.

Last edited by konsolebox; 08-16-2010 at 11:19 AM.
 
Old 08-16-2010, 11:18 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402
Hi,

Give konsolebox' advise a try.

This might also help:

Change:
@args = ( "FITS.BAT", "-i", "$file" ) ;
to
@args = ( "FITS.BAT", "-i", "C:\Documents and Settings\user\Desktop\IMD025350802\IMD025350802\IMAGES\$file" ) ;

Hope this helps.
 
1 members found this post helpful.
Old 08-16-2010, 11:22 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Perhaps you need to prepend DIR to $file.

Edit: Again this is no longer needed since druuna already made the suggestion. I always make a reply after somebody else makes a post. I should practice using the preview button.

Last edited by konsolebox; 08-16-2010 at 11:24 AM.
 
Old 08-16-2010, 11:33 AM   #9
New_user_perl
LQ Newbie
 
Registered: Aug 2010
Posts: 7

Original Poster
Rep: Reputation: 0
HI All,
Thank you so much for your help it is working now. But I have one last question. The location of these files changes I mean
opendir( DIR, 'C:\Documents and Settings\user\Desktop\IMD025350802\IMD025350802\IMAGES'

In the above path till IMD025350802 it is constant after that so many image folders may be added each day like IMD025350802, IMD025350905,IMD025350775..... and again images is
common so how can I declare this changing location in my open dir. So it has to go that location and read all the folders available there.

Thanks
 
Old 08-16-2010, 11:33 AM   #10
PhantasyConcepts
LQ Newbie
 
Registered: May 2009
Posts: 19

Rep: Reputation: 3
First of all, remember, spaces are breaks between arguments. Therefore, the C:\Documents and settings portion of the filename will be three separate arguments to the command line. Remember to put double quotes around $FILE in the command line. Now when you want to execute the command line, there are two ways of doing it. One is with the exec() command, which is annoying in syntax, and the other is with the backtick method. The backtick is the key just to the left of the 1. Do this command line inside your foreach loop:

<code>`FITS.BAT -i "$FILE"`</code>

That assumes, of course, that the Perl interpreter knows where to find FITS.BAT. If not, and to be certain, put the entire path in on the command (or set up a variable to hold the full path to FITS.BAT like the following:<code>CMD="C:\Program Files\lalalal\wowowowo\FITS.BAT"</code> but remember that any path that includes spaces requires the double-quote method to put things into perspective.)
 
Old 08-16-2010, 12:11 PM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402
Hi,

Quote:
Originally Posted by New_user_perl View Post
In the above path till IMD025350802 it is constant after that so many image folders may be added each day like IMD025350802, IMD025350905,IMD025350775..... and again images is
common so how can I declare this changing location in my open dir. So it has to go that location and read all the folders available there.
I'm not entirely sure what you are after, hopefully it is this:
Code:
#!/usr/local/bin/perl

# get both command line parameter
$inputADir  = shift ;
$inputBDir  = shift ;

# set base dir
$baseDir   = "C:\Documents and Settings\user\Desktop" ;

# glue all togeter
$targetDir = "$baseDir\$inputADir\$inputBDir\IMAGES" ;

$log = $ARGV[0] ;

# create a list of all *.JPG files in
# the current directory
opendir( DIR, "$targetDir" ) ;
@files = grep( /\.jpg$/, readdir( DIR ) ) ;
closedir( DIR ) ;

# print all the filenames in our array
foreach $file ( @files ) {
   print "$targetDir\$file\n" ;
   @args = ( "ls", "-l", "$targetDir\$file" ) ;
   system( @args ) == 0 or die "system @args failed: $?" ;
}
Now you can run the script with a parameter: fits.pl IMD025350802 IMD025350808 and this path will be examined:
C:\Documents and Settings\user\Desktop\IMD025350802\IMD025350808\IMAGES

Hope this helps.

Last edited by druuna; 08-16-2010 at 12:12 PM. Reason: Fixed backslash vs forward slash.
 
1 members found this post helpful.
Old 08-16-2010, 12:58 PM   #12
New_user_perl
LQ Newbie
 
Registered: Aug 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Hi,
I think in my above post I did not make it clear . The thing is:

I have a location for example: C:\Documents and Settings\user\Desktop\ New Folder. To this location everyday some new folders will be added.(likeIMD025350802 ,IMD025350808...). These IMD folders again consists of some sub folders out of these sub folders I need to read only one sub folder by name images and process them against a tool.

So in my script I can hard code till C:\Documents and Settings\user\Desktop\New Folder but after that I want the script to dig into all the avaiable "IMD" folders one by one grab the image folder and read the files from the image folder.

So can I specify something like this C:\Documents and Settings\user\Desktop\New Folder\IMDB?????\Images. Will this work.

Thanks
 
Old 08-16-2010, 02:00 PM   #13
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
It would really be a lot easier if just you use .cmd scripting instead.
Code:
@echo off

for /f "delims=" %%a in ('dir /s/b/a-d *.jpg') do (
    call "<path to FITS.BAT>" -i %%a
)
This I assume that your filenames (both of files and directories) does not contain any spaces. If it does, I don't know if it will work with ... -i "%%a".

You need to save it as a .cmd script and place it in 'New Folder'. And then you can run it like this:
Code:
cmd /c process.cmd

Last edited by konsolebox; 08-16-2010 at 02:02 PM.
 
Old 08-16-2010, 02:03 PM   #14
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402
Hi again,

This should traverse a directory structure and look for jpg files. I left out the execution of the FIST bat file, that is up to you.
Code:
#!/usr/local/bin/perl
use File::Find ;

# Get the directory from the command line
# or use the default directory
$search = shift || 'C:\Documents and Settings\user\Desktop' ;

# Get an array of all subdirectories
find sub { push @dirs, $File::Find::name if -d }, $search ;

for $dir ( @dirs ) {
   opendir $dh, $dir or do { warn "Cannot open '$dir' $!" ; next ; } ;

   opendir( DIR, "$dir" ) ;
   @files = grep( /\.jpg$/, readdir( DIR ) ) ;
   closedir( DIR ) ;

   foreach $file ( @files ) {
      print "$dir/$file\n" ;

      # rest goes here
   }

   closedir( $dh ) ;

}
Do check to see if I made any mistakes with the forward/backward slashes in the directories and joining. I'm not used to windows at all....

BTW: It gives you the possibility to enter a different directory. If you don't give anything at the command line it falls automatically back to the default directory.

Hope this helps.

Last edited by druuna; 08-16-2010 at 02:24 PM. Reason: Added missing closedir for syntax completeness
 
2 members found this post helpful.
Old 08-16-2010, 02:14 PM   #15
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


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
problem with perl modules declaration and perl/cgi script shifter Programming 9 02-24-2010 10:09 AM
Problem Getting A Perl Script to Run kaplan71 Linux - General 8 06-20-2008 06:19 PM
cron: problem with perl script Clemente Linux - Software 4 05-10-2006 03:30 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 11:22 AM
Problem with a perl script Nexis Linux - Software 3 08-08-2002 09:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:07 AM.

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
Open Source Consulting | Domain Registration