Perl文件处理问题? [英] Perl File Handling question?

查看:74
本文介绍了Perl文件处理问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在output.txt中具有所有文件的名称,但是在output.txt文件中没有folderpath.例如,如果filename = test.txt及其folderpath=/usr/local/binoutput.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:

  1. 将所有output.txt中存在的所有文件移动到另一个文件夹,例如"/usr/local/test/data/",所以我的问题是如何使用Perl Script来做到这一点?

  1. Move all files present output.txt to some another folder say '/usr/local/test/data/', so my question is how can I do that using Perl Script ?

使用folderpath从实际位置删除output.txt文件中存在的所有文件,因此,例如,脚本应首先将test.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 &#253;软件交付与实现(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&#253;Software Delivery&Fulfillment(Div-61) Data IPS 08-20-2010 v3.xlsm

如果在上面看到文件的编码值为&#253,但是我的文件名具有实际的特殊字符符号,因此将复制或移动该文件也考虑在内并移动文件.

If you see above the file has encoded value of &#253 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.

推荐答案

一个建议是使用perl的

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆