0

I'm not sure how to select the newest date modified file and rename it to something... This script will run once a day, and the newest date modified needs to overweight the previous days' file. Any help will be appreciated!

$session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # Download files
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
 
        $transferResult =
            $session.GetFiles("/upload*", "C:\Users\Nick\Desktop\Test", $False, $transferOptions)
 
        # Throw on any error
        $transferResult.Check()
 
        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host "Download of $($transfer.FileName) succeeded"
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • is your Question about how to find the newest file? or about how to rename is once it is found? – Lee_Dailey Mar 22 '22 at 13:35
  • i am also not sure if i understand what you are trying to do. may this help you. do you try to rename the latest file in a folder after your script ran? you mean something like that: >>> (dir | sort lastwritetime -Descending)[0] | Rename-Item -NewName hereTheNewName.ext – An-dir Mar 22 '22 at 21:22

0 Answers0