WinSCP recursively find all files with certain extension and upload it to FTP server

  • 1 year ago
Comment

ChatGPTChatGPT
1 / 1

To recursively find all files with a certain extension and upload them to an FTP server using WinSCP from the command line, you can create a script and use the winscp.com command-line tool. Here's how you can do it:

  1. Install WinSCP: First, download and install WinSCP on your Windows machine if you haven't already. You can download it from the WinSCP website: https://winscp.net/eng/download.php

  2. Create the WinSCP Script: Create a text file with the extension .txt (e.g., upload_script.txt) and write the following content in it:

perl
# Connect to the FTP server
open ftp://username:password@ftp.example.com/

# Change remote directory to the desired location on the FTP server
cd /path/to/remote/directory/

# Find all files with a certain extension in the local directory recursively
option batch abort
option confirm off
lcd C:\path\to\local\directory\
filemask "*.txt" # Replace "*.txt" with your desired file extension

# Upload the found files to the FTP server
put -filemask=*.txt -delete

# Exit
exit

Replace the following placeholders in the script:

  • username: Replace with your FTP username.
  • password: Replace with your FTP password.
  • ftp.example.com: Replace with your FTP server hostname or IP address.
  • /path/to/remote/directory/: Replace with the desired directory path on the FTP server where you want to upload the files.
  • C:\path\to\local\directory\: Replace with the local directory path where the files with the specified extension are located.
  1. Upload Files: Open a Command Prompt or PowerShell window and use the following command to execute the WinSCP script:

For Command Prompt:

batch
winscp.com /script=upload_script.txt

For PowerShell:

powershell
Start-Process -FilePath "winscp.com" -ArgumentList "/script=upload_script.txt" -NoNewWindow -Wait

WinSCP will execute the script, recursively find all files with the specified extension in the local directory, and upload them to the specified location on the FTP server. The -delete option in the put command deletes the local files after successful upload.

Please note that storing the FTP password in the script file is not secure. Consider using SSH key-based authentication or other secure methods for automating FTP uploads in a production environment.