代码之家  ›  专栏  ›  技术社区  ›  Rachel

Perl文件处理问题?

  •  1
  • Rachel  · 技术社区  · 14 年前

    output.txt 但我没有 folderpath 在里面 输出.txt 文件。例如,如果 filename = test.txt 以及它的 folderpath=/usr/local/bin 比在 输出.txt 文件,我只有 filename as test.txt

    输出.txt 文件有许多项 filename

    1. 移动所有存在的文件 输出.txt Perl Script ?

    2. 删除中存在的所有文件 从实际位置,使用 文件夹路径 ,例如,脚本应该首先移动 test.txt 到其他文件夹的位置, /usr/local/test/data/test.txt 删除测试.txt从它的实际位置使用folderpath信息,即脚本应该删除 /usr/local/bin/test.txt

    下面的脚本读取输出.txt并获取每个文件的大小以及输出.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 Answer

    输出.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
    

    我的输出.txt文件还有一些文件的名称包含特殊字符,例如, 63551_IBM &#253 ;软件交付和实现(Div-61)数据IPS 08-20-2010 v3.xlsm

    如果你看到上面的文件有编码值 & 但我的文件名有实际的特殊字符符号,所以将复制或移动我的这个文件也考虑和移动文件。

    1 回复  |  直到 7 年前
        1
  •  1
  •   AndyG    7 年前

    一个建议是使用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";
    }