Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

EmlynFarrell

Renaming a file from .tmp to .xml after file is uploaded and moved to Archive location.

Hi all, Currently trying to configure a script to upload all files that are in a directory they contain file the file type .tmp and upload them to the remote FTP server using SFTP.

So far in the script I can login and move the files to the remote FTP site and then move the uploaded files over to the archive folder, this works as intended.

The only bit I'm unsure of is how to rename all the files from .tmp to .xml.

Below is the code that im using with connection details ommited.

Looking through the documentation it seems we can use the [m]mv[m] command, but this has not worked for me.

Any help would be greatly appreciated, some parts of the code are commented out,
param (
    #$localPath = "X:\Datapool*",
    $localPath = "C:\Users\emlyn.farrell\Desktop\TestingLogic\ADDRESS*",
    $remotePath = "/transfer",
    $backupPath = "C:\Users\emlyn.farrell\Desktop\test.archive"
   # $-Path = "/transfer",
)
 
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Users\emlyn.farrell\Desktop\WinSCPnet.dll"
 
    # Set up session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "host"
        UserName = "User"
        Password = "Password"
        SshHostKeyFingerprint = ""
    }
 
    # indicates what Trsansfer setting to Use 2 = SFTP
    $sessionOptions.AddRawSettings("FSProtocol", "2")
 
    $session = New-Object WinSCP.Session
   
    $session.SessionLogPath = "C:\Users\emlyn.farrell\Desktop\incremental_LOG1.log"
 
    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)
            {
                #mv *.Tmp  $remotePath*.xml # added this for test to rename files for upload
             
                Write-Host "Upload of $($transfer.FileName) succeeded, moving to backup"
                # Upload succeeded, move source file to backup
                Move-item $transfer.FileName $backupPath
                #Move-Item $transfer.FileName $backupPath
                #Move-Item -Path $transfer -Destination $backupPath
            }
            else
            {
                Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}