2012-07-30 98 views
0

一个DOS命令,当我试图运行一个文件的简单复制到使用Perl错误运行在CGI

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 foler

有人可以帮助我吗?

回答

3

我建议你使用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\\$id\index.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; 
+0

感谢您的帮助! 我有* nix系统上运行的cgi文件,但我想在Windows上运行它。你可以将这段代码更改为在IIS窗口上运行的新代码吗? ... $ commands [1] =“cp template.html tmp/$ id/index.html; cat tmp/$ id/index.html | sed -e's/ID/$ id/g'> a ; mv a tmp/$ id/index.html“; system($ commands); – Shaman 2012-07-30 11:50:20

+0

它只是一个cgi文件,用于将某些文档类型转换为swf并在线查看。我下载了这个源代码,但它属于* nix。我很难在Windows系统上运行它 – Shaman 2012-07-30 12:20:18

+1

它不是一个CGI文件,它是一个Perl程序。你不需要通过CGI/web服务器来运行它。你的电脑上有Perl,对吧? – simbabque 2012-07-30 13:10:25