NOTE: The following code is now available as a sample Job named FileTransferPolling in JAMS V7.3 or higher. You can find this Job by clicking the Definitions shortcut and selecting the Samples folder.
# This job will poll a remote server for specific files based on the filemask provided and report how many files are available for processing # This job can be used as a precheck job for an FTP Job # When the files are found the script will increase the counter and list how many files were found that match the FileMask specified. # NOTE: The Parameters found in the options would need to exist as parameters in the FTP job or listed as Variables # [Options]
#
## FileMask parameter is a pattern of characters and wildcards used to match folder and filenames ( *123.txt, 123*abc.txt)
Write-Host "Filemask: " <<filemask>>
# SFTP parameter - Set 0 to Off for standard FTP and set to 1 for SFTP Write-Host "SFTP Option: " <<SFTP>> # PRECHECK parameter would be the number of files to check for
Write-Host "Expected Count: " <<PRECHECK>> # FTPCred parameter would be the FTP Credentials
Write-Host "FTP Cred: " <<FTPCred>> # FTPPARAMS parameter would be options for Server Binary Port and Timeout options
Write-Host "Extra FTP Params:" <<FTPPARAMS>> # FTPHostName paramter would be the host name of the FTP Server
Write-Host "FTP Host Name:" <<FTPHostName>> # RemoteSourcePath parameter would be the path of the remote source
Write-Host "Remote Source Location:" <<REMOTESOURCEPATH>> # ResubmitSleep parameter would be the time alotted for polling job to sleep
Write-Host "Sleep Time:" <<RESUBMITSLEEP>>
$foundFiles = $false do
{
#
# Count
#
$counter = 0 #
#[Get Credentials from a JAMS User]
#
$userCredentials = Get-JAMSCredential -UserName "<<FTPCred>>" -Server localhost #
# [Establishing a Connection. Check whether to use SFTP or FTP]
#
$SFTP = <<SFTP>>
if ($SFTP -eq 1)
{
Write-Host "Attempting to connect via JSFTP"
Connect-JSFTP <<FTPPARAMS>> -JAMSCredential $userCredentials -Name " <<FTPHostName>>"
}
else
{
Write-Host "Attempting to connect via JFTP"
Connect-JFTP <<FTPPARAMS>> -Credential $userCredentials -Name "<<FTPHostName>>"
} #
# [Set the Remote Location]
# if("<<REMOTESOURCEPATH>>".Length -gt 0)
{
Set-JFSLocation "<<REMOTESOURCEPATH>>"
} #
# [If an array of File masks was provided look through each]
#
if("<<filemask>>".Contains(','))
{
Write-Host "Filemask is an Array" #
# [Split the Filters into an Array]
# $filterAry = "<<filemask>>".Split(',')
#
# [Check for files matching each mask]
#
foreach($filter in $filterAry)
{
Write-Host "Current Mast:$filter"
#
# [Look for files matching the filemask]
#
Get-JFSChildItem $filter | ? {$_.IsFile -eq $true} | ForEach-Object {
#
# [File Found, increment counter]
#
Write-Host "File found: " $_.name
$counter = $counter + 1
}
}
}
else
{
#
# [Look for files matching the filemask]
#
Get-JFSChildItem "<<filemask>>" | ? {$_.IsFile -eq $true} | ForEach-Object {
#
# [File Found, increment counter]
# Write-Host "File found: " $_.name
$counter = $counter + 1
}
} #
#[Disconnect from the server]
#
Disconnect-JFS Set-JAMSStatus "Found $counter matching files." -ErrorAction "Continue"
#
# [If files were found continue processing. Otherwise sleep and try again]
#
if ($counter -ge <<PRECHECK>>)
{
Write-Host "$counter File(s) found!"
$foundFiles = $true
}
else
{
#
# [Files not found]
#
Write-Host "Files not ready will try again in <<RESUBMITSLEEP>>seconds."
Write-Host "Found $counter of <<PRECHECK>> files."
Start-Sleep <<RESUBMITSLEEP>>
}
}
while($foundFiles -eq $false)
This is great except I was getThis is great except I was getting a 'failure;failure' error message when there were no files matching the file mask - at this point: 'Get-JFSChildItem "<>"'. I worked around the problem by adding a try..catch block. Worked fine if there were files - bit bizarre.