# WinSCP & remote file transfer A series of 8 article(s), in reading order. # KeePass - Overriding the URL field > Learn how to override the KeePass URL field to launch programs like PuTTY, WinSCP, or a Remote Desktop connection (RDP) directly from your entries. --- ![KeePass - Overriding the URL field](/img/v2/keepass.webp) **TL;DR** This article shows how to repurpose the KeePass `URL` field to launch external programs instead of a web browser, using the `cmd://` prefix and entry placeholders: starting PuTTY with saved credentials, opening WinSCP directly to an SFTP path, or launching a Windows Remote Desktop (`mstsc.exe`) connection to a saved host. The `url` field can also be used to start a program and, thus, not only to contain a valid URL. The official documentation is located here: . ## Start PuTTY from KeePass If you don't have PuTTY installed on your machine, download it from . You just need the `putty.exe` file. *If PuTTY refuses your saved session with an authentication error, see [Fatal error was starting Putty after having saved settings](/blog/putty-no-supported-authentication-methods).* Download it and save `putty.exe` in a directory present in your PATH. To do this, even if you're not an administrator of your machine, just start a MS-DOS console and run `echo %PATH%`. You'll then see the list of directories already present and choose the one where you can copy a new file. Then copy `putty.exe` in that folder. So, from now on, you can just run `putty.exe` from anywhere. Update your KeePass entry and set the `URL` property to the following instruction: `cmd://putty.exe -load "Default Settings" {S:host} -l {USERNAME} -pw {PASSWORD}` > **Caution:** > > Make sure you have filled in the `host` advanced property. ## Start WinSCP from KeePass > If you wish to open WinSCP (see also [WinSCP - Synchronize host and remote](/blog/winscp-synchronize-both)) and see files, you can achieve this by updating your KeePass entry and setting the `URL` property to the following instruction: `cmd://"{ENV_PROGRAMFILES_X86}\WinSCP\WinSCP.exe" sftp://{USERNAME}:{PASSWORD}@{S:ip}:{T-REPLACE-RX:/{S:port}/-1//}{S:path}` > **Caution:** > > Make sure you have filled in the `ip` advanced property. You can also set the `port` and `path` properties; they are optional, however. ## Start an RDP/TS connection from KeePass > You can also start a remote desktop / terminal server connection from within KeePass. The URL has to be set like this: `cmd://mstsc.exe /v:{S:host} /f` > **Caution:** > > Make sure you have filled in the `host` advanced property. --- # WinSCP - Retrieve a stored password > Did you forget a saved WinSCP password? Follow this quick guide to enable logging and retrieve your stored password in plain text from your session log file. --- ![WinSCP - Retrieve a stored password](/img/v2/winscp.webp) **TL;DR** This article shows how to recover a forgotten WinSCP password by temporarily enabling "Log passwords and other sensitive information" in the Logging preferences, reconnecting to the site so it's written to the session log in `%TEMP%`, then reading the plain-text password from that log — with a reminder to disable logging and delete the log file afterward. More than once I've found myself in the situation where I've got a site saved in the [WinSCP](https://winscp.net/) configuration where I've saved the password and, um, gosh, what was it? *The real cure is to stop typing passwords at all: [How to connect to your hosting server using SSH](/blog/connect-using-ssh-to-your-hosting-server) sets up key-based authentication, and [KeePass - Overriding the URL field](/blog/keepass-overriding-url) opens WinSCP with the right credentials straight from your vault.* Did you know WinSCP provides an option to show you, in plain text, a stored password? To do this, just go to the `Preferences` menu then go to `Logging` and check *Log passwords and other sensitive information*. ![Log password](./images/log_password.webp) Click on `Ok` and double-click on your site so you'll indeed make a connection. Start a Windows Explorer and go to your `%TEMP%` directory. Sort on the last-modified date/time. You should retrieve a file having the same name as your session and having `.log` as file extension. > **Don't forget to remove the file and uncheck the box:** > > Please return to the `preferences` - `Logging` page and uncheck the box. You don't want this to happen every time. Fighting with PuTTY instead? See [Fatal error was starting Putty after having saved settings](/blog/putty-no-supported-authentication-methods). And if you'd rather stop relying on stored passwords altogether, [SSH - Launch a terminal on your session without having to authenticate yourself](/blog/linux-ssh-scp) shows how to switch to key-based authentication. --- # Fatal error was starting Putty after having saved settings > Fix the frustrating PuTTY 'No supported authentication methods available' fatal error after saving settings. Learn the quick solution by deleting a specific PuTTY registry entry. --- ![Fatal error was starting Putty after having saved settings](/img/v2/putty.webp) **TL;DR** This article fixes PuTTY's "No supported authentication methods available" fatal error that can appear after saving default settings: delete the `Default%20Settings` entry under `HKEY_CURRENT_USER\SOFTWARE\SimonTatham\PuTTY\Sessions` in the Windows registry to reset PuTTY's defaults. A few days ago, I updated my Putty configuration to set the default font-size to 12; no more 10. Today, when starting Putty — whichever server I wanted to connect to — I got the *No supported authentication methods available* error, followed, in my case, by *(server sent: publickey, gssapi-keyex, gssapi-with-mic, keyboard-interactive)*. I only understood the origin of the problem by looking at the window caption: Putty was trying to connect to a server other than the one I wanted. Why? And, ah, ok, the last time I modified my settings I was working on that server so, the solution should come from: *how can I reset my Putty settings?* The solution has been provided by @makurison on . Simply start `regedit.exe`, search for the key `Computer\HKEY_CURRENT_USER\SOFTWARE\SimonTatham\PuTTY\Sessions` and, there, remove the entry called `Default%20Settings`. *While you're at it: [KeePass - Overriding the URL field](/blog/keepass-overriding-url) shows how to launch a PuTTY session straight from a KeePass entry, credentials included — and [Windows Terminal - Adding a SSH profile](/blog/windows-terminal-ssh-profile) does the same without PuTTY at all.* > **Tired of session/password hassles with Putty?:** > > If you're also fighting with a forgotten stored password, see [WinSCP - Retrieve a stored password](/blog/winscp-retrieve-password). Or better, switch to key-based authentication once and for all: [SSH - Launch a terminal on your session without having to authenticate yourself](/blog/linux-ssh-scp). --- # WinSCP - Synchronize host and remote > Use a script with WinSCP to easily synchronize files between your local host and a remote FTP/SFTP server. Learn the commands for local, remote, or bi-directional synchronization. --- ![WinSCP - Synchronize host and remote](/img/v2/winscp.webp) **TL;DR** This article shares a WinSCP script that synchronizes a local folder with a remote FTP/SFTP server (or vice versa) via the `synchronize` command, run with `winscp.com /script=...`. It explains the `local`/`remote`/`both` direction options, useful for automated backups like syncing a folder to a Synology NAS. By the use of a script, it's possible to ask [WinSCP](https://winscp.net/) to synchronize your host and remote machine i.e. if a file is newer on the host, copy it to your remote server and the opposite. If a file has been added to your host, copy it to your remote server and vice versa. I'm using such script for making a full backup of some of my folders to my Synology. *I've also used it for years to deploy this very blog, before switching to [GitHub Actions](/blog/github-action).* *Two more WinSCP scripts on this blog: [downloading files with a specific extension recursively](/blog/winscp-download-recursively-files) and [opening a WinSCP session straight from KeePass](/blog/keepass-overriding-url).* ## The script **c:\temp\synchronize.txt** ```txt open ftp://USERNAME:PASSWORD@HOST_OR_IP:PORT/ option transfer ascii lcd "C:\Christophe" cd /Christophe synchronize remote -preservetime -transfer=automatic -filemask="|.git/" close exit ``` ## How to use 1. Save the previous script as, f.i., `c:\temp\synchronize.txt` 2. Edit the script and specify: 1. in case of need, replace `ftp` by `sftp` 2. `USERNAME`: the FTP username 3. `PASSWORD`: the password associated to this account 4. `HOST_OR_IP`: the FTP host name or its IP 5. `PORT`: the port to use (21 for an FTP connection f.i.) 6. The local folder where your files are stored (line `lcd "C:\Christophe"`) 7. The remote folder from where the files should be copied (line `cd /Christophe`), can be the ftp root or any subfolder 3. Start a DOS session 4. Run `cd \temp` 5. Run `winscp.com` from there: type `"c:\program files (x86)\WinSCP\WinSCP.com" /script="c:\temp\synchronize.txt"` If everything is correctly set up, WinSCP will start a session terminal and will start the synchronization. ## More info More info about the Synchronize verb of WinSCP: . * If files/folders are already there, don't do anything. * If there are new files/folders, copy them. * If files/folders are no more on the local drive, remove them from the remote server. So local is the master. ### Local / Remote / Both See the line `synchronize remote`. Choose `remote`, `local` or `both`: * When the first parameter is `local`, changes from remote directories are applied to local directories. * When the first parameter is `remote`, changes from the local directories are applied to the remote directories. * When the first parameter is `both`, both local and remote directories can be modified ([source](https://winscp.net/eng/docs/scriptcommand_synchronize#remarks)). --- # FTP - Remove files and folders at light speed > Stop the slow process of deleting files via FTP! Learn how to remove entire websites and folders at lightning speed using a simple, self-deleting PHP script—even without SSH access. --- ![FTP - Remove files and folders at light speed](/img/v2/winscp.webp) **TL;DR** This article shares a small `erase.php` script you can upload via FTP and run once from the browser to instantly wipe an entire website folder server-side, avoiding the painfully slow file-by-file deletion through an FTP client — with a strong warning that it deletes everything immediately, with no confirmation or backup. Have you ever deleted a website from your FTP client? It's easy, just select the folder containing the website and press the `delete` key. Easy, simple and ... so slow. It takes ages to delete files and folders. Several dozen minutes! Ouch. If you've [an SSH connection to your web server](/blog/connect-using-ssh-to-your-hosting-server) and if you're comfortable with it; you can `rm -rf` the folder and it'll be done in seconds. But what if you don't have SSH or fear to make errors? There is an alternative, the `erase.php` script. Open your FTP client, open your website folder (like `/var/www/html/old_site`); the one you wish to remove and create a new file called f.i. `erase.php` then copy/paste the code below in it. **erase.php** ```php Deleting all files and subfolders in '.$directory.''; $it = new RecursiveDirectoryIterator($directory); foreach (new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST) as $file) { if ( ($file->isDir()) && (!in_array($file->getFilename(), array('.', '..'))) ){ echo '

Deleting folder '.$file->getPathname().'

'; rmdir($file->getPathname()); } else { if ( ($file->isFile()) && ($file->getPathname()!=__FILE__) ) { echo '

Deleting file '.$file->getPathname().'

'; unlink ($file->getPathname()); } } } echo '

Deletion complete. Only the '.$script_name.' file remains in the folder.

'; unlink(__FILE__); ?> ``` Start your browser, navigate to your website (`http://your_old_site.com`) and add `/erase.php` at the end to run the script. > **Make sure this is what you want:** > > Be really sure this is what you want > **Be extremely sure this is what you want:** > > The script will then start immediately and will remove everything without asking confirmation nor backup. Just killing files and folders. > > At the end of the process (i.e. in just a few seconds), the folder will be completely empty. > > *And, exactly like [the QuickScan scanner](/blog/aesecure-quickscan), remember to delete `erase.php` from your server as soon as you're done: a script able to wipe a folder has no business staying reachable from the Internet.* --- # WinSCP - Visual Basic for Application use > Automate secure SFTP/FTP file transfers directly from MS Office. Learn how to call the WinSCP DLLs using simple Visual Basic for Application (VBA) code with a practical example. --- ![WinSCP - Visual Basic for Application use](/img/v2/winscp.webp) **TL;DR** This article shows how to call WinSCP's .NET/COM library from VBA (e.g. in Excel) to automate secure file transfers: a `Session` object connects over SFTP with host, credentials, and SSH host key fingerprint, then `PutFiles` uploads files, with error handling via `On Error Resume Next` and a results loop confirming each transfer. [WinSCP](https://winscp.net/) is a free, open-source file manager for securely transferring files between your computer and remote servers using protocols like SFTP, FTP, SCP, and WebDAV. Did you know that you can call WinSCP from ... VBA code? See . There are also several posts in the forum: The [WinSCP](https://winscp.net/) DLLs can be called up through a VBA code and therefore it would be possible to upload / download a file to / from an FTP server. *If VBA isn't a requirement, WinSCP can do the same thing with its own scripting language, which is far simpler; see [WinSCP - Synchronize host and remote](/blog/winscp-synchronize-both) and [WinSCP - Download files with specific extension recursively](/blog/winscp-download-recursively-files).* Below a simple VBA example you can use in f.i. MS Excel to upload a file to your remote server. ```vbnet Option Explicit Sub Example() Dim mySession As New Session ' Enable custom error handling On Error Resume Next Upload mySession ' Query for errors If Err.Number <> 0 Then MsgBox "Error: " & Err.Description ' Clear the error Err.Clear End If ' Disconnect, clean up mySession.Dispose ' Restore default error handling On Error GoTo 0 End Sub Private Sub Upload(ByRef mySession As Session) ' Setup session options Dim mySessionOptions As New SessionOptions With mySessionOptions .Protocol = Protocol_Sftp .HostName = "example.com" .UserName = "user" .Password = "mypassword" .SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" End With ' Connect mySession.Open mySessionOptions ' Upload files Dim myTransferOptions As New TransferOptions myTransferOptions.TransferMode = TransferMode_Binary Dim transferResult As TransferOperationResult Set transferResult = mySession.PutFiles("c:\temp\*", "/home/user/", False, myTransferOptions) ' Throw on any error transferResult.Check ' Display results Dim transfer As TransferEventArgs For Each transfer In transferResult.Transfers MsgBox "Upload of " & transfer.Filename & " succeeded" Next End Sub ``` --- # 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 . 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). --- # Search your FTP server at lightning speed > Tired of painfully slow FTP search functions? Discover php_grep, the ultra-fast PHP script that uses regular expressions to find content on your FTP server at lightning speed. --- ![Search your FTP server at lightning speed](/img/v2/winscp.webp) **TL;DR** Searching for text on an FTP server can be incredibly slow, especially with tools like WinSCP's built-in search. This article introduces `php_grep`, a lightweight and ultra-fast PHP script that revolutionizes FTP searching. By simply uploading this single file to your server and accessing it via a URL, you can perform lightning-fast, regular-expression-based searches for content within your files, and even filter results by file type. Did you know that using [WinSCP](https://winscp.net/), you can run a search on your FTP to find all the documents containing a given string? It's native in WinSCP; nothing more to install but... *Other WinSCP tricks on this blog: [download files with a specific extension recursively](/blog/winscp-download-recursively-files) and [synchronize host and remote](/blog/winscp-synchronize-both).* However, it's insanely slow; let's see how we can do it better and almost instantaneous. ## WinSCP - Search for Text Probably you never had noticed (it was my case), you can start searching files on your FTP server using WinSCP. To do this, go to the `Commands` menu, then `Static custom` commands and finally `Search for Text...`. Type your search pattern and press `Enter`. ![Search for Text](./images/search_for_text.webp) A new Powershell prompt will be started and you'll see the list of scanned files. ![Result](./images/result.webp) > **It's a Powershell script:** > > WinSCP will, in fact, start a `.ps1` script located here: `C:\Program Files (x86)\WinSCP\Extensions\SearchText.WinSCPextension.ps1`. Feel free to update the file to match your needs. It's terribly slow and you'll get a huge list with all your files; not only the ones where the search pattern is retrieved. This function has the merit of existing, but it's not much. ## php\_grep Time flies... In 2016, I developed an ultra-fast little script called [php\_grep](https://github.com/cavo789/php_grep) that scans every file present and, using a small regular expression, finds the files containing the search pattern and displays its location if the search is successful. You just need to download a copy of my script (a single PHP page), copy the script on your FTP server and access it using a URL. Really easy. This done, you'll get an interface where you can type, for sure, the search expression but also restricts the search to f.i. `.html` files. ![php\_grep in action](./images/php_grep.webp) More info on *The trick here is to search **on the server**, so nothing has to travel over the wire. When the files are already on your own disk, the same "lightning speed" feeling is provided by [ripgrep](/blog/ripgrep).* ---