Filename assistance

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
}

July 16th, 2015 2:27pm

Hi,

You want the BaseName pro

Free Windows Admin Tool Kit Click here and download it now
July 16th, 2015 2:31pm

RT9Write ("##                              "+$transfer.FileName+"       ####      ###       "+$date2+"######  Images uploaded to ### FTP site."  -f
                    $transfer.FileName)

this is what i have currently. I am currently writing this script in powershell_ise. this is the part that has the filename with the path - $transfer.FileName

when i try $transfer.BaseName - it returns nothing for the filename ie

"##                              "(used to be here)"       ####      ###       "+$date2+"######  Images uploaded to ### FTP site."

when i try $transfer.FileName.BaseName - same

"##                              "(used to be here)"       ####      ###       "+$date2+"######  Images uploaded to ### FTP site."

July 16th, 2015 2:42pm

Did you write that fun

Free Windows Admin Tool Kit Click here and download it now
July 16th, 2015 2:44pm

This should give you filename without extension:

$basename = [System.IO.Path]::GetFileNameWithoutExtension($transfer.FileName)

  • Proposed as answer by Mike Laughlin 12 hours 25 minutes ago
  • Marked as answer by Netrecov 12 hours 19 minutes ago
July 16th, 2015 2:49pm

this is my function

function RT9Write
    {
    Param ([string]$logstring)
    Add-content $RT9file -value $logstring
    }

and the $RT9file is

$RT9File = "C:\Users\me\Desktop\RT9.txt"

i am using the example script from winscp's website

script_local_move_after_successful_upload

i have searched their forums and have came up empty.

Free Windows Admin Tool Kit Click here and download it now
July 16th, 2015 2:50pm

Thank you sooo much!
July 16th, 2015 2:58pm

You are most welcome :)
Free Windows Admin Tool Kit Click here and download it now
July 16th, 2015 3:00pm

This should give you filename without extension:

$basename = [System.IO.Path]::GetFileNameWithoutExtension($transfer.FileName)

  • Proposed as answer by Mike Laughlin Thursday, July 16, 2015 6:45 PM
  • Marked as answer by Netrecov Thursday, July 16, 2015 6:51 PM
July 16th, 2015 6:43pm

This should give you filename without extension:

$basename = [System.IO.Path]::GetFileNameWithoutExtension($transfer.FileName)

  • Proposed as answer by Mike Laughlin Thursday, July 16, 2015 6:45 PM
  • Marked as answer by Netrecov Thursday, July 16, 2015 6:51 PM
Free Windows Admin Tool Kit Click here and download it now
July 16th, 2015 6:43pm

This should give you filename without extension:

$basename = [System.IO.Path]::GetFileNameWithoutExtension($transfer.FileName)

  • Proposed as answer by Mike Laughlin Thursday, July 16, 2015 6:45 PM
  • Marked as answer by Netrecov Thursday, July 16, 2015 6:51 PM
July 16th, 2015 6:43pm

You are most welcome :)
  • Proposed as answer by LarryWeiss 21 hours 58 minutes ago
Free Windows Admin Tool Kit Click here and download it now
July 16th, 2015 6:53pm

You are most welcome :)
  • Proposed as answer by LarryWeiss Friday, July 17, 2015 9:12 AM
July 16th, 2015 6:53pm

You are most welcome :)
  • Proposed as answer by LarryWeiss Friday, July 17, 2015 9:12 AM
Free Windows Admin Tool Kit Click here and download it now
July 16th, 2015 6:53pm

You are most welcome :)
  • Proposed as answer by LarryWeiss Friday, July 17, 2015 9:12 AM
July 16th, 2015 6:53pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics