我正在做以下步骤:
它只适用于一个文件,但不再适用.这是我的代码:
opendir DIR, ".";
my @files = grep {/\.txt/} readdir DIR;
foreach my $files (@files) {
@fn = split '\.', $files;
mkdir "$fn[0]"
or die "Unable to create $fn[0] directory <$!>\n";
rename "$files", "Test.txt";
require "test3.pl";
rename "Test.txt", "$files";
system "move $files $fn[0]";
}
Run Code Online (Sandbox Code Playgroud)
任何帮助都会非常感激.
该require函数是智能的,只有在尚未加载时才加载代码.为什么这很棒?(1.)我们没有条件包含的C预处理器地狱,(2)我们节省时间.
要执行另一个perl脚本,我们有各种各样的可能性.你可能想要的那个,就是do一个脚本.该do FILENAME语法是一样的eval,除了你的范围不是可见的doNE文件.
你也可以通过开始另一个翻译system "./test3.pl".
您可以创建test3.pl一个模块,例如,使用package test3;,并将内容打包成一个sub.您可能会将当前文件名作为参数传递,而不是对文件名进行硬编码.这不仅是更好的编码实践,还允许您更轻松地扩展应用程序,例如,进行多线程.
以下是我实现该代码段的方法:
use test3; # load the file once
foreach my $file (glob "*.txt") { # use `glob` to get a list of matching filenames
(my $newdir) = $file =~ m/^ (.+) \.txt $/x; # what you probably wanted
mkdir $newdir
or die "Unable to create $newdir directory <$!>\n";
test3::the_sub($file); # do the action on $file, without renaming circus.
system "move $file $newdir"; # rather use File::Copy
}
Run Code Online (Sandbox Code Playgroud)
有趣的一点:
glob 很棒,就像在shell中一样.
总是my用于变量,除非你有充分的理由.
我进一步假设该文件text.en.txt不应该创建目录text,但是text.en,该文件.txt不存在.
这是否有效还取决于您调用的脚本.
| 归档时间: |
|
| 查看次数: |
189 次 |
| 最近记录: |