PSFTP Module: changing file extensions on an FTP Server

I’m pretty new to Powershell and am trying to create a script that does the following.

  1. Copyfiles (without an extension) to an FTP Server.

  2. Once they are on the FTP Server, each file needs to to have the .tmp extension added to the file name.

  3. Once both of the above have taken place, the files on the local machine need to be moved to a separate local directory.

I have been using the PSFTP module to try and facilitate this.

https://gallery.technet.microsoft.com/scriptcenter/PowerShell-FTP-Client-db6fe0cb

The script I have so far achieves 1 and 2, but I can’t work out how to append the .tmp extension on the files on the FTP server. I’m trying to use the Rename-FTPItem cmdlet, but can;t determine how to just rename the extension portion of the file namespace.

This is what I have so far.

Set-FTPConnection -Credentials $MyCredential -Server $FTPServer -Session FTPsendGetSession
$Session = Get-FTPConnection -Session FTPsendGetSession
$fileList = Get-ChildItem -Path $LocalPath -Filter l6xq* -recurse
#Label verbose transaction block for the upload of files from the local directory to the FTP server
Write-Verbose "<>"
#For each file returned  in the above get-childitem directory, move it to the ftp location
foreach ($File in $fileList) {
        Write-Output "Processing file:  $($File.FullName)"
        try {
            
            Add-FTPItem -Session $Session -Path $FTPPath -LocalPath $File.FullName -overwrite -confirm:$False -verbose -ErrorAction Stop
            }  
            catch {
                    Write-Warning "Error occurred with $($File.FullName). $($error[0].message)"
                 }
    }
$FTPfileList = Get-FTPChildItem -Session $Session -Path $FTPPath
#Label verbose transaction block for files between local directories
Write-Verbose "<>"
#For each file returned  in the above get-ftp childitem directory, rename it appending the .tmp extension
foreach ($File in $ftpfileList) 
         {
         
        Write-Output "Processing file:  $($File.FullName)"
        try {
            Rename-FTPItem -Session $Session -path $File.Fullname -NewName {$File.Name + ".tmp"}
            }  
            catch {
                    Write-Warning "Error occurred with $($File.FullName). $($error[0].message)"
                 }
    }
$ArchivePath = 'C:/SentFileArchive'
Start-Sleep -Seconds 5
#Label verbose transaction block for the upload of files from the local directory to the FTP server
Write-Verbose "<>"
Write-Output "Processing file:  $($File.FullName)"
Get-ChildItem -Path $LocalPath -Filter L6XQ* | Move-Item -Destination $ArchivePath -ErrorAction Stop -Verbose
stop-transcript 

Try changing:

{$File.Name + “.tmp”}

to:

($File.Name + “.tmp”)

I think I’ve tried this as well (but not certain), but I’ll give it a go and feedback.

Many thanks

Look at the help file, make sure your path includes the full path and name.

Rename-FTPItem -path /TestFolder/TestFile -newname /TestFolder/TestFile.txt

or

Rename-FTPItem -path /TestFolder/TestFile -newname …/TestFile.txt

It works, great!!. Seems I mustn’t have tried that parenthesis in place of braces. Many thanks for your help, much appreciated :slight_smile:

Another requirement hs come to light, once the files have been added to the directory and renamed with the .tmp extension, they then need to be moved on to another directory and the extension has to be removed again. Racking by brains but can’t work this out.

Script so far.

#Import the module
Import-Module PSFTP 
#Enable verbose transcript logging of all transactions. New log file will be created each day, with each job runs activities in that day appending to the log. 
#Log files older than 90 days will be deleted.
$VerbosePreference = "Continue"
$LogPath = Split-Path $MyInvocation.MyCommand.Path
Get-ChildItem "$LogPath\*.log" | Where LastWriteTime -LT (Get-Date).AddDays(-15) | Remove-Item -Confirm:$False
$LogPathName = Join-Path -Path $LogPath -ChildPath "$($MyInvocation.MyCommand.Name)-$(Get-Date -Format 'MM-dd-yyyy').log"
Start-Transcript $LogPathName -Append 
$MyCredential = Import-Clixml -Path 'C:\Parcel_Force_FTP\Cred\PFAuth.txt'
$FTPServer = 'FTP://195.6.252.2'
$FTPPath = '/'
$FTPPath2 = '/in'
$LocalPath = 'c:/parcel_force_ftp'
#Set connection properties to the FTP server.
Set-FTPConnection -Credentials $MyCredential -Server $FTPServer -Session FTPsendGetSession
$Session = Get-FTPConnection -Session FTPsendGetSession
$fileList = Get-ChildItem -Path $LocalPath -Filter l6xq*
#Label verbose transaction block for the upload of files from the local directory to the FTP server
Write-Verbose "<>"
#For each file returned by get-childitem local directory, copy it to the ftp location.
Foreach ($File in $fileList) {
        Write-Output "Processing file:  $($File.FullName)"
        Try {            
            Add-FTPItem -Session $Session -Path $FTPPath -LocalPath $File.FullName -overwrite -Confirm:$False -Verbose -ErrorAction Stop
            }  
            Catch {
                    Write-Warning "Error occurred with $($File.FullName). $($error[0].message)"
                 }
    }
$FTPfileList = Get-FTPChildItem -Session $Session -Path $FTPPath -Filter l6xq*
#Label verbose transaction block for files between local directories
Write-Verbose "<>"
#For each file returned  in the above get-ftp childitem directory, rename it appending the .tmp extension
Foreach ($File in $ftpfileList) 
         {         
        Write-Output "Processing file:  $($File.FullName)"
        Try {
            Rename-FTPItem -Session $Session -Path $File.Fullname -NewName ($File.Name + ".tmp")  -Confirm:$False -Verbose -ErrorAction Stop          
            }  
            Catch {
                    Write-Warning "Error occurred with $($File.FullName). $($error[0].message)"
                 }
    }
Start-Sleep -Seconds 5
$FTPfileList = Get-FTPChildItem -Session $Session -Path $FTPPath -filter l6xq*
#Label verbose transaction block for files between local directories
Write-Verbose "<>"
#For each file returned  in the above get-ftp childitem directory, rename it appending the .tmp extension
foreach ($File in $ftpfileList) 
         {
         
        Write-Output "Processing file:  $($File.FullName)"
        try {
            Rename-FTPItem -Session $Session -path $File.Fullname -NewName ($FTPPath2 + $File.Name)  -confirm:$False -verbose -ErrorAction Stop
            }  
            catch {
                    Write-Warning "Error occurred with $($File.FullName). $($error[0].message)"
                 }
    }
Start-Sleep -Seconds 5
#Label verbose transaction block for the upload of files from the local directory to the FTP server
Write-Verbose "<>"
$ArchivePath = '/Sent'
Write-Output "Processing file:  $($File.FullName)"
Get-ChildItem -Path $LocalPath -Filter L6XQ* | Move-Item -Destination $ArchivePath -ErrorAction Stop -Verbose
Stop-Transcript 

So this moves the files to the final directory, once they are in there, I need to remove the .tmp extension from each file.

I’ve tried playing around with .Basename, but no joy so far.

Can any one assists?