Adding an auto-incrementing value to a filename string when downloading files from an FTP serer

I'm in the process of the writing a script which needs to do the following.

  • Go out to an ftp server and obtain files from a directory that match  a certain naming pattern (L6XQ*)
  • Copy them over to a local directory renaming them with the L6XQ followed by the Month and day (MMDD) and then an auto-incrementing number starting at 001, so the first file obtained would be placed in the local directory appearing as 'L6XQ0202.001.txt'.
  • Then the script will check to see if there is any files in the local directory matching the naming pattern L6XQ*, if there is, a command will then go out to the FTP server again and delete the counterpart file.

 

While($true)
{
 
$MyCredential = Import-Clixml -Path 'C:\Cred\PW.txt'
$FTPServer = 'ftp://192.168.0.18'
$FTPPath = '/NatWest/L6XQ*.txt'
IF ($StartNum -eq $null)
{
$StartNum = 0
}
 
$StartNum++
$LocalPath = "C:/Cred/L6XQ{0:MMdd}.{1:000}.txt" -f (get-date), $StartNum.ToString("000")
$CheckFile = 'C:/Cred/L6XQ*.txt'
$FileExists = Test-Path $CheckFile
Set-FTPConnection -Credentials $MyCredential -Server $FTPServer -Session FTPFileGetSession
$Session = Get-FTPConnection -Session FTPFileGetSession
Get-FTPChildItem -Session $Session -Path $FTPPath -Recurse | ForEach-Object -begin {Get-FTPItem -session $session -Verbose -localpath $LocalPath -Overwrite:$True -Confirm:$false}
IF ($FileExists -eq $True)  {Get-FTPChildItem -Session $Session -Path /Natwest -Filter l6xq* -recurse | Remove-FTPItem -session $session -verbose -confirm:$false}
 
 
Start-Sleep -Seconds 3600
}


What I have so far meets nearly all of this criteria, however, if there are 6 files on the FTP server and the script grabs them all in one job run, it moves them to the local directory bu overwrites each file it obtained with the next one. It’s also worth noting that each file that overwrites the last has the same auto-incremented value.

VERBOSE: Performing the operation "Download item: 'ftp://192.168.0.18/NatWest/l6xq1 – Copy (2).txt'" on target "".
226 Transfer complete.

VERBOSE: Performing the operation "Download item: 'ftp://192.168.0.18/NatWest/l6xq1 – Copy (2).txt'" on target "C:/Cred/L6XQ0201.004.txt".
226 Transfer complete.

VERBOSE: Performing the operation "Download item: 'ftp://192.168.0.18/NatWest/l6xq1 – Copy (3).txt'" on target "C:/Cred/L6XQ0201.004.txt".
226 Transfer complete.

If I then run the script again, the auto-incremented value will increase by one, but I wanted this to happen for each file it pulls down during each job run.

Having looked at proprietary PS cmdlets and well as functionality PS leverages from other scripting, I found a module on https://gallery.technet.microsoft.com/scriptcenter/ which offered the required functionality to upload, download and delete files on an FTP server called 'PSFTP'. This is what I have used for the script so far

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

I have tried using ForEach and ForEach-Object logic, but neither seem to be working. For instance

 

Get-FTPChildItem -Session $Session -Path $FTPPath -recurse | ForEach-Object {Get-FTPItem -session $session -verbose -localpath $LocalPath -overwrite:$Confirm -confirm:$false}

The script just stalls.

 

$Files = Get-FTPChildItem -Session $Session -Path $FTPPath -recurse

ForEach ($File IN $Files) {Get-FTPItem -session $session -verbose -localpath $LocalPath -overwrite:$Confirm -confirm:$false}

This doesn't work as the Get-FTPChildItem output has to pipe to the Get-FTPItem command and ForEach doesn't allow for pipe between the () and {}.

 

Sorry for the long winded description, I just wanted to maake sure I included everything. Can anyone advise or point me in the right direction?