Scenario

Let’s say you have a directory with a 100 files, and you want to slowly drop those file into BizTalk (via a file adapter Receive Location). Rather than copying all the files at once, you would like to wait 2, 5, or 10 seconds between each file drop? Maybe you want to make the tracked message easier to read, by basically making all the process single-threaded.

What I called “File Dripping” is really a form of throttling. Maybe your BizTalk system is stressed, and you don’t want to throw in 1000s of messages at once. BizTalk has to store all those messages in a database “queue” in the SQL database server. Then it takes longer for BizTalk to ‘walk the chain’ or to keep searching through these when doing various queries in the process of administration and processing. By dropping the files in slowly, the BizTalk Message depth never gets very deep.

Furthermore, you might want the files to be processed in date/time order.

PowerShell Solution

We will use Copy-Item (or Move-Item) – configured by a parameter. And to do a wait for x seconds, we use the Powershell Start-Sleep command in . To get the files sorted, you pipe the Get-ChildItem to the commandlet “Sort-Object LastWriteTime -Ascending”.

cls 
$fromDirName = "e:\Integration\Inbound\AppName\SubDir\"
$toDirName = "e:\Integration\Inbound\AppName\FileDropFolder\"
$fileMask = "*.*"
$waitSeconds = 5
$moveOrCopy = "Copy"  


$files = Get-ChildItem $fromDirName -Filter $fileMask -File  | Sort-Object LastWriteTime -Ascending 
$countMatchingFiles = $files.Count 
Write-Host "Count Matching Files=$countMatchingFiles fileMask=$fileMask" 

foreach ($file in $files) 
{
    if ($moveOrCopy -eq "Copy") 
      {
        Copy-Item $file.FullName -Destination $toDirName
        Write-Host "Copied $($file.BaseName)" 
      }
    else 
      {
        Move-Item $file.FullName -Destination $toDirName
        Write-Host "Moved $($file.BaseName)" 
      }
    Write-Host "Waiting $waitSeconds seconds" 
    Start-Sleep -Seconds $waitSeconds 
}

Filed under: Powershell