Nelly has asked for the wisdom of the Perl Monks concerning the following question:

I need to copy files to the directory, but the way to the directory is not permanent, for example: my $dir1="c:\\$ver\\$client\\qa\\"; and I can't copy files to this directory. copy("$dir/$file","$dir1/$file"); What is my mistake? Can You help me? Thank you very much!!!!!!!!!
  • Comment on copy files to the nonpermanent directory

Replies are listed 'Best First'.
Re: copy files to the nonpermanent directory
by Aragorn (Curate) on Mar 24, 2004 at 08:38 UTC
    Are you using File::Copy? If so, change the copy statement to copy("$dir/$file","$dir1/$file") or die "Cannot copy file: $!\n"; What error message does this generate?

    Arjen

      The error message is: Unsuccessful stat on filename containing newline at C:/Perl/lib/File/Copy.pm lin e 91, <> line 2. Cannot copy file: No such file or directory. But I'm sure that the directory exists, the strings that I'm puting into $dir1 are from input, but if I'll write the same thing but without scalars, for example: my $dir1="c:\\perl\\begperl\\qa"; error message is: Cannot copy file: Bad file descriptor. Thank you very much for your response!!!!!!!!
        As the values that you are using to build your path are coming
        from a user, I would suggest that they still contain the newline character
        you will need to remove this
        chomp($path); chomp($file); $dir1 = join('\\', 'c:', $path, $file);
        Letting us see a snippet of your code will make solving your problems MUCH easier
Re: copy files to the nonpermanent directory
by Hena (Friar) on Mar 24, 2004 at 09:59 UTC
    Ok, first when putting code or text that should not change use code tags around it. Easier for us to read :).

    But to problem. Easy way to compose paths (so that they are cross-platform, also) is by using File::Spec. In there commands rootfir,catdir and catfile would be what you want. Example:
    use File::Spec; $dir1=catdir(rootdir(),$ver,$client,"qa"); # and for copy $old=catfile($dir,$file); $new=catfile($dir1,$file);
    Then try the copy with command with die around it.
    copy ($old,$new) or die "Unable to copy '$old' to '$new': $!";
    Note what I added to the die. What is copied to where is a good idea, as it shows to you what actually is happening. Similar thing when trying to open files (print what is actually being tried). I've fixed a lot of my own mistakes like this ;).

    UPDATE: as bart told, need to use File::Spec::Functions. Remember to ask for all commands you require.
    use File::Spec::Functions (qw/catdir rootdir catfile/);
      Thank you for responding, I've tried what you've sugested, but I recieved this error message: Undefined subroutine &main::rootdir called at C:\Perl\releaseqa3.pl line 4 ine 2. Thanks!!!!!!!
      Thank you for everything, can you help one more time on that issue? When I try to copy file, I'm recieving error message: > >Unable to copy 'C:\perl\.' to 'C:\perl\begperl\qa\.': Bad file descriptor at C:\ Perl\releaseqa3.pl line 83, <> line 2. > >What does it means? >>THANK YOU AGAIN. p.s I'm new here, I don't know what is it code tags.
Re: copy files to the nonpermanent directory
by ColtsFoot (Chaplain) on Mar 24, 2004 at 08:34 UTC
    We would need to know a little more information to be able to answer
    your question

    (i)Do all the directories in you path $dir1 exist?
    (ii)Do you have permission to write to the directory?
    (iii) What does your copy sub look like
    ....
    ...
    .
      (i) All the directories in the path $dir1 are exist, but it will be usefull to know, if I can create directories in the way of copy. (ii)I have permission to write to the directories, but even if I want just to open the directory, it's not working. (iii)my $dir=shift; while $#ARGV includes input like=> c:\perl If I will give to the $dir1 permanent path, everything will work.