Solved

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

Posted on 2002-03-05
11
198 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
[X]
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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl script help 12 106
hard perl script 16 162
Perl Sort Question 4 141
XPath Syntax help in conjunction with perl XML::LibXML 18 128
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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

740 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