Bic*_*Bic 1 perl file file-find
我在文本文件上使用File :: Find和file i/o来解析一系列目录并将内容移动到一个新文件夹中.这是一个简单的脚本(见下文):
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use File::Copy;
my $dir = "/opt/CollectMinderDocuments/coastalalglive"; #base directory for Coastal documents
#read file that contains a list of closed IDs
open(MYDATA, "Closed.txt");
mkdir("Closed");
while(my $line = <MYDATA>) {
chomp $line;
my $str = "$dir" . "/Account$line";
print "$str\n";
find(\&move_documents, $str);
}
sub move_documents {
my $smallStr = substr $File::Find::name, 43;
if(-d) {
#system("mkdir ~/Desktop/Closed/$smallStr");
print "I'm here\n";
system("mkdir /opt/CollectMinderDocuments/coastalalglive/Closed/$smallStr");
#print "Made a directory: /opt/CollectMinderDocuments/coastalalglive/Closed/$smallStr\n";
}
else {
print "Now I'm here\n";
my $smallerStr = substr $File::Find::dir, 43;
my $temp = "mv * /opt/CollectMinderDocuments/coastalalglive/Closed/$smallerStr/";
system("$temp");
}
}
Run Code Online (Sandbox Code Playgroud)
文本文件包含一个数字列表:
1234
2805
5467
Run Code Online (Sandbox Code Playgroud)
我上个月执行它时代码工作,但它现在返回"找不到文件或目录"错误.实际错误是"No such file or directoryerDocuments/coastalalglive/Account2805".我知道它正在搜索的所有目录都存在.我已手动输入其中一个目录,脚本执行正常:
find(\&move_documents, "/opt/CollectMinderDocuments/coastalalglive/Account2805/");
Run Code Online (Sandbox Code Playgroud)
我不确定为什么会返回错误.在此先感谢您的帮助.
你的错误:
"No such file or directoryerDocuments/coastalalglive/Account2805"
Run Code Online (Sandbox Code Playgroud)
似乎暗示有一个\r没有被你的删除chomp.在不同文件系统之间传输文件时会发生这种情况,其中文件包含\r\n行结尾.真正的错误字符串将是这样的:
/opt/CollectMinderDocuments/coastalalglive/Account2805\r: No such file or directory
Run Code Online (Sandbox Code Playgroud)
请尝试更改chomp $line为$line =~ s/[\r\n]+$//;,然后查看是否有效.
也:
my $temp = "mv * /opt/CollectMinderDocuments/coastalalglive/Closed/$smallerStr/";
system("$temp");
Run Code Online (Sandbox Code Playgroud)
是非常错的.该循环中的第一个非目录文件将移动所有剩余的文件(包括目录?不确定是否mv默认执行此操作).因此,子程序的后续迭代将找不到任何移动,也导致"未找到"类型错误.虽然没有人被perl捕获,因为你使用system而不是File::Copy::move.例如:
move $_, "/opt/CollectMinderDocuments/coastalalglive/Closed/$smallerStr/" or die $!;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3380 次 |
| 最近记录: |