copying a .exe file from one directory to another directory

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 5
Reputation: rdevi is an unknown quantity at this point 
Solved Threads: 0
rdevi rdevi is offline Offline
Newbie Poster

copying a .exe file from one directory to another directory

 
0
  #1
Oct 12th, 2009
Hi all !
Can anybody tell me why the following code is not copying the file from source to destination?

Thakns in advance........

use File::Copy;
my $source="C:\\shared\\8.4.1\\DXSI";
my $dest="C:\\r45";
my @files;
my $file;
opendir(DIR, $source) or die "can't opendir $source: $! \n";
@files=readdir(DIR);

close DIR;
foreach $file(@files)
{
print "\n $file";
if($file ne "." || $file ne "..")
{
if (-f "$source$file")
{
chomp($file);
print "\n copying....";
copy($source, $dest) or die "Copy Failed: ";
}
}
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 10
Reputation: k_manimuthu is an unknown quantity at this point 
Solved Threads: 2
k_manimuthu k_manimuthu is offline Offline
Newbie Poster
 
0
  #2
Oct 13th, 2009
You modify the below lines in your code.

1) if (-f "$source$file")
Add "\\" in between "$source" and "$file" then only it got the valid file path.

2) copy($source, $dest);
Here $source gives the directory name only. File name also must here.


use File::Copy;
my $source="C:\\shared\\8.4.1\\DXSI";
my $dest="C:\\r45";
my @files;
my $file;
opendir(DIR, $source) or die "can't opendir $source: $! \n";
@files=readdir(DIR);

close DIR;
foreach $file(@files)
{
print "\n $file";
if($file ne "." || $file ne "..")
{
if (-f "$source\\$file")
{
chomp($file);
print "\n copying....";
copy("$source\\$file", $dest) or die "Copy Failed: ";
}
}
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: Ghodmode is an unknown quantity at this point 
Solved Threads: 3
Ghodmode's Avatar
Ghodmode Ghodmode is offline Offline
Newbie Poster
 
0
  #3
Oct 13th, 2009
In Windows, the operating system handles file-locking. I haven't looked too closely at your code, but this is a problem that has affected me on several occasions. If the file you're trying to copy is open (or running), Windows may have it locked and prevent Perl from copying it.

Also, I noticed something in your code:
[code=perl]
if ( $file ne "." || $file ne ".." )
{
#...
[/icode]

That conditional expression says "If the file is not '.' (the current directory) OR If the file is not '..' (the parent directory), do the following". Following that logic, when it gets to the current directory entry (.), it'll still execute the block because the second part of the logical OR is true. Likewise, when it gets to the parent directory entry (..), it'll still execute the block because the first part of the logical OR is true. You probably want if ( ($file ne '.') && ($file ne '..') ) .

This probably hasn't caused a problem for you because the first statement inside of the block is another conditional block which identifies whether or not the file is a directory. Actually, take out the outer conditional entirely and it'll work just fine ...

  1. # '.' and '..' are directories, so the next if statement
  2. # will exclude them anyway.
  3. # if($file ne "." || $file ne "..")
  4. # {
  5.  
  6. if ( -f "$source\\$file" )
  7. {
  8. # The following statement is probably not necessary.
  9. chomp($file);
  10. print "\n copying....";
  11. copy("$source\\$file", $dest) or die "Copy Failed: ";
  12. }
  13.  
  14. # }

--
-- Ghodmode
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 5
Reputation: rdevi is an unknown quantity at this point 
Solved Threads: 0
rdevi rdevi is offline Offline
Newbie Poster
 
0
  #4
Oct 14th, 2009
thank u
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Perl Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC