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'