computing
  • 3

Solved Script To Get Dynamic File Name

  • 3

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

Share

1 Answer

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

    • 0