CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
To stop download when file is already existed in the folder

 



ragha12
Novice

Sep 12, 2011, 9:19 PM

Post #1 of 2 (1181 views)
To stop download when file is already existed in the folder Can't Post

#!/usr/bin/perl
#!/usr/bin/perl
use LWP::Simple;
use LWP::UserAgent;
use HTML::Parse;
use HTML::FormatText;
use HTML::TreeBuilder::XPath;
use NET::SSL;
use WWW::Mechanize;
use IO::Socket::SSL;
use strict;
use warnings;

my $pageURL;
my $simplePage;
my @headings;
my @array;
my $head;
my $par;
my $var1;
my $fileName;
my $mech;
my $pageContent;
$mech = WWW::Mechanize->new;
$pageContent = $mech->get("https://some website");
#print $content->content;

my $tree = HTML::TreeBuilder::XPath->new;
#parsing the URL
$tree->parse_content( $pageContent->content );
#print $tree->as_HTML;
#Storing into the array and searching
@headings = $tree->look_down( id => "resultsetcontainer" );

foreach $head (@headings)
{
#print "headings -->" . $head->as_XML_indented(). "\n";
#Searching the element in the array
@array= $head->find_by_tag_name("a");

foreach $par (@array)
{
#Storing the each attribute into the variable
$var1=$par->attr("href");
#print "$var1\n";

my $var2=substr $var1,21,length($var1);
#print "$var2\n";
$fileName = "C:\\outputFiles\\$var2";


my $download =("https://some website");
print "Downloading $download to $fileName \n";
#print "$fileName \n";


$mech->get("$download", ':content_file' => "$fileName" );

if ( $mech->success() )
{
print "file downloaded correctly\n";
}
else
{
print "error downloading file:\n";
}
}
}

Hi ,
This is the code that i have used to download all the files from website, successfully i downloaded all the files.

Now when i downloading all the files once again it should not download, it should say that the file is already existed......how to do this......

Anyone tell me please i am waiting for ur reply...

Thank you... Unimpressed


Zhris
Enthusiast

Sep 15, 2011, 1:08 PM

Post #2 of 2 (1155 views)
Re: [ragha12] To stop download when file is already existed in the folder [In reply to] Can't Post

Hi,

Its very easy to check if a file exists:


Code
if (-e $fileName) { 
print "File already exists";
next;
}
else {
#process the file
}


Chris

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives