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: Advanced:
To stop download when file is already existed in the folder

 



ragha12
Novice

Sep 12, 2011, 9:20 PM

Post #1 of 2 (1247 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


ulo
Novice

Sep 14, 2011, 11:44 PM

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

Here's a very very simple solution:

Code
if( -f $fileName ) { 
print "file '$fileName' exists\n";
}
else {
$mech->get("$download", ':content_file' => "$fileName" );
if ( $mech->success() ) {
[..]
}


( -f $file ) will evaluate to true iff $file is the path/name to an existing file.

I don't know if you're looking for something more elaborate, e.g., comparing timestamps of the files or something.

 
 


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

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