I am still new and trying to learn powershell. I am trying to accomplish 2 things. First is to retrieve only the filename (no .ext, no path) and be able to write to a different log file in a certain way. here is the code that i have with an example.
lets say the filename is "test1324.txt" all i need is the "test1234" without the path. i cant seem to find a cmdlet that will allow me to pull the filename out of $transfer.FileName. the log im using is RT9Write. CAn anyone help me?
param ( $localPath = "C:\upload\*", $remotePath = "/home/user/", $backupPath = "C:\backup\", $RT9File = "C:\users\me\desktop\rt9.txt" ) function RT9Write { Param ([string]$logstring) Add-content $RT9file -value $logstring } try { # Load WinSCP .NET assembly Add-Type -Path "WinSCPnet.dll" # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp $sessionOptions.HostName = "example.com" $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 try { # Connect $session.Open($sessionOptions) # 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 $transfer.FileName) RT9Write ("09 "+(filename)+" 1234 123 20150716abcdefghImages uploaded to mine FTP site." -f $transfer.FileName) # Upload succeeded, move source file to backup Move-Item $transfer.FileName $backupPath } else { Write-Host ("Upload of {0} failed: {1}" -f $transfer.FileName, $transfer.Error.Message) } } } finally { # Disconnect, clean up $session.Dispose() } exit 0 } catch [Exception] { Write-Host $_.Exception.Message exit 1 }