944,030 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Marked Solved
  • Views: 1685
  • Perl RSS
Oct 12th, 2009
0

copying a .exe file from one directory to another directory

Expand Post »
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: ";
}
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rdevi is offline Offline
5 posts
since Sep 2009
Oct 13th, 2009
0
Re: copying a .exe file from one directory to another directory
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: ";
}
}
}
Reputation Points: 55
Solved Threads: 24
Junior Poster in Training
k_manimuthu is offline Offline
93 posts
since Jun 2009
Oct 13th, 2009
0
Re: copying a .exe file from one directory to another directory
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 ...

perl Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 3
Newbie Poster
Ghodmode is offline Offline
11 posts
since Sep 2009
Oct 14th, 2009
0
Re: copying a .exe file from one directory to another directory
thank u
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rdevi is offline Offline
5 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: execute ksh script in perl
Next Thread in Perl Forum Timeline: Bizarre Post problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC