Find and get a file from unknown remote subdirectory

Advertisement

Poulman
Joined:
Posts:
2

Find and get a file from unknown remote subdirectory

Hello all.

It's been a couple of days I'm trying WinSCP.
Till now I've been using FileZilla but it doesn't support command line automation.
My usual tasks are searching for ZIP files by given name in a specific remote folder and its subdirectory.
It's unknown which subdirectory contains the file. For example:
I search for the latest version of file "FILE*.ZIP". The sure subdirectory where the ZIP files are to be searched is like "/main/files" and there might be a "FILE_V1.ZIP" in "/main/files/Jan", a "FILE_V2.ZIP" also in "/main/files/Jan" but a "FILE_V3.ZIP" might be in "/main/files/Mar", which is the latest version of the "FILE*.ZIP" that I'm searching.
The job until now could only be done in Filezilla manually by opening the "file search" window, set in the "/main/files" remote subdirectory and pressing the "search" button. Then I was getting a list of all the "FILE*.ZIP" files found in every subdirectory of every level, chose the latest version and download the file manually (flattening).
With WinSCP I managed to use the "get" command in cmd to download the latest version of a remote file from a specific subdirectory but -of course- there might be an even newest version in another subdirectory of the same or lower level.
The command I'm using is like this:
C:\Users\me\AppData\Local\WinSCP\WinSCP.com /log="C:\Users\me\AppData\Local\WinSCP\WinSCP.log" /ini=nul /command "open ftp://sitename:password8@ip.ip.ip.ip/ -rawsettings ProxyPort=0" "cd /main/files/2018/01-1" "lcd C:\Users\me\Desktop" "get -latest File123*.zip" "exit"
There are "File123_V1" and "File123_V2" in "/main/files/2018/01-1" subdirectory but there is also "File123_V3" in "/main/files/2018/01" subdirectory which is -of course- omitted from the search.
So, could you please direct me how could I search for a file in all subdirectories by giving the part of its name?
Thanks in advance.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
28,123
Location:
Prague, Czechia

Re: Find and get a file from unknown remote subdirectory

You cannot do this with WinSCP scripting.

But it's easy with WinSCP .NET assembly, e.g. from a PowerShell script.

Start with this PowerShell script:
https://winscp.net/eng/docs/script_download_most_recent_file#powershell
(which does the same as your WinSCP script).

And replace the simple $directoryInfo = $session.ListDirectory($remotePath) and $directoryInfo.Files with $session.EnumerateRemoteFiles($remotePath, "FILE*.ZIP", [WinSCP.EnumerationOptions]::AllDirectories)

Reply with quote

Guest

Well, thanks for the reply but unfortunately I'm not a programmer and I can't understand a bit.
I don't even know what power script is.
Well, I have to accept that it's not achievable,
Thanks anyway...

Reply with quote

Poulman
Joined:
Posts:
2

Eventually, I used "brute force" searching by repeating the command line, starting from latest directory upwards and when the latest version of the file is found, exit the procedure.
At least winscp is very helpful at this type of search.
The only people is that the ZIP file cannot be opened immediately because it seems to be used by winscp for a fraction of a second, so I'll have to wait a little after downloading each one and before opening them.
Thanks WINSCP. Now I can save lot of time and repeating clicks (good for my mouse too!)

Reply with quote

Advertisement

You can post new topics in this forum