Topic "Loop through sub-directories and get single file"

Author Message
Mudassir
[View user's profile]

Joined: 2014-12-02
Posts: 5
Location: Germany
Hi,

I am using Window batch file to write a script using WinSCP.com. Idea is the following:

1. Connect to target (open ftp://root:password@xxx.xxx.xxx.xxx)
2. change remote directory to /fs/usb0/MyFolder (MyFolder has lot of subfolders like: ABC, DEF, GHI, JKL, etc. and each sub-folder has different files but one file name is same: same_file.txt). Directory structure looks like:
/fs/usb0/MyFolder/ABC/same_file.txt
/fs/usb0/MyFolder/ABC/diff1.txt
/fs/usb0/MyFolder/ABC/diff2.txt
/fs/usb0/MyFolder/DEF/same_file.txt
/fs/usb0/MyFolder/DEF/diff3.txt
/fs/usb0/MyFolder/GHI/same_file.txt
...
3. I want to copy all "same_file.txt" together folder name ( like ABC/same_file.txt) to a local folder (for example at: C:\Temp).

How can I use path mask or something else in Get command to make WinSCP.com iterate in a loop on subdirectories and fetch only one file?
I used following but it does not work:

"get /fs/usb0/MyFolder/*/same_file.txt C:\Temp\*\same_file.txt .\"

Error listing directory '/fs/usb0/MyFolder/*'.
Could not retrieve directory listing
/fs/usb0/MyFolder/*/: No such file or directory.

Regards,
Mudassir
Advertisements
Mudassir
[View user's profile]

Joined: 2014-12-02
Posts: 5
Location: Germany
Any quick help would be much appreciated. Thanks!
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
Use get -filemask="same_file.txt" /fs/usb0/MyFolder/ C:\Temp\
See https://winscp.net/eng/docs/scriptcommand_get
_________________
Martin Prikryl
Mudassir
[View user's profile]

Joined: 2014-12-02
Posts: 5
Location: Germany
Thanks prikryl for your reply.
But the problem here is that "same_file.txt" has same name in sub-folders but different contents.
Will the command you provided, download all the files to C:\Temp under their respective folder name? like C:\Temp\ABC\same_file.txt, C:\Temp\DEF\same_file.txt, C:\Temp\GHI\same_file.txt?
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
Sure, it will.
_________________
Martin Prikryl
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License