Batch/Macro code to get latest file from FTP site - excel

We have a shared FTP site with a daily file upload which I need to download and run checks over. I'm simply trying to automate the process. The FTP file is something like name_20150901.xml.
So far I have a batch file to get the file but I can't figure out how to get the latest. Here is my code:
#ftp -i -s:"%~f0"&GOTO:EOF
open ftp.site.com
<user>
<pass>
lcd my local direc
binary
get todaysfile_20150901.xml
What changes do I need to read the files and get the newest one? My end goal is to have a macro that calls this and then reads the file that's grabbed from the FTP and runs my checks.

There's no easy way to select the most recent file with the ftp.exe.
If you know that the file has today's timestamp, you can generate the script dynamically with the today's timestamp. You can use the DATE environment variable, though it has its caveats. A more reliable (and complex) way is to use the wmic os get LocalDateTime.
See How to get current datetime on Windows command line, in a suitable format for using in a filename?
If you can determine the latest file alphabetically, you can:
run the ftp.exe with the ls command redirected to a file
sort the files by alphabet in a descending order
read the first line
generate a download script for the second ftp.exe run
WinSCP can download the files created within the last 24-hours (1 day):
winscp.com /command ^
"open ftp://user:pass#ftp.site.com/" ^
"lcd c:\my\local\direc" ^
"get *>=1D" ^
"exit"
If you really need to download the latest file (by timestamp), you probably need some more advanced scripting.
For example there's a guide available for downloading the most recent file with WinSCP.
(I'm the author of WinSCP)

Related

How to overwrite ".listing" file when using "wget" command

I have a generic script that uses wget to download the file (passed as parameter to the script) from FTP server. The script always downloads the files into the same local folder. The problem I am running into is that .listing file created by wget gets deleted by default so if the script is called in parallel for different files, whichever process gets to delete the .listing file succeeds and the rest fail.
So I tried to use --no-remove-listing along with wget command, but then I get the error:
File ".listing" already there; not retrieving.
I looked at another post but as mentioned in the comments by original poster, the question hasn't been answered even though it is marked so.
One option I was thinking about is to change the script to create subdirectory with filename and download the file there. But since it is a large script, I was trying to see if there is an easier option to just change wget command.

Linux file transfer between server automatically when a file is created

In my work, I use 2 Linux servers.
The first one is used for web-crawling and create it as a text file.
The other one is used for analyzing the text file from the web crawler.
So the issue is that when a text file is created on web-crawling server,
it needs to be transferred automatically on the analysis server.
I've used shell programming guides referring some tips,
and set up the crawling server to be able to execute the scp command without requiring the password (By using ssh-keygen command, Add ssh-key on authorized_keys file located in /root/.ssh directory)
But I cannot figure out how to programmatically transfer the file when it is created.
My job position is just data analyze (Not programming)
So, the lack of background programming knowledge is my big concern
If there is a way to trigger the scp to copy the file when it is created, please let me know.
You could use inotifywait to monitor the directory and run a command every time a file is created in the directory. In this case, you would fire off the scp command. IF you have it set up to not prompt for the password, you should be all set.
inotifywait -mrq -e CREATE --format %w%f /path/to/dir | while read FILE; do scp "$FILE"analysis_server:/path/on/anaylsis/server/; done
You can find out more about inotifywait at http://techarena51.com/index.php/inotify-tools-example/

wget to download new wildcard files and overwrite old ones

I'm currently using wget to download specific files from a remote server. The files are updated every week, but always have the same file names. e.g new upload file1.jpg will replace local file1.jpg
This is how I am grabbing them, nothing fancy :
wget -N -P /path/to/local/folder/ http://xx.xxx.xxx.xxx/remote/files/file1.jpg
This downloads file1.jpg from the remote server if it is newer than the local version then overwrites the local one with the new one.
Trouble is, I'm doing this for over 100 files every week and have set up cron jobs to fire the 100 different download scripts at specific times.
Is there a way I can use a wildcard for the file name and have just one script that fires every 5 minutes for example?
Something like....
wget -N -P /path/to/local/folder/ http://xx.xxx.xxx.xxx/remote/files/*.jpg
Will that work? Will it check the local folder for all current file names, see what is new and then download and overwrite only the new ones? Also, is there any danger of it downloading partially uploaded files on the remote server?
I know that some kind of file sync script between servers would be a better option but they all look pretty complicated to set up.
Many thanks!
You can specify the files to be downloaded one by one in a text file, and then pass that file name using option -i or --input-file.
e.g. contents of list.txt:
http://xx.xxx.xxx.xxx/remote/files/file1.jpg
http://xx.xxx.xxx.xxx/remote/files/file2.jpg
http://xx.xxx.xxx.xxx/remote/files/file3.jpg
....
then
wget .... --input-file list.txt
Alternatively, If all your *.jpg files are linked from a particular HTML page, you can use recursive downloading, i.e. let wget follow links on your page to all linked resources. You might need to limit the "recursion level" and file types in order to prevent downloading too much. See wget --help for more info.
wget .... --recursive --level=1 --accept=jpg --no-parent http://.../your-index-page.html

wget newest file in another server's folder

I have an automatic backup of a file running on a cronjob. It outputs into a folder, let's call /backup, and appends a timestamp to each file, every hour, like so:
file_08_07_2013_01_00_00.txt, file_08_07_2013_02_00_00.txt, etc.
I want to download these to another server, to keep as a separate backup. I normally just use wget and download a specific file, but was wondering how I could automate this, ideally every hour it would download the most recent file.
What would I need to look into to set this up?
Thanks!
wget can handle that, just enable time-stamping. I'm not even going to attempt my own explanation, here's a direct quote from the manual:
The usage of time-stamping is simple. Say you would like to download a
file so that it keeps its date of modification.
wget -S http://www.gnu.ai.mit.edu/
A simple ls -l shows that the time stamp on the local file equals the state of the Last-Modified
header, as returned by the server. As you can see, the time-stamping
info is preserved locally, even without ā€˜-Nā€™ (at least for http).
Several days later, you would like Wget to check if the remote file
has changed, and download it if it has.
wget -N http://www.gnu.ai.mit.edu/
Wget will ask the server for the last-modified date. If the local file has the same timestamp as
the server, or a newer one, the remote file will not be re-fetched.
However, if the remote file is more recent, Wget will proceed to fetch
it.

get the current HEAD version of a CVS file on the server

Using the rlog command I can analyze the commit log to a file on the CVS server itself (that is, directly accessing the file ending in ",v"). That's fine.
Is there a similar command line utility that prints the current HEAD version of that file to stdout?
I need this for a custom CVS status utility (something like ViewVC, but made specifically for a certain repository) that will be written in PHP.
To print the content of the file that would be checked out, just use co -p filename. That will print a small header including the revision number to stderr, and the content of the file to stdout.
You probably want cvs log filename. Not sure buy you might need to do cvs update filename first.

Resources