Perl文件处理问题? [英] Perl File Handling question?
问题描述
我在output.txt
中具有所有文件的名称,但是在output.txt
文件中没有folderpath
.例如,如果filename = test.txt
及其folderpath=/usr/local/bin
比output.txt
文件中的小,我只有filename as test.txt
I have names of all files in output.txt
but I do not have folderpath
in output.txt
file. For Example, if filename = test.txt
and its folderpath=/usr/local/bin
than in output.txt
file, I only have filename as test.txt
output.txt
文件有许多filename
条目,我想做的是:
output.txt
files has many entries for filename
, what I am trying to do is:
-
将所有
output.txt
中存在的所有文件移动到另一个文件夹,例如"/usr/local/test/data/",所以我的问题是如何使用Perl Script
来做到这一点?
Move all files present
output.txt
to some another folder say '/usr/local/test/data/', so my question is how can I do that usingPerl Script
?
使用folderpath
从实际位置删除output.txt
文件中存在的所有文件,因此,例如,脚本应首先将test.txt
移动到其他文件夹位置,例如/usr/local/bin/test.txt
Delete all the files which are present in output.txt
file from the actual location, using their folderpath
, so for example, script should first move test.txt
to some other folder location, say, /usr/local/test/data/test.txt
and delete test.txt from its actual location using folderpath information, i.e., script should delete /usr/local/bin/test.txt
任何建议将不胜感激.
Any suggestions would be highly appreciated.
更新: 下面的脚本读取output.txt文件,并获取每个文件的大小以及output.txt中存在的所有文件的总大小
Updates: Below scripts reads the output.txt file and gets size of each file and total size of all the files present in the 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";
礼貌: RickF 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 ý
;软件交付与实现(Div-61)数据IPS 08-20-2010 v3.xlsm
My output.txt file also have some files whose name contain special characters, for example,
63551_106640_63551 IBMý
;Software Delivery&Fulfillment(Div-61) Data IPS 08-20-2010 v3.xlsm
如果在上面看到文件的编码值为ý
,但是我的文件名具有实际的特殊字符符号,因此将复制或移动该文件也考虑在内并移动文件.
If you see above the file has encoded value of ý
but my filename has actual special character symbol for that and so will copy or move take my this file also into account and move the files.
推荐答案
One suggestion would be to use perl's rename
function. You can use this to move files like so:
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";
}
这篇关于Perl文件处理问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!