To monitor multiple folders and automatically upload new files added via SFTP on Windows Server 2025, you can utilize free methods. Here's a step-by-step guide using PowerShell and WinSCP:
-
Install WinSCP:
- Download WinSCP from https://winscp.net/eng/download.html
- Install WinSCP on your Windows Server 2025
-
Create a PowerShell script (e.g.,
AutoUploadSFTP.ps1) to monitor folders and upload new files:
# Import WinSCP .NET assembly
Add-Type -Path "C:\Program Files\WinSCP\winscp.exe"
# Set your SFTP credentials
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.HostName = "your_sftp_server"
$sessionOptions.Username = "your_username"
$sessionOptions.Password = "your_password"
$sessionOptions.SshHostKeyFingerprint = "your_ssh_host_key_fingerprint"
# Set the local folders to monitor
$foldersToMonitor = @("C:\LocalFolder1", "C:\LocalFolder2")
# Set the remote folder to upload files
$remoteFolder = "/remote/folder"
# Create a WinSCP session
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
# Function to get the latest timestamp of a file
function Get-LatestFileTimestamp($folder) {
Get-ChildItem $folder -File | Sort-Object LastWriteTime -Descending | Select-Object -First 1
}
# Function to upload files from local to remote
function Upload-Files($session, $localFolder, $remoteFolder) {
$latestFile = Get-LatestFileTimestamp $localFolder
if ($latestFile) {
$localFilePath = $latestFile.FullName
$remoteFilePath = Join-Path $remoteFolder ($latestFile.Name)
$transferOptions = New-Object WinSCP.TransferOptions
$session.PutFiles($localFilePath, $remoteFilePath, $transferOptions)
}
}
# Monitor folders and upload new files
foreach ($folder in $foldersToMonitor) {
Write-Host "Monitoring folder: $folder"
Upload-Files $session $folder $remoteFolder
Start-Sleep -Seconds 60 # Adjust sleep interval as needed
}
# Close the WinSCP session
$session.Close()
-
Modify the script with your SFTP server details, local folders to monitor, and remote folder to upload files.
-
Save the script as
AutoUploadSFTP.ps1and run it with PowerShell:
.\AutoUploadSFTP.ps1
This PowerShell script will monitor the specified local folders, and if new files are added, it will upload them to the remote SFTP server.
References: