{"id":13705399,"url":"https://github.com/cavo789/tools_winscp","last_synced_at":"2025-05-05T16:32:39.312Z","repository":{"id":101354653,"uuid":"82780906","full_name":"cavo789/tools_winscp","owner":"cavo789","description":"Tips and tricks and small scripts for automation of the WinSCP FTP client (Windows).","archived":true,"fork":false,"pushed_at":"2023-09-03T07:16:11.000Z","size":405,"stargazers_count":16,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-03T22:15:05.716Z","etag":null,"topics":["ftp-client","windows","winscp"],"latest_commit_sha":null,"homepage":"","language":"Batchfile","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cavo789.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-02-22T08:40:43.000Z","updated_at":"2024-04-25T15:27:11.000Z","dependencies_parsed_at":"2024-01-14T20:16:46.534Z","dependency_job_id":"f68cc311-37b3-474e-8ce3-173ce6e4a4d7","html_url":"https://github.com/cavo789/tools_winscp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cavo789%2Ftools_winscp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cavo789%2Ftools_winscp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cavo789%2Ftools_winscp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cavo789%2Ftools_winscp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cavo789","download_url":"https://codeload.github.com/cavo789/tools_winscp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224455866,"owners_count":17314200,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ftp-client","windows","winscp"],"created_at":"2024-08-02T22:00:40.040Z","updated_at":"2024-11-13T13:30:30.621Z","avatar_url":"https://github.com/cavo789.png","language":"Batchfile","funding_links":[],"categories":["Batchfile"],"sub_categories":[],"readme":"﻿# WinSCP\n\n![Banner](./banner.svg)\n\n\u003e Tips and tricks and small scripts for automation of the WinSCP FTP client (Windows).\n\n\u003c!-- table-of-contents - start --\u003e\n* [Tips](#tips)\n    * [Start PuTTY without typing a password](#start-putty-without-typing-a-password)\n    * [Retrieve password](#retrieve-password)\n    * [Search for files that contain a keyword](#search-for-files-that-contain-a-keyword)\n    * [Super User - Running command with SUDO SU](#super-user-running-command-with-sudo-su)\n* [Scripts](#scripts)\n    * [Download files with specific extension recursively](#download-files-with-specific-extension-recursively)\n       * [The script](#the-script)\n       * [How to use](#how-to-use)\n    * [Synchronize files from a local path to a remote drive](#synchronize-files-from-a-local-path-to-a-remote-drive)\n       * [The script](#the-script)\n       * [How to use](#how-to-use)\n       * [More info](#more-info)\n          * [Local / Remote / Both](#local-remote-both)\n    * [VBA - Visual Basic for Application](#vba-visual-basic-for-application)\n* [License](#license)\n\u003c!-- table-of-contents - end --\u003e\n\n## Tips\n\n### Start PuTTY without typing a password\n\nWe can start a PuTTY console without typing the SSH password but just reuse the one of the FTP connection.\n\n![PuTTY](./010-tips/PuTTY/images/WinSCP_Putty.png)\n\n1. open your `Preferences`\n2. in the `Integration`-\u003e`Application` tab (1) be sure to check `Remember session password and pass it to PuTTY` (2).\n\nThis way, you just need to provide the login name and that’s it.\n\n![Remember password](./010-tips/PuTTY/images/WinSCP_Putty-RememberPassword.png)\n\n### Retrieve password\n\n![Retrieve password](./010-tips/Retrieve-password/images/Retrieve_password.png)\n\nTo retrieve an encrypted password with WinSCP, you'll need to enable the logging (see 1 and 2) and enable storing password (3).\n\n1. Click on the `Report` menu item\n2. Click on the `Enable session saving`\n3. Click on the `Save password`\n\nA file will then be created in the `%TEMP%` folder. His name will be the session name.\n\nBy opening that `.log` file in Notepad, you'll retrieve the password un-encrypted, in a plain-text format.\n\n### Search for files that contain a keyword\n\nIt is possible to launch a search to, for example, find the string *Notes management* in all the `.php` files of the remote site.\n\nTo do this, go to the `Commands` menu, then `Static custom commands` and finally `Search for Text...`.\n\nIt is, in fact, the execution of a script (an extension in the WinSCP language) which is here : `C:\\Program Files (x86)\\WinSCP\\Extensions\\SearchText.WinSCPextension.ps1`\n\n![Search for Text...](./010-tips/Search-for-text/images/search_for_text_1.png)\n\nThe search result is displayed in a Powershell console:\n\n![Console output](./010-tips/Search-for-text/images/search_for_text_2.png)\n\n### Super User - Running command with SUDO SU\n\nDue to the configuration done on the FTP server, it can happen that we can't create a folder or just uploading files by using a FTP like `WinSCP` even when we can do this by using a `SSH` connection (and by running a `sudo su` command).\n\n`WinSCP` gives us a way to *force* such command:\n\n* In the `Site Manager` window, click on the `Advanced` button\n\n  ![Site Manager](./010-tips/sudo-Super-user/images/winscp_sudo1.png)\n\n* Then go to `Environment` -\u003e `SCP/Shell` and type `sudo su -` in the `Shell` entry\n\n  ![sudo su -](./010-tips/sudo-Super-user/images/winscp_sudo2.png)\n\nTry again and now, it should work.\n\n## Scripts\n\n### Download files with specific extension recursively\n\n\u003e How to quickly get all php scripts under /images? This script given the answer\n\nThe WinSCP FTP client ([website](https://winscp.net/eng/index.php)) allow to run a script that make possible automation.\n\nThe idea under the script below is to connect to a FTP server, go to the ftp root folder and then, recursively, get all files having the php extensions.\n\nI've coded this small script when facing the following problem: there were a lot of viruses in the /images folder of a website and that specific folder has hundreds of sub-folders and the `totalsize` of `/images` was bigger than 1 GB.\n\n#### The script\n\n```text\noption batch abort\n\noption confirm off\n\nlcd \"c:\\temp\\ftp\"\n\nopen ftp://USERNAME:PASSWORD@HOST_OR_IP/\n\ncd /public_html/site_name/images\n\noption transfer ascii\n\nget -filemask:*.php *\n\nclose\n\nexit\n```\n\nChange the file extension in `get -filemask:*.php *` to the desired extension.\n\n#### How to use\n\n*WinSCP is for Windows, this step by step explain how to use it on a Windows computer.*\n\n1. Get a copy of the script: [download it](https://raw.githubusercontent.com/cavo789/winscp/master/download_recursive/rget.txt) and save it, f.i., as `c:\\temp\\ftp\\rget.txt` (or update the script, see 2.1)\n2. Edit the script and specify:\n   1. where files should be downloaded, local folder (line 8)\n   2. in case of need, replace `ftp`by `sftp` (line 11)\n   3. `USERNAME`: the FTP username (line 11)\n   4. `PASSWORD`: the password associated to this account (line 11)\n   5. `HOST_OR_IP`: the FTP host name or his IP (line 11)\n   6. The remote folder from where the files should be downloaded (line 14), can be the ftp root or any sub-folder\n   7. the file extension (if not `.php`) (line 20)\n   8. save the script\n3. Start a DOS session (start -\u003e run -\u003e cmd.exe)\n4. Go to your `c:\\temp\\ftp` folder: type `cd \\temp\\ftp`\n5. Run `winscp.com` from there: type `\"c:\\program files (86)\\WinSCP\\WinSCP.com\" /script=\"c:\\temp\\ftp\\rget.txt\"`\n\nIf 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).\n\n### Synchronize files from a local path to a remote drive\n\n\u003e Copy files/folders from a local disk to a remote drive\n\nThe WinSCP FTP client ([website](https://winscp.net/eng/index.php)) allow to run a script that make possible automation.\n\nThe idea under the script below is to connect to a FTP server and copy an entire directory from a local path (like c:\\my documents f.i.) to a remove drive (like /backup/documents)\n\nThis script is a nice companion if you've f.i. a Synology at home and if you want to copy files/folders easily from your HDD so you can have a backup of them\n\n#### The script\n\nSynchronize the folder `C:\\Christophe\\` (local) to `/Christophe` (remote)\n\n* Get all files/folders from the local drive and make a copy to the FTP drive.\n  * Copy new files / new folders\n  * Delete files / folders not anymore on local drive from the remote drive\n* `-filemask=\"|.git/` for ignoring the `.git` folder.\n\n```text\noption batch abort\n\noption confirm off\n\nopen ftp://USERNAME:PASSWORD@HOST_OR_IP:PORT/\n\noption transfer ascii\n\nlcd \"C:\\Christophe\"\ncd /Christophe\n\nsynchronize remote -preservetime -transfer=automatic -filemask=\"|.git/\"\n\nclose\n\nexit\n```\n\n#### How to use\n\n*WinSCP is for Windows, this step by step explain how to use it on a Windows computer.*\n\n1. Get a copy of the script: [download it](https://raw.githubusercontent.com/cavo789/winscp/master/synchronize/winscp_Synchronize.txt.txt) and save it, f.i., as  `c:\\temp\\ftp\\winscp_Synchronize.txt.txt` (or update the script, see 2.1)\n2. Edit the script and specify:\n   1. in case of need, replace `ftp`by `sftp` (line 9)\n   2. `USERNAME`: the FTP username (line 9)\n   3. `PASSWORD`: the password associated to this account (line 9)\n   4. `HOST_OR_IP`: the FTP host name or his IP (line 9)\n   5. `PORT`: the port to use (21 for a FTP connection f.i., line 9)\n   6. The local folder where your files are stored (line 23)\n   7. The remote folder from where the files should be copied (line 24), can be the ftp root or any sub-folder\n3. Start a DOS session (start -\u003e run -\u003e cmd.exe)\n4. Go to your `c:\\temp\\ftp` folder: type `cd \\temp\\ftp`\n5. Run `winscp.com` from there: type `\"c:\\program files (86)\\WinSCP\\WinSCP.com\" /script=\"c:\\temp\\ftp\\winscp_Synchronize.txt\"`\n\nIf everything is correctly set up, WinSCP will start a session terminal and will start the synchronization.\n\n#### More info\n\nMore info about the Synchronize verb of WinSCP: https://winscp.net/eng/docs/scriptcommand_synchronize\n\n* If files/folders are already there, don't do anything.\n* If there are new files/folders, copy them.\n* If files/folders are no more on the local drive, remove them from the remote server.\n\nSo local is the master.\n\n##### Local / Remote / Both\n\nSee the line 25 of the `.txt` script: synchronize remote.\n\nChoose remote, local or both:\n\nWhen the first parameter is local, changes from remote directory are applied to local directory. When the first parameter is remote, changes from the local directory are applied to the remote directory. When the first parameter is both, both local and remote directories can be modified ([source](https://winscp.net/eng/docs/scriptcommand_synchronize#remarks)).\n\n### VBA - Visual Basic for Application\n\nThe `WinSCP` 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.\n\nSee [https://winscp.net/eng/docs/library_vb#using](https://winscp.net/eng/docs/library_vb#using). There are also several posts in the forum: [https://winscp.net/forum/search.php?mode=results](https://winscp.net/forum/search.php?mode=results)\n\n```vbnet\nOption Explicit\n\nSub Example()\n\n    Dim mySession As New Session\n\n    ' Enable custom error handling\n    On Error Resume Next\n\n    Upload mySession\n\n    ' Query for errors\n    If Err.Number \u003c\u003e 0 Then\n        MsgBox \"Error: \" \u0026 Err.Description\n\n        ' Clear the error\n        Err.Clear\n    End If\n\n    ' Disconnect, clean up\n    mySession.Dispose\n\n    ' Restore default error handling\n    On Error GoTo 0\n\nEnd Sub\n\nPrivate Sub Upload(ByRef mySession As Session)\n\n    ' Setup session options\n    Dim mySessionOptions As New SessionOptions\n    With mySessionOptions\n        .Protocol = Protocol_Sftp\n        .HostName = \"example.com\"\n        .UserName = \"user\"\n        .Password = \"mypassword\"\n        .SshHostKeyFingerprint = \"ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx\"\n    End With\n\n    ' Connect\n    mySession.Open mySessionOptions\n\n    ' Upload files\n    Dim myTransferOptions As New TransferOptions\n    myTransferOptions.TransferMode = TransferMode_Binary\n\n    Dim transferResult As TransferOperationResult\n    Set transferResult = mySession.PutFiles(\"c:\\temp\\*\", \"/home/user/\", False, myTransferOptions)\n\n    ' Throw on any error\n    transferResult.Check\n\n    ' Display results\n    Dim transfer As TransferEventArgs\n    For Each transfer In transferResult.Transfers\n        MsgBox \"Upload of \" \u0026 transfer.Filename \u0026 \" succeeded\"\n    Next\n\nEnd Sub\n```\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcavo789%2Ftools_winscp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcavo789%2Ftools_winscp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcavo789%2Ftools_winscp/lists"}