Re: calling .EXE through Perl script
by holli (Abbot) on May 11, 2006 at 10:13 UTC
|
You don't need Perl for that:
C:\> for %f in (test\*.txt) do youprogram.exe %f
The windows shell may not be very powerful, but for such simple stuff it's usable.
| [reply] [d/l] |
Re: calling .EXE through Perl script
by Zaxo (Archbishop) on May 11, 2006 at 09:36 UTC
|
Another way to list all the files in a directory is with glob. It might be a good idea to filter that list so that only regular files are taken.
my @files = grep { -f } glob '/path/to/files/*';
for my $file (@files) {
my $output = `/path/to/parser.exe $file`;
# do stuff with $output
}
Is your problem as simple as calling backticks in void context, without collecting the result?
System programs can also be launched with system, open, or fork-exec. Which you choose depends on what you want to do with them.
| [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: calling .EXE through Perl script
by GrandFather (Saint) on May 11, 2006 at 08:32 UTC
|
On the face of it your syntax is fine. Is parser.exe on the search path or in teh current directory?
As a test you may care to try `notepad.exe` instead.
You can iterate over the files in a folder like this:
use strict;
use warnings;
my $scan;
opendir $scan, '.';
while (my $entry = readdir ($scan)) {
next if ! -f $entry; # Skip if not a file
print "$entry\n";
}
DWIM is Perl's answer to Gödel
| [reply] [d/l] [select] |
|
|
Thanks for quick reply. Parser.exe is in current directory. I wanted to send the file name to my C program so that it can execute further opening that file. I am not able to send the file name to my C pgm now.
| [reply] |
Re: calling .EXE through Perl script
by dokkeldepper (Friar) on May 11, 2006 at 08:39 UTC
|
untested, sorry have no system here to do it, but it should give you the direction.
See also:
-
File::Find
-
Cookbook 2nd ed. 16.2. Running another program
use File::Find;
find(\&run, '.');
sub run{
system("bla.exe $File::Find::name");
}
| [reply] [d/l] |
|
|
return if ! -f $File::Find::name;
For scanning a single directory it is probably easier to simple use opendir/readdir as shown elsewhere.
DWIM is Perl's answer to Gödel
| [reply] [d/l] [select] |
Re: calling .EXE through Perl script
by Anonymous Monk on May 11, 2006 at 09:44 UTC
|
Here is my script which i tried...
use strict; use warnings;
$dir = "C:\\parser_pgm\\test";
opendir(TXT, $dir) or die "Can't open $dir: $!";
while( defined ($file = readdir TXT) ) {
`parser_pgm.exe $file`;
}
closedir(XML);
| [reply] |
|
|
$dir = "C:\\parser_pgm\\test";
opendir(TXT, $dir) or die "Can't open $dir: $!";
while( defined ($file = readdir TXT) ) {
print `parser_pgm.exe $file`;
}
closedir(TXT); # not XML
Is that more like what you expect?
| [reply] [d/l] |
|
|
How are you running this script? If you run it from the command line, you'll get these errors:
Global symbol "$dir" requires explicit package name at test.pl line 4.
Global symbol "$dir" requires explicit package name at test.pl line 5.
Global symbol "$dir" requires explicit package name at test.pl line 5.
Global symbol "$file" requires explicit package name at test.pl line 6
+.
Global symbol "$file" requires explicit package name at test.pl line 7
+.
Execution of test.pl aborted due to compilation errors.
If you're running the program by double-clicking on the file in Windows Explorer, you're probably not going to see the errors before the DOS box closes.
I think this code is what you want: use strict;
use warnings;
my $dir = "C:\\parser_pgm\\test";
opendir(DIR, $dir) or die "Can't open $dir: $!";
while(my $file = readdir DIR) {
my $pathname = "$dir\\$file";
if (-f $pathname) {
# I added the -f test after running this and finding
# out that readdir returns "." and ".."
# (At least on the version of ActivePerl I am using)
print STDERR "running parser_pgm.exe on $pathname\n"; # for debugg
+ing
system("parser_pgm.exe \"$pathname\"");
# I added quotes in case there are spaces in the
# file name (or in directory name in case you change it.
if ($? != 0) {
print STDERR "parser_pgm returned error $?\n";
}
}
}
closedir(DIR);
| [reply] [d/l] [select] |
|
|
Hi Thelonius, Thanks a lot for your help. Indeed your code works. And as suggested by other monks I was not reading the output and hence was not aware if script really did the job.
Thanks one and all for the needful.
Regards Ashutosh
| [reply] |
|
|
use strict;
use warnings;
$dir = "C:\\parser_pgm\\test";
opendir(TXT, $dir) or die "Can't open $dir: $!";
while( defined ($file = readdir TXT) ) {
next if ! -f $file; # Skip if not a file
`parser_pgm.exe $file`;
}
closedir(TXT);
Note also that I've changed the closedir(XML); to closedir(TXT); to match the opendir. The defined check is redundant by the way.
DWIM is Perl's answer to Gödel
| [reply] [d/l] [select] |