| | |
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:
Solved Threads: 0
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: ";
}
}
}
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: ";
}
}
}
•
•
Join Date: Jun 2009
Posts: 10
Reputation:
Solved Threads: 2
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: ";
}
}
}
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: ";
}
}
}
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
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 ...
--
-- Ghodmode
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)
# '.' and '..' are directories, so the next if statement # will exclude them anyway. # if($file ne "." || $file ne "..") # { if ( -f "$source\\$file" ) { # The following statement is probably not necessary. chomp($file); print "\n copying...."; copy("$source\\$file", $dest) or die "Copy Failed: "; } # }
--
-- Ghodmode
![]() |
Similar Threads
- Is There A Way To Make Access File As Exe File (MS Access and FileMaker Pro)
- uploaded file moving to new directory (PHP)
- open file from root directory (C#)
- Copying exe file into startup (Visual Basic 4 / 5 / 6)
- Creating an exe file (C++)
- File doesn't upload into the directory I specified. Need help! (PHP)
- A agntsrvc.exe file error (Viruses, Spyware and other Nasties)
- Put file in protected directory? (Networking Hardware Configuration)
Other Threads in the Perl Forum
- Previous Thread: execute ksh script in perl
- Next Thread: Bizarre Post problem
| Thread Tools | Search this Thread |





