Click to See Complete Forum and Search --> : How to retrieve a file


mbhojani
01-15-2008, 11:31 AM
Hi,

I have got a script which takes as an argument an http/ftp address only. Now instead of http/ftp address I wanted to pass as an argument a location of file.
Please tell me how to proceed.

Here is script:

#!/usr/bin/perl -w

#This program downloads a file or a list of files from web in binary mode

use strict;
use LWP::UserAgent;

my $location = $ARGV[0];
my $save_dir;
if ($ARGV[1])
{
$save_dir = $ARGV[1];
}
else
{
$save_dir = "c:\\";
}

if (not $ARGV[0])
{
print "Usage: download.pl webfile location\n";
print "e.g. download.pl http://xxx/whatever.xxx c:\\tools\n";
exit;
}

printf "Downloading $location to $save_dir\n";

my $ua = LWP::UserAgent -> new(env_proxy => 0,
timeout => 50,
keep_alive => 1,
);

my ($file, %file);
if ($location =~ /^(http|ftp)/i)
{
$file = $location;
$file =~ s/^.*\///;
$file{$location} = $file;
}
else
{
open (IN, $location) or die "Can't read $location - $!";
while (<IN>)
{
chomp;
$file = $_;
$file =~ s/^.*\///;
$file{$_} = $file;
}
close IN;
}


foreach my $f(keys %file)
{
while (-e "$save_dir/$file{$f}")
{
$file{$f} = '1' . $file{$f};
}
my $request = HTTP::Request -> new('GET', $f);
my $response = $ua -> request($request, "$save_dir/$file{$f}");
}

Please suggest me if I want to put:
file://///lon-javaserv01/intranet-root/Dev/java/results/

instead of:

http://web.intra/dev/java/results/