2010-09-21 115 views
1

我有output.txt中的所有文件的名稱,但我沒有folderpathoutput.txt文件中。例如,如果filename = test.txtfolderpath=/usr/local/binoutput.txt文件,我只有filename as test.txtPerl文件處理問題?

output.txt文件有filename很多項目,我所要做的是:

  1. 移動所有文件目前output.txt到有些另一個文件夾說'/ usr/local/test/data /',所以我的問題是我該如何使用Perl Script

  2. 刪除其存在於與實際位置output.txt文件,所有文件都使用他們的folderpath,因此,例如,腳本應首先移動test.txt到其他文件夾的位置,也就是說,/usr/local/test/data/test.txt並從其實際刪除的test.txt位置使用folderpath信息,即腳本應該刪除/usr/local/bin/test.txt

任何建議將不勝感激。

更新: 下面的腳本讀取output.txt的文件,並獲取所有存在於output.txt的

my $folderpath = 'the_path'; 
open my $IN, '<', 'path/to/infile'; 
my $total; 
while (<$IN>) { 
    chomp; 
    my $size = -s "$folderpath/$_"; 
    print "$_ => $size\n"; 
    $total += $size; 
} 
print "Total => $total\n"; 

禮貌的文件中的每個文件的大小和總大小:RickFAnswer

Output.txt

2304_2328_Test Data November 19 2003 - Test Tele.xls 
2344_2341_Data Dummy Test 12-17.doc 
2343_2342_Dummy Test Test 12-17.doc 

我output.txt的文件中也有他的名字包含特殊字符的一些文件,例如, 63551_106640_63551 IBM &#253;軟件交付&履行(DIV-61)數據IPS 2010年8月20日v3.xlsm

如果您看到上面的文件編碼值爲&#253,但我的文件名具有實際的特殊字符符號,因此將複製或移動我的這個文件也考慮到並移動文件。

+0

你能告訴你'output.txt'文件的幾行,以及迄今爲止腳本的內容(如果你已經開始寫作還沒完成)? – 2010-09-21 14:22:12

+0

如果你想引用unix中的文件,你使用「?」在文件名,或特殊字符? – Powertieke 2010-09-22 07:28:19

回答

1

一個建議是使用perl的rename函數。你可以用它來舉動文件,像這樣:

use 5.012; 
use warnings; 

my $folderpath = '/some/where/'; # is it really "/usr/local/bin"? be careful now!! 
my $tofolder = '/usr/local/test/data'; # must have permissions to use this and $folderpath 

my @files = ('test.txt', 'someotherfile.txt'); 

for my $file (@files) { 
    rename "$folderpath/$file", "$tofolder/$file" 
     or warn "Couldn't move $file"; 
} 
+2

'File :: Copy'有一個可能更安全的'move'功能。 – 2010-09-21 14:48:03

+0

@Platinum Azure:你能舉一個例子嗎? – Rachel 2010-09-21 14:53:52

+0

回覆:@Platinum:http://perldoc.perl.org/File/Copy.html – RickF 2010-09-21 15:09:25