Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

File name is long to copy

by harishnv (Sexton)
on Apr 26, 2020 at 13:07 UTC ( [id://11116074]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to copy a file but the file name is long. Therefore copy is failing even though the file exits. How to resolve this issue? The path specified is an example of what i'm trying to copy the file.

#!\\\\usr\\\\bin\\\\perl use strict; use warnings; use File::Copy; my $source="C:\\sandboxes\\source_backup\\source_backup\\source_develo +p\\source_folder\\source_part\\source_specific\\source_common\\rb_src +c_sourcedevelopfolderpartspe_Interfacessourcecommonspecific.h"; my $target="C:\\sandboxes\\target_backup\\target_backup\\target_develo +p\\generated\\_hnv_scan\\_source_develop\\source_folder\\source_part\ +\source_specific\\source_common"; copy($source,$target) or die "could not do"; print ("done");

Replies are listed 'Best First'.
Re: File name is long to copy
by syphilis (Archbishop) on Apr 27, 2020 at 08:56 UTC
    I copied your directory set up, along with the filename, and ran your script on perl-5.12.0 on Windows 7 and it worked fine for me. I did add a couple of lines to check that both the file ($source) and the destination directory ($target) existed:
    use strict; use warnings; use File::Copy; my $source="C:\\sandboxes\\source_backup\\source_backup\\source_develo +p\\source_folder\\source_part\\source_specific\\source_common\\rb_src +c_sourcedevelopfolderpartspe_Interfacessourcecommonspecific.h"; my $target="C:\\sandboxes\\target_backup\\target_backup\\target_develo +p\\generated\\_hnv_scan\\_source_develop\\source_folder\\source_part\ +\source_specific\\source_common"; print "OK 1\n" if -e $source; print "OK 2\n" if -d $target; copy($source,$target) or die "could not do: $!"; print ("done");
    I got:
    C:\_32\pscrpt>perl try.pl OK 1 OK 2 done C:\_32\pscrpt>
    However, the first time I ran it, it failed because I had not named the $target directories correctly.
    I had missed the underscores at the beginning of "_hnv_scan" and "_source_develop", and the script therefore produced:
    C:\_32\pscrpt>perl try.pl OK 1 could not do: at try.pl line 10.
    Please do check that both the $target directory and $source file do actually exist, as it seems to me that the most likely explanation of your issue is a typographical error.

    Cheers,
    Rob
Re: File name is long to copy
by marto (Cardinal) on Apr 26, 2020 at 13:18 UTC

    What error are you getting? You could be hitting some OS (which?) based path depth limit. Let perl tell you what happened:

    copy( $source, $target ) or die "couldn't copy file $!";

      Good advice. I'd include the paths too as the longer they are the more likely it is that one of them may have a typo.

      copy( $source, $target ) or die "couldn't copy file '$source' to '$tar +get': $!";

        the other files in that folder are copied but not this, if I reduce the name of the file then it works but that is not possible for me.

      "could not do at C:\sandboxes\Scripts\dummy.pl line 9" the error I got. any other solution? or can I copy the folders, sub folders and files in line of code? I'm using Windows

        Which version of Windows? It doesn't look like you tried what I suggested, note the $!

        copy( $source, $target ) or die "couldn't copy file $!"; #---------------------------------------------------^
Re: File name is long to copy
by 1nickt (Canon) on Apr 26, 2020 at 14:32 UTC

    Hi,

    Did you try the copy outside your Perl program on the command line, cutting and pasting the pathnames from your code? I see that when you printed the error it stated that the file or path does not exist.

    Hope this helps!


    The way forward always starts with a minimal test.

      the path name was mentioned also in the line 9

Re: File name is long to copy
by NetWallah (Canon) on Apr 27, 2020 at 00:36 UTC
    The code you pasted has funny HTML markup in the middle of the $source filename.

    The part of the filename that says:

    so­urce_specific -><---
    has HTML between the "o" and the "u".

    The HTML says:

    <font color="red"><b><u><wbr>­</u></b></font>
    THis is also true of $target.

    I'm not sure if that is an artifact of the way this site renders the code .. but there appears to be a problem with what it contains.

                    "Imaginary friends are a sign of a mental disorder if they cause distress, including antisocial behavior. Religion frequently meets that description"

      I don't see anything like that in the post, so it must be how this site renders the code for you. Check Code Wrapping in your Display Settings.
      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        Thank you for pointing that out -- Yes - it does appear to have something to do with local rendering.

        My setting has "Code Wrap Length" set to 100 , and "Auto Code Wrapping" enabled.

        The inserted HTML starts at the 104'th character, so it is likely that some characters (probably backslashes \\) do not count.

        The inserted HTML does have a clue that it has to do with wrapping:

        <font color="red"><b><u><wbr>­</u></b></font>
        I learned that:
        The <wbr> (Word Break Opportunity) tag specifies where in a text it would be ok to add a line-break.

                        "Imaginary friends are a sign of a mental disorder if they cause distress, including antisocial behavior. Religion frequently meets that description"

Re: File name is long to copy
by jcb (Parson) on Apr 28, 2020 at 01:42 UTC

    Your problem is almost certainly Windows. You will need to work around some Microsoft dain bramage by changing to the source directory, opening the source file using a relative filename, changing to the target directory, opening the target file using a relative filename, and then copying the file contents. Note that you may have to change directories in steps (chdir('C:\sandboxes\source_backup\source_backup\source_develop') && chdir('.\source_folder\source_part\source_specific') && chdir('.\source_common') or die "chdir: $!"; is one possibility) if the directories are themselves deeply nested with long names, or use UNC paths for source and target, which are either exempt from path length limits or have higher limits — I do not know which.

      Your problem is almost certainly Windows

      I think the problem is almost certainly a typo - but only because, having replicated the OP's file structure (as specified by his code), I can find no problem with it on Windows 7.
      OP's script works fine for me, as is.

      Either way, it would be interesting to know precisely which it is, but I'm not confident that we'll find out.

      Cheers,
      Rob

        The example paths in OP's code look sanitized to me and I suspect that the real file names are much longer. I have had these kinds of errors occur at $WORK with mapped network drives in the past and the solution I used then was to simply map another drive letter to a subfolder and access the deeply nested directories that way.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11116074]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (None)
    As of 2024-04-25 01:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found