vmandem
asked on
How to delete all files after I download from a ftp site using Perl
Hi
I want to delete all the files once i downloaded from ftp site using Perl.
I already have a perl script how to download files from ftp site into a desingated folder.
Now i want to write code how to delete all the files that i downloaded, in the same above file.
VM
I want to delete all the files once i downloaded from ftp site using Perl.
I already have a perl script how to download files from ftp site into a desingated folder.
Now i want to write code how to delete all the files that i downloaded, in the same above file.
VM
my $dir = sprintf "$cwd";
my @Files;
my $file;
opendir DIRECTORY, "$dir" or die "Cannot Open Local Project directory : $!";
@Files = grep !/^\.\.?$/, readdir DIRECTORY;
closedir (DIRECTORY);
foreach $file (@Files) {
unlink ("$file");
}
*** thats all
tip : you can filter files before erasing them :
if ( ($file =~ /^tmp/) || # remove files begins with tmp
($files =~ /old/) || # remove *old* files
($files =~ /txt$/) ) {
unlink ("$file");
}
in this example , you will erase only tmp* *old* and *txt files
tal
my @Files;
my $file;
opendir DIRECTORY, "$dir" or die "Cannot Open Local Project directory : $!";
@Files = grep !/^\.\.?$/, readdir DIRECTORY;
closedir (DIRECTORY);
foreach $file (@Files) {
unlink ("$file");
}
*** thats all
tip : you can filter files before erasing them :
if ( ($file =~ /^tmp/) || # remove files begins with tmp
($files =~ /old/) || # remove *old* files
($files =~ /txt$/) ) {
unlink ("$file");
}
in this example , you will erase only tmp* *old* and *txt files
tal
ASKER
Talmash
I got my perl script file like this, how do i include the delete process in this script:
use Net::FTP;
open (LOG,">\\test\\Data\\ftp Data Log Files\\ftp Data.log");
$ftp = Net::FTP->new("ftp2.test.c om");
$ftp->login("test","pwd123 ");
@dir = $ftp->ls();
foreach (@dir)
{
print $_ . "\n";
$ftp->get("$_","\\test\\Da ta\\$_");
if (!$ftp->ok())
{
print "Error -> " . $ftp->message();
print LOG "Error -> " . $ftp->message();
}
}
$ftp->quit;
close LOG;
Using the above file i'm able to download the files to a 'Data' directory if you look at my for each loop.
After downloading i want to delete the files.
All my files are in main directory on ftp site. As soon i logon i get the files using $ftp ls();
Without changing the above code i want to include your code to delete the files in that directory.
Please help me on this. I appreciate your response.
VM
I got my perl script file like this, how do i include the delete process in this script:
use Net::FTP;
open (LOG,">\\test\\Data\\ftp Data Log Files\\ftp Data.log");
$ftp = Net::FTP->new("ftp2.test.c
$ftp->login("test","pwd123
@dir = $ftp->ls();
foreach (@dir)
{
print $_ . "\n";
$ftp->get("$_","\\test\\Da
if (!$ftp->ok())
{
print "Error -> " . $ftp->message();
print LOG "Error -> " . $ftp->message();
}
}
$ftp->quit;
close LOG;
Using the above file i'm able to download the files to a 'Data' directory if you look at my for each loop.
After downloading i want to delete the files.
All my files are in main directory on ftp site. As soon i logon i get the files using $ftp ls();
Without changing the above code i want to include your code to delete the files in that directory.
Please help me on this. I appreciate your response.
VM
ASKER
I want to open this question because i'm not getting any
response from Talmash.
VM
response from Talmash.
VM
foreach (@dir)
{
print $_ . "\n";
$ftp->get("$_","\\test\\Da ta\\$_");
if (!$ftp->ok())
{
print "Error -> " . $ftp->message();
print LOG "Error -> " . $ftp->message();
}
else
{
unlink('/test/Data/$_');
}
}
{
print $_ . "\n";
$ftp->get("$_","\\test\\Da
if (!$ftp->ok())
{
print "Error -> " . $ftp->message();
print LOG "Error -> " . $ftp->message();
}
else
{
unlink('/test/Data/$_');
}
}
ASKER
teraplane
Is this code delete the files in my windows directory?.
I'm wondering that the code you included will delete the files i downloaded to a folder called \\test\\data.
I want to delete the files in the ftp site itself, not the files i downloaded to my folder. which is at:
@dir = $ftp->ls();
I will try any way your code and let you know.
VM
Is this code delete the files in my windows directory?.
I'm wondering that the code you included will delete the files i downloaded to a folder called \\test\\data.
I want to delete the files in the ftp site itself, not the files i downloaded to my folder. which is at:
@dir = $ftp->ls();
I will try any way your code and let you know.
VM
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes, $ftp->delete($_); should do it. You didn't specify that you wanted the source files deleted.
ASKER
teraplane
sorry about that. I regret my mistake. I will not do that again.
VM
sorry about that. I regret my mistake. I will not do that again.
VM
No, that's fine, part of our job is to seek out missing detail. Can we close this question now?
ASKER
I really appreciate your response.
Thanks
VM
Thanks
VM
unlink($filename);