ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
#!/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 #!
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.
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"
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.
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.
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.)
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.