user3030931
user3030931

Reputation: 13

Comparing part of a filename in a windows batch file

I need help with comparing the filenames in a dir and get the latest one over FTP. The directory on the FTP server has n number of files, all suffixed with modified date. For e.g there are 3 files on the FTP server : Test_20131125.txt Test_20131124.txt Test_20131123.txt.

The filenames will always begin with Test_ but the date portion will vary. Now I have to get the latest one among these which would be Test_20131125.txt in the example. I want to use a batch file to do so but not sure how to go about it.

Thanks,

Upvotes: 0

Views: 151

Answers (1)

Matt Williamson
Matt Williamson

Reputation: 7095

Here is how I would do it:

  1. create a temp directory and download all of the files to it using a scripted ftp file. (google for specifics)
  2. use for /f %%a in ('%temp%\folder dir /b /od') set latest=%%a
    to get the latest file.
  3. copy the latest file where you want it and remove the temp directory.

Something like this:

MD %temp%\ftpdown
ftp -s:myftp.txt 
for /f %%a in ('%temp%\ftpdown dir /b /od') set latest=%%a
copy /Y %latest% wherever
rd /q /s %temp%\ftpdown

This is fine if there are only a few smallish files because you're downloading them all locally to do the compare. If it's a large directory or the files are big, it's better to download a dir/ls listing from the server and extract the filename from it then write an ftp script file to get than file. It all depends.

Upvotes: 1

Related Questions