我尝试使用 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\\$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;