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 Im 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 MV 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
}