ShareFile-PowerShell icon indicating copy to clipboard operation
ShareFile-PowerShell copied to clipboard

Sync-SFItem deleting files that were not moved with the -move option

Open rwinhold opened this issue 9 years ago • 5 comments

I am attempting to download files from our Sharefile direcrtory using Sync-SFItem. Once downloaded. I want the downloaded files to be removed from the Sharefile directory. So, for the Sync-SFItem command, I'm using -Move. The problem is that all the files are copied without issue but it seems that everything is deleted - including new files that have arrived after the download started. The behaviour seems to be that only files that existed at the time the Sync was started are download. BUT all the files that exist after the download is finished are delete. Is this an issue? Is there a workaround?

Here is the bit of code I am using to do this task:

New-PSDrive -Name sfWXX -PSProvider Sharefile -Client $sfClient -Root "/WXX" -RootUri $EFBUnprocessedHome Sync-SfItem -ShareFilePath "sfWXX:" -Download -OverWrite -Move -LocalPath $localUnprocessedEFBFlightPlansDir Remove-PSDrive sfWXX

rwinhold avatar Sep 23 '16 21:09 rwinhold

Hi @rwinhold , it's definitely an issue within the provider. There are two sets of operations that occur the actual download and the delete from ShareFile. The issue that is surfacing is the provider does not track distinct items that are being downloaded. Therefore, after download it naively deletes the whole ShareFile path. Until we get this fixed, I don't see a workaround short of having to implement the download and delete behavior with the tracking mentioned.

rgmills avatar Sep 26 '16 00:09 rgmills

Thanks for the reply. I was wondering if I was going crazy or if there was a problem.

It appears that if I specify an exact filename, then it will download it and delete it without deleting the whole directory content. I think what I'll attempt as a "workaround" is to try to generate a list of what I want to download and move them one at a time.

rwinhold avatar Sep 26 '16 13:09 rwinhold

Here's a bit of code showing what I did to solve my problem. It runs quite slow but if I schedule a task to run this fairly often, the number of files doesn't get to large and the run time is acceptable:

$CredentialFile = "C:\sharefilescript\credentials.sfps"
$sfClient=Get-SfClient -Name $CredentialFile
$localRootDir = "c:\moveFilesToHereROOT"
$RootSharefileDir = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxxx" #from URL of actual root dir

$childDir = Send-SfRequest $sfClient -Method GET -Entity Items -Id $RootSharefileDir -Expand "Children,Owner"`

foreach($dir in $childDir.Children)
{
    $dirname = $dir.filename
    $file = Send-SfRequest $sfClient -Method GET -Entity Items -Id $dir.id -Expand "Children,Owner"
    foreach($fileToMove in $file.children)
    {
        $FileToMove = (Send-SfRequest $sfClient -Entity Items -id $fileToMove.id).url
        New-PSDrive -Name sfmoveFile -PSProvider Sharefile -Client $sfClient -Root "/" -RootUri $FileToMove
        Sync-SfItem -ShareFilePath "sfmoveFile:" -Download -OverWrite -Move -LocalPath "$localRootDir\$dirname"
        Remove-PSDrive sfmoveFile
    }
}

rwinhold avatar Sep 26 '16 21:09 rwinhold

@rwinhold I've opened up a PR that should resolve the issue. There is still some testing to be done to make sure my late night boredom paid off.

rgmills avatar Sep 27 '16 19:09 rgmills

Cool! Thanks!

rwinhold avatar Sep 27 '16 20:09 rwinhold