WinSCP - Download files with specific extension recursively
ยท 2 min read
The WinSCP FTP client support scripting as we can read on https://winscp.net/eng/docs/guide_automation.
In a previous life, I was often faced with the need to download a certain type of file, e.g. connect to an FTP server and retrieve PHP files locally for analysis.
Since WinSCP allows automation, it's easy to write a little script to do just that.
As an example, we'll thus download any .php
files from a host.
The scriptโ
The script is pretty straightforward, if you can believe it:
option batch abort
option confirm off
lcd "c:\temp"
open ftp://USERNAME:PASSWORD@HOST_OR_IP/
cd /public_html
option transfer ascii
get -filemask:*.php *
close
exit
How to useโ
- Save the previous script as, f.i.,
C:\temp\download.txt
- Edit the script and make these changes:
- Where files should be downloaded, local folder (line
lcd "c:\temp"
) - In case of need, replace
ftp
bysftp
USERNAME
: the FTP usernamePASSWORD
: the password associated to this accountHOST_OR_IP
: the FTP host name or his IP- The remote folder from where the files should be downloaded (line
cd /public_html
) - The file extension to download (if not
.php
) (lineget -filemask:*.php *
) - Save the script
- Where files should be downloaded, local folder (line
- Start a DOS session
- Run
cd c:\temp
- Run
winscp.com
from there: type"C:\Program Files (x86)\WinSCP\WinSCP.com" /script="c:\temp\download.txt"
If everything is correctly set up, WinSCP will start a session terminal and will start to download each .php
files found under your remote folder (sub-folders included).