WinSCP - Download files with specific extension recursively
Learn how to use WinSCP's scripting feature to download files with a specific extension (like .php) recursively from your FTP/SFTP server. Includes a simple script example.
This article is over a year old but was reviewed on July 30, 2026 — the content is still accurate.
This article is part of the WinSCP & remote file transfer series:
- KeePass - Overriding the URL field
- WinSCP - Retrieve a stored password
- Fatal error was starting Putty after having saved settings
- WinSCP - Synchronize host and remote
- FTP - Remove files and folders at light speed
- WinSCP - Visual Basic for Application use
- WinSCP - Download files with specific extension recursively
- Search your FTP server at lightning speed

This article shares a WinSCP automation script that recursively downloads all files matching a given extension (e.g. .php) from a remote FTP/SFTP server to a local folder, run via winscp.com /script=... — handy for pulling down a specific file type from an entire site for local analysis.
The WinSCP FTP client supports 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.
Downloading everything just to search inside it is sometimes overkill: Search your FTP server at lightning speed does the search on the server. And once the files are local, jakzal/phpqa gives you every PHP analysis tool without installing a thing.
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:
lcd "c:\temp"
open ftp://USERNAME:PASSWORD@HOST_OR_IP/
cd /public_html
option transfer ascii
get -filemask:*.php *
close
exitThis script connects to an FTP server, navigates to a specific directory, and downloads all PHP files from it to a local folder. It's automating a file retrieval task that would otherwise require manual steps in an FTP client.
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
ftpbysftp USERNAME: the FTP usernamePASSWORD: the password associated to this accountHOST_OR_IP: the FTP host name or its 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.comfrom 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 file found under your remote folder (sub-folders included).
This article is part of the WinSCP & remote file transfer series:
- KeePass - Overriding the URL field
- WinSCP - Retrieve a stored password
- Fatal error was starting Putty after having saved settings
- WinSCP - Synchronize host and remote
- FTP - Remove files and folders at light speed
- WinSCP - Visual Basic for Application use
- WinSCP - Download files with specific extension recursively
- Search your FTP server at lightning speed


