在CGI中运行DOS命令时出错



我尝试使用Perl 将一个文件的简单副本运行到另一个文件夹

system("copy template.html tmp/$id/index.html");

但我得到了错误:The syntax of the command is incorrect.

当我将其更改为时

system("copy template.html tmp\$id\index.html");

系统将另一个文件复制到之后的tmp$id

有人能帮我吗?

我建议您使用File::Copy,它与您的Perl发行版一起提供。

use strict; use warnings;
use File::Copy;
print copy('template.html', "tmp/$id/index.html");

您不需要担心Windows上的斜杠或反斜杠,因为模块会为您处理这些问题。

请注意,您必须从当前工作目录设置相对路径,因此template.html和目录tmp/$id/都需要在那里。如果您想动态创建文件夹,请查看File::Path


更新:回复下面的评论。

您可以使用此程序创建文件夹,并使用ID的就地替换来复制文件。

use strict; use warnings;
use File::Path qw(make_path);
my $id = 1; # edit ID here
# Create output folder
make_path("tmp/$id");
# Open the template for reading and the new file for writing
open $fh_in, '<', 'template.html' or die $!;
open $fh_out, '>', "tmp\$idindex.html" or die $!;
# Read the template
while (<$fh_in>) {
  s/ID/$id/g;       # replace all instances of ID with $id
  print $fh_out $_; # print to new file
}
# Close both files
close $fh_out;
close $fh_in;

最新更新