Solved script to get dynamic file name

Dell Dell gaming backpack carrying case...
April 17, 2012 at 01:33:58
Specs: Windows XP, DDR
Hi,

So for my source file name static.
consider.. filename is "IDM_CON_DATA.DAT"

so it was easy to pick up the file from ftp server by - mget IDM_CON_DATA.DAT..

Now my source file changed to dynamic.. like "IDM_CON_DATA_YYYYMMDD.DAT"
Its a weekly process.. so file name will change..

How to handle this situdation ?

Plz help

Thanks
Umar


See More: script to get dynamic file name

Report •

✔ Best Answer
April 17, 2012 at 05:36:49
Ok, so here is a small script, that downloads the IDM_CON_DATA file with the current date, e.g. IDM_CON_DATA_20120417.dat, even if there are more oder files in the directory.

---SNIP---

#!/bin/bash
# filename: idmgetfile
# Get file via FTP with actual date at the end of the file name

# first of all, set the parameters for the FTP operations


DESTFTPANSWERFILE="/tmp/ftpanswerfile"                         # define, where to store the ftp answer file
FTPSERVER="192.168.1.10"                                                     # define domainname or ip address of the ftp server
DESTDIR="/tmp"                                                                           # define local destination directory to download to:
SRCDIR="/data/transfer/temp"                                                   # define directory at the ftp server, to download from
FTPUSERNAME="ftpuser"                                                           # define ftp username
FTPPASSWD="ftp123"                                                                  # define ftp password
FNAME="IDM_CON_DATA_$(date +"%Y%m%d".dat)"           # define filename to download


# Now with this informations, we can generate the FTP answer file

echo "open $FTPSERVER" > $DESTFTPANSWERFILE                             # an already existing answer file will be overwritten
echo "user" >> $DESTFTPANSWERFILE                                                       # initiate ftp logon
echo "$FTPUSERNAME $FTPPASSWD" >> $DESTFTPANSWERFILE    # give username and password automatically
echo "binary" >> $DESTFTPANSWERFILE                                                    # set ftp transfermode to binary
echo "lcd $DESTDIR" >> $DESTFTPANSWERFILE                                     # change to local destination folder, where to store downloaded file
echo "cd $SRCDIR" >> $DESTFTPANSWERFILE                                        # change ftp source folder, from where to download the file
echo "mget $FNAME" >> $DESTFTPANSWERFILE                                     # get the source file
echo "bye" >> $DESTFTPANSWERFILE                                                         # end ftp session


# At least, start the ftp download
ftp -inA < $DESTFTPANSWERFILE

---SNIP---


You simply have to change some foldernames at the beginning of the script, to meet your needs and the ftp server address.

This should work for you.



#1
April 17, 2012 at 01:54:43
Try this:

mget IDM_CON_DATA*


Report •

#2
April 17, 2012 at 02:55:36
Yes.. Its a good option... but the problem is, if there two files in same location.. i think it will be a problem..

Thanks
Umar


Report •

#3
April 17, 2012 at 02:58:36
Do you mean 2 files with the same name but different extensions?

If so, simply use:
mget IDM_CON_DATA*.dat

Should work for you.


Report •

Related Solutions

#4
April 17, 2012 at 03:57:18
No.. It will be .dat only..

consider.. if ther are 2 files eg : IDM_CON_DATA_20120410.dat and IDM_CON_DATA_20120417.dat. then i have to process today's file which is IDM_CON_DATA_20120417.


Report •

#5
April 17, 2012 at 05:36:49
✔ Best Answer
Ok, so here is a small script, that downloads the IDM_CON_DATA file with the current date, e.g. IDM_CON_DATA_20120417.dat, even if there are more oder files in the directory.

---SNIP---

#!/bin/bash
# filename: idmgetfile
# Get file via FTP with actual date at the end of the file name

# first of all, set the parameters for the FTP operations


DESTFTPANSWERFILE="/tmp/ftpanswerfile"                         # define, where to store the ftp answer file
FTPSERVER="192.168.1.10"                                                     # define domainname or ip address of the ftp server
DESTDIR="/tmp"                                                                           # define local destination directory to download to:
SRCDIR="/data/transfer/temp"                                                   # define directory at the ftp server, to download from
FTPUSERNAME="ftpuser"                                                           # define ftp username
FTPPASSWD="ftp123"                                                                  # define ftp password
FNAME="IDM_CON_DATA_$(date +"%Y%m%d".dat)"           # define filename to download


# Now with this informations, we can generate the FTP answer file

echo "open $FTPSERVER" > $DESTFTPANSWERFILE                             # an already existing answer file will be overwritten
echo "user" >> $DESTFTPANSWERFILE                                                       # initiate ftp logon
echo "$FTPUSERNAME $FTPPASSWD" >> $DESTFTPANSWERFILE    # give username and password automatically
echo "binary" >> $DESTFTPANSWERFILE                                                    # set ftp transfermode to binary
echo "lcd $DESTDIR" >> $DESTFTPANSWERFILE                                     # change to local destination folder, where to store downloaded file
echo "cd $SRCDIR" >> $DESTFTPANSWERFILE                                        # change ftp source folder, from where to download the file
echo "mget $FNAME" >> $DESTFTPANSWERFILE                                     # get the source file
echo "bye" >> $DESTFTPANSWERFILE                                                         # end ftp session


# At least, start the ftp download
ftp -inA < $DESTFTPANSWERFILE

---SNIP---


You simply have to change some foldernames at the beginning of the script, to meet your needs and the ftp server address.

This should work for you.


Report •

Ask Question