<!--
  canonical-url: https://www.avonture.be/blog/winscp-download-recursively-files
  published:     2024-08-22
  This is a static plain-Markdown mirror generated at build time.
  Visit the canonical URL above for the fully rendered page, with images and interactive components.
-->

# 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.

---
![WinSCP - Download files with specific extension recursively](/img/v2/winscp.webp)

**TL;DR**

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](https://winscp.net/) 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](/blog/php-grep-searching-at-lightning-speed) does the search **on** the server. And once the files are local, [jakzal/phpqa](/blog/php-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:

**C:\temp\download.txt**

```txt
lcd "c:\temp"

open ftp://USERNAME:PASSWORD@HOST_OR_IP/

cd /public_html

option transfer ascii

get -filemask:*.php *

close

exit
```

## How to use

1. Save the previous script as, f.i., `C:\temp\download.txt`
2. Edit the script and make these changes:
   1. Where files should be downloaded, local folder (line `lcd "c:\temp"`)
   2. In case of need, replace `ftp` by `sftp`
   3. `USERNAME`: the FTP username
   4. `PASSWORD`: the password associated to this account
   5. `HOST_OR_IP`: the FTP host name or its IP
   6. The remote folder from where the files should be downloaded (line `cd /public_html`)
   7. The file extension to download (if not `.php`) (line `get -filemask:*.php *`)
   8. Save the script
3. Start a DOS session
4. Run `cd c:\temp`
5. 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` file found under your remote folder (sub-folders included).
