windows - I want to fetch the name of the latest updated folder at particular path of FTP server -
using command able latest updated folder in unix
ls -t1 | head -1
but how can same in ftp server windows?
i want name of latest updated folder @ particular path of ftp server. 1 please help?
there's no easy way windows shell commands.
you can:
- use
ftp.exe
executels /path c:\local\path\listing.txt
save directory listing text file. - exit
ftp.exe
. - parse listing , find latest files. not easy task windows shell commands.
it way easier powershell script.
you can use ftpwebrequest
class. though not have easy way retrieve structured directory listing either. offers listdirectorydetails
, getdatetimestamp
methods.
see retrieving creation date of file (ftp).
or use 3rd-party library task.
for example winscp .net assembly can do:
param ( $sessionurl = "ftp://user:mypassword@example.com/", $remotepath = "/path" ) # load winscp .net assembly add-type -path "winscpnet.dll" # setup session options $sessionoptions = new-object winscp.sessionoptions $sessionoptions.parseurl($sessionurl) # connect $session = new-object winscp.session $session.open($sessionoptions) # list of files in directory $directoryinfo = $session.listdirectory($remotepath) # select recent file $latest = $directoryinfo.files | where-object { -not $_.isdirectory } | sort-object lastwritetime -descending | select-object -first 1 # file @ all? if ($latest -eq $null) { write-host "no file found" } else { write-host "the latest file $latest" }
see full example downloading recent file.
(i'm author of winscp)
Comments
Post a Comment