Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

copy files and directories recursively

by aloknath521 (Initiate)
on Apr 01, 2008 at 11:18 UTC ( [id://677736]=perlquestion: print w/replies, xml ) Need Help??

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

Hello all, I wanted to recursively copy the contents of a directory(both directory and files) from one folder to the other. I have this code which tries to do that, but it copies the files as folders which is not correct. Can anybody tell me how to fix this ?I dont want to use any CPAN module for this.
#!/usr/bin/perl $startDir = q{c:\test}; $newDir = q{c:\new}; &myReadDir($startDir); exit 0; sub myReadDir { my ($dir) = @_; print "\n\nProcessing $dir \n" ; my (@dirs,$list); opendir(DIR,$dir) || warn "can't open the directory $dir: $!\n +"; #@dirs=grep {!(/^\./) && -d "$dir\\$_"} readdir(DIR); @dirs=grep {!(/^\./)} readdir(DIR); closedir (DIR); for $list(0..$#dirs) { print "list = $list\n" ; mkdir ($newDir) unless -d $newDir; #mkdir ($newDir) ; $ndir = $dir; $ndir =~ s/c:\\test/c:\\new/; print $ndir."\\".$dirs[$list],"\n"; mkdir ($ndir."\\".$dirs[$list]); &myReadDir($dir."\\".$dirs[$list]); } return 1; }

Replies are listed 'Best First'.
Re: copy files and directories recursively
by BrowserUk (Patriarch) on Apr 01, 2008 at 11:34 UTC
    I dont want to use any CPAN module for this.

    Use xcopy /s c:\test c:\new. You might want a couple of other options:

    c:\test>help xcopy Copies files and directory trees. XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [ +/W] [/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T +] [/U] [/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/EXCLUDE:file1[+file2][+file3]...] source Specifies the file(s) to copy. destination Specifies the location and/or name of new files. /A Copies only files with the archive attribute set, doesn't change the attribute. /M Copies only files with the archive attribute set, turns off the archive attribute. /D:m-d-y Copies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time. /EXCLUDE:file1[+file2][+file3]... Specifies a list of files containing strings. Each str +ing should be in a separate line in the files. When any of + the strings match any part of the absolute path of the file + to be copied, that file will be excluded from being copied. +For example, specifying a string like \obj\ or .obj will ex +clude all files underneath the directory obj or all files wit +h the .obj extension respectively. /P Prompts you before creating each destination file. /S Copies directories and subdirectories except empty ones +. /E Copies directories and subdirectories, including empty +ones. Same as /S /E. May be used to modify /T. /V Verifies each new file. /W Prompts you to press a key before copying. /C Continues copying even if errors occur. /I If destination does not exist and copying more than one + file, assumes that destination must be a directory. /Q Does not display file names while copying. /F Displays full source and destination file names while c +opying. /L Displays files that would be copied. /G Allows the copying of encrypted files to destination th +at does not support encryption. /H Copies hidden and system files also. /R Overwrites read-only files. /T Creates directory structure, but does not copy files. D +oes not include empty directories or subdirectories. /T /E incl +udes empty directories and subdirectories. /U Copies only files that already exist in destination. /K Copies attributes. Normal Xcopy will reset read-only at +tributes. /N Copies using the generated short names. /O Copies file ownership and ACL information. /X Copies file audit settings (implies /O). /Y Suppresses prompting to confirm you want to overwrite a +n existing destination file. /-Y Causes prompting to confirm you want to overwrite an existing destination file. /Z Copies networked files in restartable mode. The switch /Y may be preset in the COPYCMD environment variable. This may be overridden with /-Y on the command line.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: copy files and directories recursively
by pKai (Priest) on Apr 01, 2008 at 12:04 UTC

    You have to adjust your logic.

    You are not testing the entries to copy for being directories.

    You have one -d test, but that is done on the "constant" (i.e. never changed) $newDir.

Re: copy files and directories recursively
by apl (Monsignor) on Apr 01, 2008 at 12:03 UTC
    I dont want to use any CPAN module for this.

    Why are you unduly hindering yourself?

    Some hints:

    • Why are you doing a mkdir on $newDir (which never changes value) multiple times?
    • Why aren't you doing a unless -d ... when doing the second mkdir?
    Re: copy files and directories recursively
    by Scrat (Monk) on Apr 01, 2008 at 13:10 UTC
      Something else I picked up: As a rule of thumb you should always try to include
      use strict; use warnings;
      in your code.

      Here's why.

    Log In?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others romping around the Monastery: (4)
    As of 2024-04-16 04:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found