How to copy files from one directory to another?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prasadrallabandi
    New Member
    • Nov 2008
    • 2

    How to copy files from one directory to another?

    Hello every one,

    my question is how to copy from one directory to another directory (Windows)?

    clearly:

    c:\perl> present working directory

    I have to copy all the text files(*.txt), lets take 5 files from d:\textfile\ directory to e:\copied\ directory.

    note: all are different directories.

    please help me asap

    Thanks in advance
    Prasad
  • bytesonfire
    New Member
    • Nov 2008
    • 7

    #2
    Try this
    File::Copy

    Comment

    • prasadrallabandi
      New Member
      • Nov 2008
      • 2

      #3
      Originally posted by bytesonfire
      Try this
      File::Copy
      Thanks for the replay its not working..

      error: file cant be find.

      files are there even though iam getting

      copy command iam using
      copy ("C:/prasad/SONGS/songs/movie_list/*.txt",, "C:/prasad/SONGS/temp/radio/create/");

      Comment

      • bytesonfire
        New Member
        • Nov 2008
        • 7

        #4
        Originally posted by prasadrallaband i
        Thanks for the replay its not working..

        error: file cant be find.

        files are there even though iam getting

        copy command iam using
        copy ("C:/prasad/SONGS/songs/movie_list/*.txt",, "C:/prasad/SONGS/temp/radio/create/");

        If you want to copy multiple file use File::Glob to get all the files. After that do a foreach on that arry to copy all files one by one.

        otherwise you can simply use copy command in backticks (`) or within system command.

        Comment

        • ahgan
          New Member
          • Jul 2008
          • 13

          #5
          If you do not want to use additional library, you can use the conventional opendir/readdir functions to read in all the files into array, then use the foreach loop and system command to copy the file into the destination folder.

          Code:
          use strict;
          use warnings;
          
          my $pwd = "C:\\perl";
          my $destination = "D:\\textfile";
          
          opendir(DIR,"$pwd") or die "Cannot open $pwd\n";
          my @files = readdir(DIR);
          closedir(DIR);
          
          foreach my $file (@files) {
          	next if ($file !~ /\.txt$/i);  # filtered only file with extension .txt
          	system("copy \"$pwd\\$file\" \"$destination\""); 
          }
          The additional (\") in the system command is to make sure the command line takes in the space as part of the directory path name.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Please do not provide code to people that show no effort to first solve their own coding problems, especially simple ones like this.

            Comment

            • ahgan
              New Member
              • Jul 2008
              • 13

              #7
              Oops... my bad. Will take note of that.

              Comment

              • rinku james

                #8
                if you want copy one directory which contain many subfolders and files and other types then this command is used
                xcopy d:\mainfiles c:\rinku /e /i /h

                Comment

                Working...