Hello Dears,

I want to download all files that have been loaded to a directory with FTP automatically with the below code. the code can run but with below error:

Can't call method "mtime" on an undefined value at -e line 1.

Also, however it gets above error it downloads all the files in the path, not only added newly added files. I really appreciate if you help me.

I want to schedule this Perl code to be run at a specific time, so every day I don't need to get the files manually.
#!/bin/bash use strict; use warnings; use diagnostics; # Login credentials user='*****' #Do not forget to enclose inside single or double quotes pass='*****' directory='Remoredirectory' host='RemoteIP' # Set time # perl -e 'print time' will return the current time in Unix epoch form +at # So if you substract 43200, that should give you the current time min +us 30 minutes in Unix epoch format: time=$(perl -e 'print time-43200') # Connect to host and download the listing of the remote directory ftp -n $host <<END_GET_LIST quote USER $user quote PASS $pass cd $directory ls -l /Localdirectory/ftpList.txt quit END_GET_LIST # Disconnect from remote host # Save the 6th, 7th, and 8th field of the directory listing (i.e. Aug +15 5:15) of each line into file Dates.txt awk -F ' ' '{print $6,$7,$8}'/Localdirectory/ftpList.txt > /Localdirec +tory/'Dates.txt' # Save the 9th field of the directory listing (file name) of each line + into file Files.txt awk -F ' ' '{print $9}' /Localdirectory/ftpList.txt > /Localdirectory/ +'Files.txt' linenum=0 #Auxiliary variable for sed. while read list; do linenum=$((linenum+1)) #Convert the modification datetime of each file to Unix's epoc +h epoch=$(perl -MFile::stat -e "print stat('$list')->mtime") if [ $epoch -gt $time ] ; then file=$(sed -n "${linenum}p" Files.txt) #If the condit +ion is satisfied, use sed to get the name of the file #in the same line of Files.txt. #Connect again and download file when the condition above has been sat +isfied. ftp -n $host <<END_RETRIEVE quote USER $user quote PASS $pass cd $directory binary get $file quit END_RETRIEVE fi done < /Localdirectory/'Dates.txt'

In reply to Downloading only new files added to a local path with Perl (FTP) by SaraMirabi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.