471,896 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,896 software developers and data experts.

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)?


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
Nov 17 '08 #1
7 27419
Try this
Nov 17 '08 #2
Try this
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/");
Nov 17 '08 #3
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.
Nov 17 '08 #4
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.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  4. my $pwd = "C:\\perl";
  5. my $destination = "D:\\textfile";
  7. opendir(DIR,"$pwd") or die "Cannot open $pwd\n";
  8. my @files = readdir(DIR);
  9. closedir(DIR);
  11. foreach my $file (@files) {
  12.     next if ($file !~ /\.txt$/i);  # filtered only file with extension .txt
  13.     system("copy \"$pwd\\$file\" \"$destination\""); 
  14. }
The additional (\") in the system command is to make sure the command line takes in the space as part of the directory path name.
Nov 18 '08 #5
4,059 Expert 2GB
Please do not provide code to people that show no effort to first solve their own coding problems, especially simple ones like this.
Nov 18 '08 #6
Oops... my bad. Will take note of that.
Nov 19 '08 #7
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
Nov 4 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Mike | last post: by
8 posts views Thread by Chris Strobel | last post: by
11 posts views Thread by MarkusR | last post: by
2 posts views Thread by xoinki | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.