我想找出一種方法來組織 Perl文件夾中的文件。我的腳本的基礎很簡單。該腳本讀取根文件夾中的 子文件夾,遍歷每個文件夾,並使用 簡單比較文件名中的前3位數字和3 數字文件夾名稱。如果它們不匹配,則將其放入正確的 的地方。我怎麼能自動增加一個文件,並將其重置
我的問題是,雖然通過文件查看,我有時運行 成重複的訂單號。由於訂單已存在,我不能覆蓋原始文件,但由於顯而易見的原因,兩個文件不能存在於 相同的地方。所以我想出了這個想法 追加文字_TEMP
到文件名的末尾,以便將它們移動 ,他們可以稍後重命名。我現在遇到的問題 是當我已經有兩個重複。我正在尋找一種方式來 允許TEMP標記每次使用時增加1,然後 重置爲零,每次循環再次開始。我只是不太確定 我應該在哪裏實施這個想法。
下面是該腳本的主程序:
foreach my $office (keys %office_names) {
make_junk_folder($office);
# The matches and unmatches come back as array references!
my ($returned_matches, $returned_unmatches) = read_root_folder($office);
foreach my $folder (@$returned_matches) {
my $returned_files = read_subfolder($office, $folder);
foreach my $file (@$returned_files) {
analyze_file($office,$folder,$file);
}
}
foreach my $folder (@$returned_unmatches) {
print "$folder\n";
remove_root_junk($office,$folder);
}
}
這裏是處理文件的移動和重命名子程序:
sub analyze_file {
my $office = shift;
my $folder = shift;
my $file = shift;
my $order_docs_path = $office_names{$office};
if ($file =~ /^(?<office> (C[AFL]|ME)) (?<folder_num> \d{3})
(?<file_num> \d{3}) ([_|\-] \d+)? \. (?<file_ext> pdf)
$/xmi) {
my $file_office = uc($+{office});
my $folder_num = $+{folder_num};
my $file_num = $+{file_num};
my $file_ext = lc($+{file_ext});
# Change hyphens to a underscore
$file_num =~ s/\-/_/;
my $file_name = "$file_office" . "$folder_num" . "$file_num" .
"\." . "$file_ext";
my $temp_name = "$file_office" . "$folder_num" . "$file_num" .
"_TEMP" . "\." . "$file_ext";
if ($folder != $folder_num) {
# If the folder does not exist create the folder
if (! -e "$order_docs_path\\$folder_num") {
system "mkdir $order_docs_path\\$folder_num";
}
# Check to see if the file already exists
if (-e "$order_docs_path\\$folder_num\\$file_name") {
# Append the file with a "_TEMP". These files are
# missorted pages belonging to a larger document
rename ("$order_docs_path\\$folder\\$file",
"$order_docs_path\\$folder_num\\$temp_name");
} else {
# Moves the file to correct place, these are mismatched files
rename ("$order_docs_path\\$folder\\$file",
"$order_docs_path\\$folder_num\\$file_name");
}
} else {
# Files are in the correct place, the file name will be
# corrected only
rename ("$order_docs_path\\$folder\\$file",
"$order_docs_path\\$folder_num\\$file_name");
}
}
}
一些示例文件名看起來像這樣:
CF100145.pdf
CA310244.pdf
CL211745.pdf
CL211745_1.pdf(這是第二頁上,從我們的文檔掃描儀)
ME102103.pdf
哪裏出現問題是當相同的工作發生變化,而不是將文件放在應該到達的地方,人將更新後的作業信息放入當前作業文件夾,該目錄與文件中的前3個數字不匹配。所以後來他們必須進行排序才能解決錯誤,問題是在一個辦公室裏有超過50萬份文件,而我們有4個辦事處。
我不認爲這'[_ | \ - ]'做什麼,你認爲它。它匹配下劃線,管道或連字符。我懷疑你的意思是'[_-]'匹配下劃線或連字符。 –
儘管可以從正則表達式中推導出文件名,但如果給出一些示例,則會更容易。 – TLP
當然,我可以舉一些例子,我不確定需要什麼。 – ianc1215