Delete empty folders after sync - script
Hello.
New to winscp and scripting. I have found a script that does what I want, but it leaves the empty directories on the source directory after deleting the files.
the script is located in (https://winscp.net/eng/docs/library_example_delete_after_successful_download)
Script:
param (
$localPath = "",
$remotePath = ""
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = ""
UserName = ""
Password = ""
SshHostKeyFingerprint = ""
TimeoutInMilliseconds = 300000
}
$sessionOptions.AddRawSettings("FSProtocol", "2")
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Synchronize files to local directory, collect results
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $False)
# Iterate over every download
foreach ($download in $synchronizationResult.Downloads)
{
# Success or error?
if ($download.Error -eq $Null)
{
Write-Host "Download of $($download.FileName) succeeded, removing from source"
# Download succeeded, remove file from source
$removalResult =
$session.RemoveFiles($session.EscapeFileMask($download.FileName))
if ($removalResult.IsSuccess)
{
Write-Host "Removing of file $($download.FileName) succeeded"
}
else
{
Write-Host "Removing of file $($download.FileName) failed"
}
}
else
{
Write-Host "Download of $($download.FileName) failed: $($download.Error.Message)"
}
}
}
finally
{
$session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
If anyone know how to add something to check for empty directories and delete them from the source, that would be grand.
thanks
New to winscp and scripting. I have found a script that does what I want, but it leaves the empty directories on the source directory after deleting the files.
the script is located in (https://winscp.net/eng/docs/library_example_delete_after_successful_download)
Script:
param (
$localPath = "",
$remotePath = ""
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = ""
UserName = ""
Password = ""
SshHostKeyFingerprint = ""
TimeoutInMilliseconds = 300000
}
$sessionOptions.AddRawSettings("FSProtocol", "2")
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Synchronize files to local directory, collect results
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $False)
# Iterate over every download
foreach ($download in $synchronizationResult.Downloads)
{
# Success or error?
if ($download.Error -eq $Null)
{
Write-Host "Download of $($download.FileName) succeeded, removing from source"
# Download succeeded, remove file from source
$removalResult =
$session.RemoveFiles($session.EscapeFileMask($download.FileName))
if ($removalResult.IsSuccess)
{
Write-Host "Removing of file $($download.FileName) succeeded"
}
else
{
Write-Host "Removing of file $($download.FileName) failed"
}
}
else
{
Write-Host "Download of $($download.FileName) failed: $($download.Error.Message)"
}
}
}
finally
{
$session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
If anyone know how to add something to check for empty directories and delete them from the source, that would be grand.
thanks