PowerShell script – Get Files / Change Name / Move to another folder

Advertisement

vinicostasantos1
Guest

PowerShell script – Get Files / Change Name / Move to another folder

Hi guys,

Could someone please help me with this question?
I'm trying to use this site example to DOWNLOAD files but I'm having a issue:
https://winscp.net/eng/docs/script_locking_files_while_uploading#rename

I'm having a error when the script try to move the renamed file:
"Error: Error moving file 'G:\temporary_localpath\._filepart' to 'G:localpathnewfilename'
'."
Could someone please please help me with this? Here is the script:
param (
    $localPath = "\localpath\",
    $remotePath = "/remotepath/",
)
 
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\WinSCP\WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
    $sessionOptions.HostName = "****"
    $sessionOptions.UserName = "****"
    $sessionOptions.Password = "****"
    $sessionOptions.SshHostKeyFingerprint = "************"
 
    $session = New-Object WinSCP.Session
 
    try
    {
        $session.Open($sessionOptions)
 
        $suffix = "_filepart"
 
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.ResumeSupport.State =
            [WinSCP.TransferResumeSupportState]::Off
            
        $transferResult =
        $session.GetFiles(
            ($remotePath + "*.*"), ($localPath + "*.*" + $suffix),
            $False, $transferOptions)
        $transferResult.Check()
         
        foreach ($transfer in $transferResult.Transfers)
        {       
            $finalName =
                $transfer.Destination.Substring(0, $transfer.Destination.Length - $suffix.Length)
            Write-Host (
                "Renomeando arquivo {0} para o nome final" -f $transfer.Destination, $finalName)
            $session.MoveFile($transfer.Destination, $finalName)
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1
}

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,454
Location:
Prague, Czechia

Re: PowerShell script – Get Files / Change Name / Move to another folder

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

Advertisement

You can post new topics in this forum