Topic "get local filename after transfer so i can attach to email"

Author Message
[View user's profile]

Joined: 2015-07-20
Posts: 3
Hello I am trying to get the filename of the file just downloaded with the localpath so i can attach it to an email.
here is the code
[code]param (
$localPath = "C:\upload\*",
$remotePath = "/home/user/",
$backupPath = "C:\backup\"

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = ""
$sessionOptions.UserName = "user"
$sessionOptions.Password = "mypassword"
$sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"

$session = New-Object WinSCP.Session

# Connect

# Upload files, collect results
$transferResult = $session.PutFiles($localPath, $remotePath)

# Iterate over every transfer
foreach ($transfer in $transferResult.Transfers)
# Success or error?
if ($transfer.Error -eq $Null)
Write-Host ("Upload of {0} succeeded, moving to backup" -f
Send-MailMessage -From $From -to $To -Cc $Cc -Bc $Bc -Attach $session.GetFileInfo(Filename) -Subject $Subject -Body $Body -SmtpServer $SMTPServer
# Upload succeeded, move source file to backup
Move-Item $transfer.FileName $backupPath
Write-Host ("Upload of {0} failed: {1}" -f
$transfer.FileName, $transfer.Error.Message)
# Disconnect, clean up

exit 0
catch [Exception]
Write-Host $_.Exception.Message
exit 1
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24755
Location: Prague, Czechia
You get the target path in $transfer.Destination

You can post new topics in this forum

Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!


About donations

$9   $19   $49   $99

About donations


WinSCP Privacy Policy

WinSCP License