Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl文件处理问题?_Perl_File_Scripting - Fatal编程技术网

Perl文件处理问题?

Perl文件处理问题?,perl,file,scripting,Perl,File,Scripting,我在output.txt文件中有所有文件的名称,但在output.txt文件中没有folderpath。例如,如果filename=test.txt及其folderpath=/usr/local/bin大于output.txt文件中的值,则我只有文件名为test.txt output.txt文件有许多关于filename的条目,我想做的是: 将存在的所有文件output.txt移动到另一个文件夹中,比如“/usr/local/test/data/”,所以我的问题是如何使用Perl脚本 使用fol

我在
output.txt
文件中有所有文件的名称,但在
output.txt
文件中没有
folderpath
。例如,如果
filename=test.txt
及其
folderpath=/usr/local/bin
大于
output.txt
文件中的值,则我只有
文件名为test.txt

output.txt
文件有许多关于
filename
的条目,我想做的是:

  • 将存在的所有文件
    output.txt
    移动到另一个文件夹中,比如“/usr/local/test/data/”,所以我的问题是如何使用
    Perl脚本

  • 使用
    folderpath
    ,从实际位置删除
    output.txt
    文件中存在的所有文件,因此,例如,脚本应首先将
    test.txt
    移动到其他文件夹位置,例如,
    /usr/local/test/data/test.txt
    并使用folderpath信息从实际位置删除test.txt,即脚本应删除
    /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";
    
    我的output.txt文件还有一些文件的名称包含特殊字符,例如, 63551_106640_63551 IBM
    和#253
    ;软件交付与实现(Div-61)数据IPS 08-20-2010 v3.xlsm


    如果您在上面看到,文件的编码值为
    和#253
    ,但我的文件名具有实际的特殊字符符号,因此将复制或移动我的此文件并移动文件

    一个建议是使用perl函数。您可以使用此选项移动文件,如下所示:

    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函数。您可以使用此选项移动文件,如下所示:

    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";
    }
    

    您是否可以显示
    output.txt
    文件的几行,以及到目前为止脚本的内容(如果您已经开始编写)?如果您想在unix中引用该文件,您是在文件名中使用“?”还是在特殊字符中使用“?”?您是否可以显示
    output.txt
    文件的几行,以及到目前为止脚本的内容(如果已经开始编写)?如果要在unix中引用该文件,是否使用“”在文件名或特殊字符中?
    File::Copy
    有一个可能更安全的
    move
    功能。@Platinum Azure:你能举个例子.hwo来处理名称中有特殊字符的文件吗?复制会考虑特殊字符文件吗?这只在沿同一磁盘移动文件时有效,不是吗?是
    File::复制是一种更普遍的方法。我的建议是教某人如何钓鱼,因此包括指向perldoc()的链接和对perms的查询等。原文这个问题被描述为Unix唯一的解决方案。所以我希望OP能看到
    重命名
    脚注:对于独立于平台的移动函数,请查看
    文件::复制
    模块。
    文件::复制
    有一个
    移动
    函数,可能更安全。@Platinum Azure:您能给出一个.hwo示例来处理具有特殊字符的文件吗名字中的字符?复制会考虑特殊字符文件吗?这只在沿着同一磁盘移动文件时有效,不是吗?是的,
    file::copy
    是更通用的方法。我的
    建议是教某人如何搜索答案,因此包括指向perldoc()的链接和对perms的查询等(原件)这个问题被描述为Unix唯一的解决方案。所以我希望OP能看到
    重命名
    脚注:对于独立于平台的移动函数,请查看
    文件::复制
    模块。