By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,372 Members | 1,950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,372 IT Pros & Developers. It's quick & easy.

How to copy files from one directory to another?

P: 2
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
Nov 17 '08 #1
Share this Question
Share on Google+
7 Replies


bytesonfire
P: 7
Try this
File::Copy
Nov 17 '08 #2

P: 2
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/");
Nov 17 '08 #3

bytesonfire
P: 7
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

P: 13
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;
  3.  
  4. my $pwd = "C:\\perl";
  5. my $destination = "D:\\textfile";
  6.  
  7. opendir(DIR,"$pwd") or die "Cannot open $pwd\n";
  8. my @files = readdir(DIR);
  9. closedir(DIR);
  10.  
  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

KevinADC
Expert 2.5K+
P: 4,059
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

P: 13
Oops... my bad. Will take note of that.
Nov 19 '08 #7

P: n/a
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.