简体   繁体   English

从Perl运行批处理文件(Windows中的Activestate perl)

[英]Running a batch file from Perl (Activestate perl in Windows)

I have a Perl program which does something like below: 我有一个Perl程序,它执行如下操作:

#!/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. 我在eval块中调用此Perl代码。 When invoked, i get the status printed as 0, but the batch file has not actually executed. 调用时,我将状态打印为0,但批处理文件尚未实际执行。 What would be the reason for this? 这是什么原因? Any issue with the 'system' call coded above? 以上编码的“系统”调用的任何问题?

Thanks, Jits 谢谢,Jits

我会说你应该像这样定义exe:

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

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");

Are you sure the bat file isn't running. 你确定bat文件没有运行。 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"

你可以用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM