FTP search ,grep using perl

Yes i am looking for literal strings.

Perhaps you're in the wrong directory,
There is only one directory to search for the files

perhaps you're not looking for where your files are appearing,
Where should we look for the appearing files .

perhaps you don't have permissions to write to the current folder,
I dont need any permission as we are not putting or getting any file . We are just verifying if the file exist if so output to a txt file on the local server.

perhaps you don't have permissions to retrieve the files,
We are not getting any files from FTP server .

perhaps your search strings contain garbage you didn't intend (carriage returns?)
The example that i have given is a real example of the filename and the files in my text files .

They get put in the current working directory.

Oh. :o All right then. I'll change the program.

Thank you Corona688 . Really appreciate your time and patience

#!/usr/bin/perl -w

use Net::FTP;

while($line=<STDIN>)
{
        chomp($line); # Remove newline from end of string
        push(@str, $line); # Add string to end of @str array
}

$ftp = Net::FTP->new("localhost");
$ftp->login('user', 'pass');
$ftp->cwd("path");
$ftp->binary;
#$flag=$ftp->mdtm($_);

my @filenames=$ftp->ls();
print " Total files:", $#filenames + 1, "\n";
$match=0;
foreach (@filenames)
{
        for my $s(@str)
        {
                if(index($_, $s) >= 0)
                {
                        print $_, " Size:[", $ftp->size($_), "]",
                                " Mtime ", scalar(localtime($ftp->mdtm($_))),
                                "\n";
                        $match++;
                }
        }
}

if($match == 0) { print "No files matched\n"; }

$ftp->quit();

Corona688. This works absolutely great . Hats off to you ...:slight_smile:

You are my Guru in programming...

---------- Post updated at 01:27 PM ---------- Previous update was at 01:17 PM ----------

This script is wonderful . Need one more help in the same script .

Now we got the match for list of files , size and modified time.

In addition to that :

Sorry for the confusion .

I want the output to redirect to a file.

What do you mean 'updated dates'? Newer than what?

Sorry for the confusion ..

I wanted the output to be redirected to a file . Can you please help me on this

---------- Post updated at 03:01 PM ---------- Previous update was at 02:57 PM ----------

I got my answer...thank you

---------- Post updated 04-25-13 at 02:21 PM ---------- Previous update was 04-24-13 at 03:01 PM ----------

Corona688

how can i add the listfile inside the script itself and redirect the output to a log file in the script itself

./myscript.pl < listfile

At the top of the script,

my @str=( "12345", "67890", "12345678" );
open(OUT,">output.log");

Then change your print statements to

print OUT "something","else";

I tried that way . Script takes a very long time and doesnt reply back anything

Show your code please.

#!/usr/bin/perl -w

use Net::FTP;

my @str=("edfiles.txt");
open(OUT,"output.log");

while($line=<STDIN>)
{
        chomp($line); # Remove newline from end of string
        push(@str, $line); # Add string to end of @str array
}

$ftp = Net::FTP->new("ftp.ncdc.noaa.gov");
$ftp->login('anonymous', 'ara2wj@bp.com');
$ftp->cwd("/pub/data/gsod/2012");
$ftp->binary;
#$flag=$ftp->mdtm($_);

my @filenames=$ftp->ls();
print " Total files:", $#filenames + 1, "\n";
$match=0;
foreach (@filenames)
{
        for my $s(@str)
        {
                if(index($_, $s) >= 0)
                {
                         print OUT "$_", " ",  $ftp->size($_),
#                        print $_, " Size:[", $ftp->size($_), "]",
#                                " Mtime ", scalar(localtime($ftp->mdtm($_))),
                                "\n";
                        $match++;
                }
        }
}

if($match == 0) { print "No files matched\n"; }

$ftp->quit();

Are you looking for "edfiles.txt", or do you want to load from edfiles.txt?

Load from Edfiles.txt . It has the list of files i am searching

It's waiting forever because it's still trying to load from stdin, you didn't delete that part.

That code would not load from that file, since that's not what you asked for before.

You copied the code imperfectly, as well, leaving an > out of my instructions.

You also forgot to add the OUT to all the print statements.

open(IN, "<edfiles.txt");
open(OUT, ">output.log");

while($line=<IN>)
{
        chomp($line); # Remove newline from end of string
        push(@str, $line); # Add string to end of @str array
}

Thank you Again . it worked

I am Relatively very new to Perl scripting . I dont have help at work .
This forum and you are a great help to me .

---------- Post updated 04-26-13 at 01:31 PM ---------- Previous update was 04-25-13 at 04:07 PM ----------

Need help again . Below are the 2 things i need to this script

  1. This script creates an output to output.log . when i run again it should create a backup file example
output.log.<date>

with timestamp
2. Check the difference of the old file and the new file and report it if there is an update .

I have added the below script , the second script is an example of creating a backup file with timestamp but i dont know how to club that script to the 1st script

#!/usr/bin/perl -w

use Net::FTP;


my @str=("files.txt");

open(IN, "<files.txt");
open(OUT, ">output.log");

while($line=<IN>)
{
        chomp($line); # Remove newline from end of string
        push(@str, $line); # Add string to end of @str array
}

$ftp = Net::FTP->new("hostname");
$ftp->login('user', 'password');
$ftp->cwd("path");
$ftp->binary;

my @filenames=$ftp->ls();
print " Total files:", $#filenames + 1, "\n";
$match=0;
foreach (@filenames)
{
        for my $s(@str)
        {
                if(index($_, $s) >= 0)
                {
                         print OUT "$_", " ",  $ftp->size($_),
#                        print $_, " Size:[", $ftp->size($_), "]",
#                                " Mtime ", scalar(localtime($ftp->mdtm($_))),
                                "\n";
                        $match++;
                        copy ("output.log", "output.log".$date."");
                }
        }
}

if($match == 0) { print "No files matched\n"; }

$ftp->quit();

use File::Copy;
my (undef,undef,undef,$mday,$mon,$year,undef,undef,undef) = localtime;
$year += 1900;
$mon += 1;
$date = sprintf "%02d/%02d/%04d", $mon, $mday, $year;
#copy (" output.log","output.log".$date."");
 

This is getting more and more complicated and finicky. Would it be possible to set up rsync instead of ftp? It can do everything you want just with a few commandline switches.

Yes it would be great . I dont know how rsync works with third party FTP server . Can you send me a script .

But if you can do those 2 steps with perl i appreciate it.

It doesn't... It isn't FTP, it's rsync. That's why I asked.

If you don't control the FTP server then probably not.

Is there any end to the requirements you want? You've been asking for more and more for some days now.

Any way help me on the perl script instead of rsync

Since my FTP doesn't have mtime, I can't build anything I can test.