3

I have a Perl program which does something like below:

#!/usr/bin/env perl    
use strict;
use warnings;

my $exe = "C:\\project\\set_env_and_run.bat";

my $arg1 = "\\\\Server\\share\\folder1";    
my $arg2 = "D:\\output\\folder1";

my $cmd = "$exe \"$arg1\" \"$arg2\"";    
my $status = system("$cmd > c:\\tmp\\out.txt 2>&1");

print "$status\n";

I am calling this Perl code in an eval block. When invoked, i get the status printed as 0, but the batch file has not actually executed. What would be the reason for this? Any issue with the 'system' call coded above?

Thanks, Jits

djangofan
  • 28,471
  • 61
  • 196
  • 289
Jithesh
  • 39
  • 1
  • 1
  • 3

4 Answers4

1

I would say that you should define exe like this:

my $exe = "cmd.exe /c C:\\project\\set_env_and_run.bat";
djangofan
  • 28,471
  • 61
  • 196
  • 289
1

you could use

 system ("start C:\\project\\set_env_and_run.bat");
avinashkr
  • 512
  • 1
  • 5
  • 20
1

You need to escape your backslashes inside of double quotes.

my $exe = "C:\\project\\set_env_and_run.bat";
...
my $status = system("$cmd > c:\\tmp\\out.txt 2>&1");
mob
  • 117,087
  • 18
  • 149
  • 283
  • Sorry, i had put the sample code and missed escaping the backslashes! have corrected it above. – Jithesh May 25 '10 at 19:08
  • You can use proper (forward) slashes in file paths in Win32 perl? `my $exe = 'C:/project/set_env_and_run.bat';` will work quite nicely. – daotoad May 25 '10 at 20:08
1

Are you sure the bat file isn't running. I have taken your code, fixed up the paths that don't exist on my machine. I get it to call the batch file

echo In myrun  1=%1  2=%2

And it writes the following to the output file

 In myrun  1="\\Server\share\folder1"  2="D:\output\folder1"
justintime
  • 3,601
  • 4
  • 22
  • 37