Solved

How to delete all files after I download from a ftp site using Perl

Posted on 2002-03-05
11
182 Views
Last Modified: 2010-03-05
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
Comment
Question by:vmandem
11 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 6843274
For each file you want to remove:
unlink($filename);
0
 
LVL 6

Expert Comment

by:Talmash
ID: 6843698
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
 

Author Comment

by:vmandem
ID: 6844100
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
 

Author Comment

by:vmandem
ID: 6845170
I want to open this question because i'm not getting any
response from Talmash.

VM
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 6845377
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:vmandem
ID: 6846236
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
 
LVL 6

Accepted Solution

by:
GnarOlak earned 15 total points
ID: 6847361
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
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 6848582
Yes, $ftp->delete($_); should do it. You didn't specify that you wanted the source files deleted.
 
0
 

Author Comment

by:vmandem
ID: 6850985
teraplane

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

VM
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 6851644
No, that's fine, part of our job is to seek out missing detail. Can we close this question now?
0
 

Author Comment

by:vmandem
ID: 6855334
I really appreciate your response.

Thanks
VM
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl for loop for 2000 ms 7 92
Filtering a file to table 9 98
Exchange 2010 Transport Rule Regex 28 88
Awk Question 2 116
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now