Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2002-03-05
11
193 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SIMPLE Perl Regex 1 162
PERL export multiple query results to a JSON file 1 173
git fat pull happens nothing in centos 6.3 python 2.6.6 33 219
Perl Awk Need Help 3 128
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

856 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