为什么此程序无法复制文件? [英] Why does this program fail to copy files?

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

问题描述

今天早上,我的朋友和我讨论并写下面的代码。这个Perl脚本背后的想法是创建目录结构并将文件复制到相应的目录。

this morning, my friend and I discussed and wrote the below code. The idea behind this Perl script is to create the directory structure and copy the files to the corresponding directory.

#!/usr/bin/perl
use File::Path;
use File::Copy;
use Path::Class;
use File::Basename qw/dirname/;
my $src = "/Vijay/new.txt";
unless (open(MYFILE, "file1")) {
    die ("cannot open input file file1\n");
}
$line = <MYFILE>;
while ($line ne "") {
    print ($line);
    mkdir_and_copy($src,$line);
    $line = <MYFILE>;
}
sub mkdir_and_copy {
    my ($from, $to) = @_;
    my($directory, $filename) = $to =~ m/(.*\/)(.*)$/;
    print("creating dir $directory");
    system "mkdir -p $directory";
    print("copying file $from to $to");
    system "cp -f $from $to";
    return;
}

上面的代码创建目录结构,但无法复制文件到相应的目录。

The above piece of code creates the directory structure, but fails to copy the files to the corresponding directory. Could you please let us know, where exactly we are wrong?

档案1的内容:

test/test1/test2/test.txt

Shell/Test/test1/test1.txt
Shell/Test/test2/test2.txt
Shell/Test/test3/test3.txt

输出:

> ./mypgm.pl
test/test1/test2/test.txt
creating dir test/test1/test2/copying file /Vijay/new.txt to     test/test1/test2/test.txt
cp: cannot access /Vijay/new.txt: No such file or directory
>

目录 Vijay c $ c> new.txt 。

The directory Vijay has the file new.txt with the above mentioned content.

提前感谢,

Vijay

大家好,

我刚刚修改了我的代码。请参考下面的代码部分。

Hello everyone,
I just modified my code. Please refer the below section of code.

#!/usr/bin/perl        
use File::Path;    
use File::Copy;    
use File::Basename qw/dirname/;    

my $src = "./Vijay/new.txt";       
unless (open(MYFILE, "file1"))    
{    
die ("cannot open input file file1\n");    
}

$line = ;
while ($line ne "")
{
print ($line); print("\n");
mkdir_and_copy($src,$line);
$line = ""; }

sub mkdir_and_copy        
{      
my ($from, $to) = @_;    
my($directory, $filename) = $to =~ m/(.\/)(.)$/;    
$temp = $directory.$filename;    
print("Creating dirrectory $directory \n");    
if(! -d $directory)    
{    
mkpath($directory) #or die "Failed to create path";    
}    
printf("From: $from \n");    
printf("To: $temp \n");    
copy($from,$temp) or die "Failed to Copy";    
return;    
}    



现在,它创建确切的目录结构,并将文件复制到相应的目录。

Now, it creates the exact directory structure and copies the file to the corresponding directory. Could you please tell me that, whether the above code is a proper one?

推荐答案

您的目标对我来说不清楚,但是,也许这将帮助你解决问题:

Your goal is not clear to me, but perhaps this will help you solve the problem:

# Perl scripts should always include this.
# Your original script was generating some useful warnings.
use strict;
use warnings;

my $src = "/Vijay/new.txt";
my $f1  = 'file1';

# This is the recommended way to open a file --
# that is, using a lexical file handle.
open(my $file_handle, '<', $f1) or die "open() failed : $f1 : $!";

# This is the typical way of iterating over the lines in a file.
while (my $line = <$file_handle>){
    # You probably want to remove the newline
    # before passing the line to mkdir_and_copy()
    chomp $line;

    mkdir_and_copy($src, $line);
}

sub mkdir_and_copy {
    my ($from, $to) = @_;
    my ($directory, $filename) = $to =~ m/(.*\/)(.*)$/;

    # When writing a script that makes system() calls,
    # start by simply printing them. After everything
    # looks good, convert the print commands to system() calls.
    print "system(): mkdir -p $directory", "\n";
    print "system(): cp -f $from $to",     "\n";

    # The return is not needed.
}



当我使用你提供的输入来运行脚本时, / p>

When I run the script with the inputs you provided, here's the output:

system(): mkdir -p test/test1/test2/
system(): cp -f /Vijay/new.txt test/test1/test2/test.txt

这不是你的意图。特别是,当它只包含一行时,为什么要迭代 file1 ?也许你打算迭代 new.txt

This can't be your intent. In particular, why are you iterating over file1 when it contains only one line? Perhaps you meant to iterate over new.txt?

这篇关于为什么此程序无法复制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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