PowerShell File Copy/Move Drippers (for BizTalk or other reasons)

Sometimes with testing (especially with BizTalk), you need to copy or move some or all files in one folder to another, or all files after a given date. But, you want to space out the copy/move of the files, and that’s what I mean by “dripping” them. You want the files to drip in for example every 10 seconds or every minute.

If you are moving the files, instead of copying them, I strongly suggest making a backup of the source directory before running the scripts.

Note that you can sort files in different ways (primarily by Name or CreationTime).

Example #1


cls
$fromFolder = "C:\Dir\From" 
$toFolder = "C:\Dir\To" 

$files = Get-ChidlItem -Path $fromfolder | sort CreationTime

$startFileName = "ABC1.xls"  # only copy this file and files created after it 

$maxFiles = 999 
$fileCounter = 0 
$processedFileCounter = 0 
$foundStartFile = $false 
$sleepSeconds = 15 

foreach ($file in $files) 
{
    $fileCounter += 1 
    Write-Host "$fileCounter : $($file.name)" 

    if ($file.name -eq $startFileName) 
       {
          $foundStartFile = $true 
          Write-Host "Set foundStartFile to true" 
       }

    if ($foundStartFile) 
       {
           $processedFileCounter +=1 
           if ($processedFileCounter -ge $maxFiles) 
               {
                     Write-Host "Stopped because reached maxFiles=$maxFiles"  
                     break 
               }
           Copy-Item -Path $file.FullName -Destionation $toFolder 
           Start-Sleep $sleepSeconds 
       } 
}  
Write-Host "Number of Files Moved: $processedFileCounter "

Example #2


cls
$fromFolder = "C:\Dir\From" 
$toFolder = "C:\Dir\To" 

$files = Get-ChidlItem -Path $fromfolder | sort Name 

$startFileName = "ABC1.xls"  # only copy this file and files created after it 

$maxFiles = 999 
$fileCounter = 0 
$processedFileCounter = 0 
$sleepSeconds = 5

foreach ($file in $files) 
{
     $fileCounter += 1 
     Write-Host "$fileCounter : $($file.name)" 

     # only get files that contain the strings "ABC" or "DEF" in the filename 
     if ( ($file.name.IndexOf("ABC") -gt 0) -or 
          ($file.name.IndexOf("DEF") -gt 0) ) 
        {
          $processedFileCounter  += 1 
           Move-Item -Path $file.Fullname -Destination $toFolder 
           Start-Sleep $sleepSeconds 
        }
} 
Write-Host "Number of Files Moved: $processedFileCounter "

Leave a Reply