File search variables

I have a remote server that generates files with name format:
Daily_Generated_File_11-14-07.txt.

The file name remains the same regardless, but the date portion at the end of the name will change (mm-dd-yy). Can someone supply a script that will search for files with the latest date based on the file name?

How will the code get the list of files to search?

Could you not just start at today and work backwards?

No this is something new, so it will search for files today going forward.

So, today, you have files with tomorrow's date?

How does this code get the list of files?

Huh? I think you miss understood.

I need the script to look in a remote directory for today's files. I came up with this and it works, but now how to I get this to run on a remote FTP Server :confused:

FILENAME="Daily_Generated_File_$(date +%m-%d-%y).txt"
find . -type f -name $FILENAME

Okay, it's FTP then.

I suggest you first use FTP to get a listing then enumerate through that listing and decide which is the latest.

Then when you know the name, use get to download it.

Whether you use two ftp sessions or one depends on how fancy you want to do your coding.

Sounds about right.

Now, if only someone would be so gracious as to get me started on this .. :rolleyes:

#!/bin/ksh

ftp -nv <<EOF
open remoteServer
user userName userPassword
cd desiredDirectory
mls Daily_Generated_File_* myLocalListingFile
EOF
# once you get the 'myLocalListingFile' on the LOCAL server, go through files listed in the 
# file and decide which one is the latest.
# Once decided, ftp to the remote server and get the desired file
ftp -nv <<EOF
open remoteServer
user userName userPassword
cd desiredDirectory
get desiredLatestFile
EOF

If I knew where to find you, I would and kiss ya! :smiley:

I got the file listing, THANKS! But now, how to I go through the list, select the latest file, the have FTP go back and grab that one file? :confused:

nawk -F_ -f bbb.awk myLocalListingFile | sort -k1n | cut -f2- | tail -1

bbb.awk:

{
  d=substr($NF, 1, index($NF, ".")-1)
  split(d, dA, "-")
  print dA[3] dA[1] dA[2] "\t" $0
}

where 'myLocalListingFile' looks something similar to:

Daily_Generated_File_11-14-07.txt
Daily_Generated_File_11-17-07.txt
Daily_Generated_File_11-24-07.txt
Daily_Generated_File_11-13-07.txt

ok great!

We're almost there. Now, how to I get the FTP to get that file?

ftp -nv <<EOF
open remoteServer
user userName userPassword
cd desiredDirectory
get $(nawk -F_ -f bbb.awk Filelisting.txt | sort -k1n | cut -f2- | tail -1)
EOF

?

sure.
Or you can put it all in ONE ftp session:
connecting
cd-ing
getting the listing
'munging' the listing file
getting the LATEST file

Here's what I tried:

ftp -nv <<EOF
open ftpserver.com
user username password
mls Daily_Generated_File_* Filelisting.txt
get $(nawk -F_ -f bbb.awk Filelisting.txt | sort -k1n | cut -f2- | tail -1)
EOF

SWEET!!!!!!

You must be ROOT!

one lil change:

ftp -nv <<EOF
open ftpserver.com
user username password
mls "Daily_Generated_File_*" Filelisting.txt
get $(nawk -F_ -f bbb.awk Filelisting.txt | sort -k1n | cut -f2- | tail -1)
EOF

you want the wild-card expansion to happen on the REMOTE machine - not in the current working directory on the LOCAL machine.

Thanks for the update. Here's another change: Apparently the files that are generated are a day behind, so today's file 12-19-07 won't be available until 12-20-07. The script you so graciously gave me, will only look for today's file. How can the script be edited to look for yesterdays file? I'm assuming it would be in the

"get $(nawk -F_ -f bbb.awk Filelisting.txt | sort -k1n | cut -f2- | tail -1)"

That would have to change.

Thanks again.

Hi,
This one is a little difficult. But should work for you.

1> find . -name Daily_Generated_File_\*
---> find all the related file in current directory
2> sed 's/\(Daily_Generated_File_\)\([0-9][0-9]\)\(-[0-9][0-9]-\)\([0-9][0-9]\)\(.txt\)/\1\4\3\2\5/'
---> since the file is in MM-DD-YY format, change it to YY-MM-DD format
3> sort -t -
---> sort the result with '-' as delimite
4> tail -1
---> get the last one which is the latest according to the date
5> sed 's/\(Daily_Generated_File_\)\([0-9][0-9]\)\(-[0-9][0-9]-\)\([0-9][0-9]\)\(.txt\)/\1\4\3\2\5/'
---> change from 'YY-MM-DD' back to 'MM-DD-YY'

code:

find . -name Daily_Generated_File_\* |sed 's/\(Daily_Generated_File_\)\([0-9][0-9]\)\(-[0-9][0-9]-\)\([0-9][0-9]\)\(.txt\)/\1\4\3\2\5/' |sort -t - | tail -1 |sed 's/\(Daily_Generated_File_\)\([0-9][0-9]\)\(-[0-9][0-9]-\)\([0-9][0-9]\)\(.txt\)/\1\4\3\2\5/'

The script 'gets' the LAST file (sorted by 'date'). If your file generation lags a day, then the 'last' available file will from yesterday (assuming files are generated on the daily bases with no gaps).
Unless I'm missing something obvious.....

You're right. But when I ran the script, it kept coming back with nothing found. Then it hit me "duh! if it's looking for today's file then..." so i changed the date on one of the files in the dir to todays file and ran the script again with the "echo" and it spit out the file with today's date.

Hm..... I'm not sure if it's good or bad.....
There's nothing in the script that deals with either today OR yesterday.
As I said previously - the script gets the LAST file it finds (sorted by the 'date').
Not sure if everything is kosher now or not.........

How does this work for me? This searches the current directory. I need something that will search a remote FTP Server. Not the local server.