Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

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
0
vmandem
Asked:
vmandem
1 Solution
 
Kim RyanIT ConsultantCommented:
For each file you want to remove:
unlink($filename);
0
 
TalmashCommented:
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
0
 
vmandemAuthor Commented:
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.com");
$ftp->login("test","pwd123");

@dir = $ftp->ls();


foreach (@dir)
{
     print $_ . "\n";
     $ftp->get("$_","\\test\\Data\\$_");
     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
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
vmandemAuthor Commented:
I want to open this question because i'm not getting any
response from Talmash.

VM
0
 
Kim RyanIT ConsultantCommented:
foreach (@dir)
{
    print $_ . "\n";
    $ftp->get("$_","\\test\\Data\\$_");
    if (!$ftp->ok())
    {
         print "Error -> " . $ftp->message();
         print LOG "Error -> " . $ftp->message();
    }
    else
    {
        unlink('/test/Data/$_');
    }
}
0
 
vmandemAuthor Commented:
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
0
 
GnarOlakCommented:
If I read your question correctly, you want to remove the file from the remote system, the FTP host, if you download it correctly?  If so then teraplane's solution is 99% correct with one minor change:

else
{
    $ftp->delete($_);
}
0
 
Kim RyanIT ConsultantCommented:
Yes, $ftp->delete($_); should do it. You didn't specify that you wanted the source files deleted.
 
0
 
vmandemAuthor Commented:
teraplane

sorry about that. I regret my mistake. I will not do that again.

VM
0
 
Kim RyanIT ConsultantCommented:
No, that's fine, part of our job is to seek out missing detail. Can we close this question now?
0
 
vmandemAuthor Commented:
I really appreciate your response.

Thanks
VM
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now