• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

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

kmthien

Senior member
#!/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 !

 
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.
 
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


 
ls in array context will return an array of lines the server replies with, just loop through it like you would normally.
 
Hi,

Can u show me the code how to loop thru the files inside a folder in remote server via FTP ? Thanks !
 
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.
 
Do you not have documentation for the Net::FTP module? All of it's methods are documented in the perldocs and man pages.
 
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.
 
Back
Top