Delete empty folders after sync - script

Advertisement

rech
Joined:
Posts:
1
Location:
Canada

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

Reply with quote

Advertisement

Guest

Re: Delete empty folders after sync - script

martin wrote:

Do you really have to use SynchronizeDirectories?

Cannot you just use GetFiles with remove parameter?
$session.GetFiles("$remotePath/*", "$localPath\", $True).Check()

The difference (while referring to scripting, not .NET assembly) is discussed here:
https://winscp.net/eng/docs/faq_delete_synchronized_files

Hello Martin.

I would like to delete the files after they have been downloaded to Local, I'm following the script that the link that you sent refers to. I could look for a script to delete all folders from source and I add it to the one that I'm using, that way I can "clean" the source directory.

DO you know the commands?

thanks

R

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,454
Location:
Prague, Czechia

Re: Delete empty folders after sync - script

Sorry, but you keep repeating yourself. Did you try some of my suggestions? Particularly the GetFiles with remove parameter?

$session.GetFiles("$remotePath/*", "$localPath\", $True).Check()

Reply with quote

Guest

Re: Delete empty folders after sync - script

martin wrote:

Sorry, but you keep repeating yourself. Did you try some of my suggestions? Particularly the GetFiles with remove parameter?

$session.GetFiles("$remotePath/*", "$localPath\", $True).Check()

Sorry for the delay in answering, I was able to modify my script to delete the empty directories (without using the $session.GetFiles command). Now that I have my script working and have the time, I will build a new one and use your recommendation.

Thanks

Reply with quote

Advertisement

UR
Guest

Re: Delete empty folders after sync - script

It would be nice to see your solution, after you ask others und find it by yourself.

Anonymous wrote:

martin wrote:

Sorry, but you keep repeating yourself. Did you try some of my suggestions? Particularly the GetFiles with remove parameter?

$session.GetFiles("$remotePath/*", "$localPath\", $True).Check()

Sorry for the delay in answering, I was able to modify my script to delete the empty directories (without using the $session.GetFiles command). Now that I have my script working and have the time, I will build a new one and use your recommendation.

Thanks

Reply with quote

Advertisement

You can post new topics in this forum