Running a batch file with Perl (activate perl on Windows)

I have a Perl program that 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 call this Perl code in an eval block. When I call, I get the status printed as 0, but the batch file is actually not executed. What is the reason for this? Any problem with the "system" code encoded above?

Thanks Jits

+3
source share
4 answers

You need to avoid the backslash inside double quotes.

my $exe = "C:\\project\\set_env_and_run.bat";
...
my $status = system("$cmd > c:\\tmp\\out.txt 2>&1");
+1
source

Are you sure the bat file is not running. I took your code, fixed paths that do not exist on my machine. I get it to call a batch file

echo In myrun  1=%1  2=%2

And he writes the following to the output file

 In myrun  1="\\Server\share\folder1"  2="D:\output\folder1"
+1

, exe :

my $exe = "cmd.exe /c C:\\project\\set_env_and_run.bat";
+1

 system ("start C:\\project\\set_env_and_run.bat");
0

Source: https://habr.com/ru/post/1747069/


All Articles