FTP "get" on files using date/time

hi!

I want to "get/mget" files from a FTP server,
selecting on age of existing files at remote host.

example in natural lang:
"get all files from FTP-server newer than 2 days".

I'm presently trying to "output - grep - awk on date/time - mget".
Doesn't work very good.

Has any1 stubled of this before and like 2 share?

/Jonas

:wink: I propose to handle this in 2 steps (whithin 1 script off course).
First make a connection and execute the ftp command dir. Make sure u use the option -v and capture your ftp output in a temp file.
After that you are going to read that file and create dynamicly a new ftp script to get only the files in which you're intrested.
You'll probably need also a script to calculate whith dates. If you haven't got one, just ask.

Thanks for your quick reply.

Your last statement IS my REAL problem - calculating dates from the file output. Doesn't output differ among servers?

Please - what's your solution?

//greatful Jonas

yeah,

Can you post a small example of the output you got

extract from output of ftp "ls" command:

-r-xr-xr-x 1 owner group 254082 Dec 27 2001 KD2.D48
-r-xr-xr-x 1 owner group 36690 Jan 2 12:10 KD2.D49
-r-xr-xr-x 1 owner group 954175 Jun 10 9:39 KD2.D72
-r-xr-xr-x 1 owner group 475550 Jun 17 11:09 KD2.D73
-r-xr-xr-x 1 owner group 267741 Jun 24 10:00 KD2.D74
/

This script seems to work on my system. But as you say, the exact format of the ouput from "dir" can vary. So you may need to tweak it for your system. But anyway, it should get you real close. It relies on datecalc, another script that I wrote. You can find it by searching this site.

#! /usr/bin/ksh

#
#  Setup a few variables

SYSTEM=somehost
USER=joeblow
PASSWORD=*******
DIRECTORY=tmp

tmpfile=/tmp/ftpdir$$
thisyear=$(date +%Y)
today=$(datecalc -j $(date "+%Y %m %d"))
Jan=01 Feb=02 Mar=03 Apr=04 May=05 Jun=06 
Jul=07 Aug=08 Sep=09 Oct=10 Nov=11 Dec=12

#
#  Get a copy of the remote dir output in a local file
#  called $tmpfile and attach that file to stdin for reading

exec 5>&1 >$tmpfile 4>&1
ftp -nv  >&4 2>&4 |&
print -p open $SYSTEM
print -p user $USER $PASSWORD
print -p cd $DIRECTORY
print -p dir
print -p bye
wait
exec >&5 2>&1 < $tmpfile

#
#  Ignore lines until we get a line that starts with 150

IFS=""
looking=1
while ((looking)) ; do
        read line
        word1=${line%%${line##+([! ])}}
        [[ $word1 = 150 ]] && looking=0
done

#
# Loop getting lines that start with a "-"
# Then delete some leading fields

{
IFS="" 
while read line ; do
        char1=${line%%${line#?}}
        [[ $char1 != - ]] && continue
        line="${line##+([! ])}"
        line="${line##+([ ])}"
        line="${line##+([! ])}"
        line="${line##+([ ])}"
        line="${line##+([! ])}"
        line="${line##+([ ])}"
        line="${line##+([! ])}"
        line="${line##+([ ])}"
        line="${line##+([! ])}"
        line="${line##+([ ])}"
        echo "$line"
done ; } | {

#
# Second loop reads selected and shortened lines,
# Computes the date and may get the file

IFS=" "

exec 4>&1
ftp -nv  >&4 2>&4 |&
print -p open $SYSTEM
print -p user $USER $PASSWORD
print -p cd $DIRECTORY
while read month day swing name ; do
        eval month=\$$month
        if [[ $swing = *:* ]] ; then
                year=$thisyear
        else
                year=$swing
        fi
        julian=$(datecalc -j $year $month $day)
        if ((julian > (today-2))) ; then
                print -p get $name
        fi
done
print -p bye
wait
}
rm $tmpfile
exit 0

Thanks Perderabo,

I'll get back to you with a report on my implementation,
and hopefully a few tips for the community.

//Jonas