EvotecIT / Transferetto

Small PowerShell module with FTPS/SFTP functionality
MIT License
55 stars 14 forks source link

SFTP upload issue when folder name has a space #23

Open harleykin opened 1 year ago

harleykin commented 1 year ago

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 commented 1 year ago

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    : 
PrzemyslawKlys commented 1 year ago

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.

harleykin commented 1 year ago

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 commented 1 year ago

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.

PrzemyslawKlys commented 1 year ago

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.

harleykin commented 1 year ago

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. :)

PrzemyslawKlys commented 1 year ago

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