Transferetto icon indicating copy to clipboard operation
Transferetto copied to clipboard

SFTP upload issue when folder name has a space

Open harleykin opened this issue 3 years ago • 7 comments
trafficstars

I'm using your SFTP uploading example here: https://github.com/EvotecIT/Transferetto/blob/master/Examples/Example07-UploadSFTP.ps1 and it works GREAT except when a folder to be uploaded has a space in the folder name.

Example: /uploads/folder/sub folder/* is placed at the root uploads folder, rather than under /uploads/folder/*

(I hope this made sense)

This is the script I used:

$SftpClient = Connect-SFTP -Server 'sftp.server.com' -Username "test.user" -PrivateKey "C:\Path\to\My\Private_key" -Verbose
$ListFiles = Get-ChildItem -LiteralPath "C:\BoxTesting" -Recurse -File
foreach ($File in $ListFiles) {
    $Directory = [io.path]::GetDirectoryName($File.FullName)
    if ($Directory -eq "C:\BoxTesting") {
        Send-SFTPFile -SftpClient $SftpClient -LocalPath $File.FullName -RemotePath "/dataplatform-sftp-bucket-1234567890/$($File.Name)" -AllowOverride
    } else {
        #$RemotePath = "/dataplatform-sftp-bucket-1234567890/$($Directory.Split('\')[-1])/$($File.Name)"
        $RemoteFolder = "/dataplatform-sftp-bucket-1234567890/$($Directory.Split('\')[-1])"
        $List = Get-SFTPList -SftpClient $SftpClient -Path $RemoteFolder -WarningAction SilentlyContinue
        if (-not $List) {
            $SftpClient.CreateDirectory($RemoteFolder)
        }
        Send-SFTPFile -SftpClient $SftpClient -LocalPath $File.FullName -RemotePath "$RemoteFolder/$($File.Name)" -AllowOverride
    }
}
Disconnect-SFTP -SftpClient $SftpClient

harleykin avatar Nov 17 '22 20:11 harleykin

Here's one of the outputs (sanitized):

Action     : UploadFile
Status     : True
LocalPath  : C:\BoxTesting\Long_Path_Name\z_Archive_YYYY-1234\weekly upload\Very Long Filename with Spaces.txt
RemotePath : /dataplatform-sftp-bucket-1234567890/weekly upload/Very Long Filename with Spaces.txt
Message    : 

harleykin avatar Nov 17 '22 21:11 harleykin

Are you saying the Send-SFTPFile is unable to support filename with spaces and when it sees one it puts it in wrong folder or are you talking about my example which just shows some functionality, but not really tested for such scenario.

PrzemyslawKlys avatar Nov 17 '22 22:11 PrzemyslawKlys

I'm saying I think the issue is directory names with spaces, but it could also be I am not using your module correctly. Hence why I was using your example. I could be wrong on both things, as I was wrong on other issues I asked you about. :)

harleykin avatar Nov 18 '22 14:11 harleykin

Good morning/afternoon! Looking back over the example, its logic looks like it should be doing exactly what I want. But there's an error somewhere. Ideally, I just need to upload files from a local drive to an SFTP, skipping any existing files, creating new directories if they don't already exist, and writing any new files. Your example looks to be doing just that, but something is off somewhere.

harleykin avatar Nov 21 '22 16:11 harleykin

I need to do a deeper dive an maybe write something custom. I don't think this fully works to transfer folder with all its files to server the same way it's implemented in FTP. Just playing with Split and [-1] is probably bad idea and a reason it doesn't work reliably.

PrzemyslawKlys avatar Nov 21 '22 17:11 PrzemyslawKlys

I appreciate you taking a look. The automation PC I'm setting this up on has WinSCP installed, so I might could get that going as a workaround, but honestly I'd rather use your module. :)

harleykin avatar Nov 21 '22 18:11 harleykin

Implementing this https://github.com/EvotecIT/Transferetto/issues/24 would probably help, but you could try to use this method directly.

PrzemyslawKlys avatar Nov 22 '22 15:11 PrzemyslawKlys