2010-09-21 175 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