Perl script to check number of files in a directory in remote server ?

kmthien

Senior member
Oct 8, 2002
363
0
0
#!/usr/bin/perl

use Net::FTP;
use IO::File;

unless($filesToPut[0]){
open(OUTFILE,">>/home/pa/telecentre/log/pa_tc.log");
print OUTFILE "- No senior citizen data files(zip) found on $Day/$RealMonth/$RealYear $Hour:$Minute:$Second\n";
close(OUTFILE);
exit;
}

### Start connection to www.abc.com ###
print "Connecting to www.abc.com .....";
if(not defined($ftp = Net::FTP->new("www.abc.com", Debug => 0, Timeout => 300)))
{
print "failed to connect to server !\n";
exit;
}

print "\nVerifying user.....";
if(!$ftp->login("abc","abc"))
{
$ftp->quit();
die "failed to login\n";
exit;
}
print "Connected to TeleCentre Server !\n";

if(not $ftp->binary())
{
$ftp->quit();
print "failed to set to binary mode\n";
exit;
}

print "Getting files from abc server.....\n\n";

# from here, how do I check if the directory contain any .bin file(s), only transfer .bin file. If yes, it will get back all the .bin file to local server !


Thanks !

 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
From my Net::FTP man page:
ls ( [ DIR ] )
Get a directory listing of "DIR", or the current
directory.

In an array context, returns a list of lines returned
from the server. In a scalar context, returns a refer­
ence to a list.

I'm not sure if you can specify a regular expression to it (i.e. *.bin), but even if not you can loop through the array it returns and filter it manually fairly easily.
 

kmthien

Senior member
Oct 8, 2002
363
0
0
Hi,

How to loop thru all the .bin in remote server since I m using ftp ? Like what I did in local server as follows:

@filesToPut;

opendir F, "/var/temp"; # Open directory

###### Search thru directory if *.zip file(s) is found #####
while(my $temp = readdir F)
{
if($temp =~ m/\.bin$/i)
{
push @filesToPut, $temp;
}
}
closedir(F);

### If no bin file found inside the directory, exit this script ####
unless($filesToPut[0])
{
exit;
}

How can I search thru a folder in remote server using ftp and put each .bin files name inside an array like what I did locally ? Pls help, Thanks !

regards,
kmthien


 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
ls in array context will return an array of lines the server replies with, just loop through it like you would normally.
 

kmthien

Senior member
Oct 8, 2002
363
0
0
Hi,

Can u show me the code how to loop thru the files inside a folder in remote server via FTP ? Thanks !
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Intead of using $variable = readdir to return one entry, use @dir_listing = ftp->ls() to get them all at once. Then use foreach to loop across the array and see if there's any .bin files. You have most of the code posted above, it just needs some minor changes.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Do you not have documentation for the Net::FTP module? All of it's methods are documented in the perldocs and man pages.
 

kmthien

Senior member
Oct 8, 2002
363
0
0
Hi,

cwd is to change remote server directory only. What I need to change is local directory.

Thanks!
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
CWD means Current Working Directory, since you're logged into an FTP server you have 2 of them, one on the FTP server and one locally.

You can use $ftp->cwd to change the FTP CWD and you can use chdir to change the local CWD.

Where are you doing this development? Do you have the perl documentation installed? It's worth reading, it's mostly really good.
 

kmthien

Senior member
Oct 8, 2002
363
0
0
Hi,

U means in my perl script, I just enter

`chdir \var\temp`;

will change tp other directory ?