473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Not able to copy all the files using perl

Hi All,

i am trying to move a bunch of files in a newly created directory: here is what i am trying to do.

: i read my source dir.
: i search for a file which has the folder name information in it. This file is in the source folder.
: i create that folder.
: copy all the files from source dir in the new folder.

Now every thing goes fine except for the last piece which is copy. when i check the folder after running the script i get only one file which is the one i searched in the source folder.

i want to copy all the files in the source folder to the newly created folder. Kindly help.

this is what i have written so far, its not neat but i have tried :

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl
  3. use File::Find;
  4. use File::Copy;
  5. my $dir = "/ctm/ctms/ctm_server/tmp/";
  6. my $myfile="cobrun_date.txt";
  7. opendir(DIR, $dir) or die "Can't open $dir: $!";
  8. my @files = grep {!/^\.+$/} readdir(DIR);
  9. closedir DIR;
  10. my @kill= grep(/cobrun_date\.txt/, @files);
  11. if ($myfile == $kill[0])
  12. {
  13. print "the match is found\n";
  14. open (FILE, $myfile) or die "Can't Open the file $myfile:$!";
  15. my @filedata=<FILE>;
  16. chomp($filedata[0]);
  17. $path = $dir.$filedata[0];
  18. print "$path \n";
  19. mkdir($path, 0777) or die "Couldnt create dir $path : $!";
  20. close FILE;
  21. foreach $f (@files)
  22. {
  23. print "The file name is :$f\n";
  24. if ( -f $f ){
  25. copy("$f","$path") or die "Copy Failed : $!";
  26. print "The file have been moved \n";
  27. }}
  28. }
  29. else
  30. {
  31. print "the match is not found\n";
  32. }
  33.  
  34.  
Feb 13 '11 #1
3 2537
miller
1,089 Expert 1GB
I see one major flaw in your code, and that lies with this line:
Expand|Select|Wrap|Line Numbers
  1. if ($myfile == $kill[0]) {
You're using the numerical equality check '==' instead of string equality 'eq'. This will always evaluate as true because each of the strings will be treated as 0 for the equality test. Therefore you could end up processing a file that doesn't actually exist.

Anyway, the following is probably closer to what you want. I leave it to you to setup error checking for the case where the files you are moving already exist at their destination
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use File::Copy;
  4. use File::Spec;
  5.  
  6. use strict;
  7.  
  8. my $dir = "/ctm/ctms/ctm_server/tmp/";
  9. my $myfile = 'cobrun_date.txt';
  10.  
  11. my $path = File::Spec->catfile($dir, $myfile);
  12.  
  13. if (! -e $path) {
  14.     die "$path does not exist\n";
  15. }
  16.  
  17. my $destdir = do {
  18.     open my $ih, $path or die "Can't open file, $path: $!";
  19.     my $subdir = <$ih>; # Only retrieves first line
  20.     chomp($subdir);
  21.     File::Spec->catpath($dir, $subdir);
  22. };
  23.  
  24. if (! -e $destdir) {
  25.     mkdir($destdir, 0777) or die "Can't mkdir $destdir: $!";
  26. }
  27.  
  28. opendir my $dh, $dir or die "Can't opendir $dir: $!";
  29. while (my $file = readdir $dh) {
  30.     next if $file =~ /^\.+$/;
  31.     my $fromfile = File::Spec->catfile($dir, $file);
  32.     if (-f $fromfile) {
  33.         my $tofile = File::Spec->catfile($destdir, $file);
  34.         copy($fromfile, $tofile) or die "Copy failed $fromfile->$tofile: $!";
  35.         print "$file copied $dir -> $destdir\n";
  36.     }
  37. }
  38. closedir $dh;
  39.  
  40. 1;
  41.  
  42. __END__
  43.  
- Miller
Feb 14 '11 #2
Thanks Miller, for your reply to me. but the below section of the code doesnt do what it should do.
Expand|Select|Wrap|Line Numbers
  1. opendir my $dh, $dir or die "Can't opendir $dir: $!";
  2. while (readdir $dh) {
  3.     next if /^\.+$/;
  4. print "$dh\n";
  5.     my $fromfile = File::Spec->catpath($dir, $_);
  6.     if (-f $fromfile) {
  7.         my $tofile = File::Spec->catpath($destdir, $_);
  8.         copy($fromfile, $tofile) or die "Copy failed $fromfile->$tofile: $!";
  9.         print "$_ copied $dir -> $destdir\n";
  10.     }
  11. }
  12. closedir $dh;
  13.  

This is simply printing the following :

GLOB(0x91a83c8)
GLOB(0x91a83c8)
GLOB(0x91a83c8)
GLOB(0x91a83c8)
GLOB(0x91a83c8)
GLOB(0x91a83c8)
GLOB(0x91a83c8)
GLOB(0x91a83c8)
GLOB(0x91a83c8)
GLOB(0x91a83c8)


where as it should give the file names and copy them to the destination dir.

thanks.

Rajesh
Feb 15 '11 #3
miller
1,089 Expert 1GB
Sorry Rajesh, I've helped you as much as I can. It's up to you to take the time to learn what the code that I provided for you is doing so that you can debug it yourself or ask more intelligent questions than, "It doesn't work".

As for why it's printing out "GLOB(0x91a83c8)" repeatedly, well that's because you added a print statement for $dh, which is a directory handle. If you wanted to print out the filenames, you should use the in place variable $_.

Good luck, and we'll be here if you have specific problems, but it looks like you need to do some reading at http://perldoc.perl.org first.

- Miller
Feb 15 '11 #4

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

Similar topics

1
by: Kay Lee | last post by:
Hi, I looked up os module to find out some method to move and copy files in python, but os doesn't support such methods. Is there any way to move & copy files in python? Thanks in adv.
3
by: Kay Lee | last post by:
Hi, I looked up os module to find out some method to move and copy files in python, but os doesn't support such methods. Is there any way to move & copy files in python? Thanks in adv.
14
by: **--> That Guy Downstairs | last post by:
What files are needed to be #included to be able to copy files to a new directory and be portable? ie. use it in Unix (SGI and Linux) or Windows 2000. #ifdefs Ok. using dirent.h on SGI, but...
1
by: dkmarni | last post by:
Hi, I am trying to do this perl script, but not able to complete it successfully. Here is the description what the script has to do.. Accept two and only two command line arguments. Again,...
11
Vasuki Masilamani
by: Vasuki Masilamani | last post by:
Hi, Can any one help me in writing a script in Perl to compare two csv files and pick out the records which show differences? Any responses would be appreciated. Thanks, Vasuki
1
by: jazzy121 | last post by:
i want to copy a file usind default share on to the other pc which is also on lan and in same domain as well. the user from wich have logged on have full permissions. but when i try to copy the...
3
by: SJM | last post by:
Howdy, hope someone can help me out with this. I want to run a job each night that copies files from one server to another. I cant even get a simple copy one file from one directory to another,...
3
by: Davo1977 | last post by:
Does anyone know a regular expression that will rename multiple files that have different extensions to have the same extension. For example, you could use this code when several text files exist in...
2
by: parmarjm | last post by:
Hi I am trying to copy files from mapped drive (J:\) to a different machine. The perl script is going to run on my machine to copy files from one place to another. File transfer works when i copy...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.