posh | event-driven concurrently auto archive/split files by 7z and copy to target

$locked = 0; $locked | Out-File "G:\Last Backup File\lock.txt" -Force
$conc = 0; $conc | Out-File "G:\Last Backup File\conc.txt" -Force

function INIT {
    $fls = 'G:\Last Backup File', 'Y:\Last Backup File', 'Z:\Last Backup File' | %{
        DIR $_ -Filter *.BAK
    }
    $fls | ?{(gc ($_.FULLNAME -REPLACE "\.BAK", ".TXT")) -eq 1} | %{
        0 > ($_.FULLNAME -REPLACE "\.BAK", ".TXT")
    }
    $fls | %{
        $m = $_.name -match "-(?<fd>\d+)"
        $fd = [io.directoryinfo] ($_.DirectoryName + "\" + $Matches.fd)
        if(!$fd.exists){
            $fd.create()
        } ELSE {
            if((gc ($_.FULLNAME -REPLACE "\.BAK", ".TXT")) -eq 1){
                DIR $FD.FULLNAME -Filter *.* | REMOVE-ITEM
            }
        }
    }
    Get-EventSubscriber | Unregister-Event

    Register-EngineEvent -SourceIdentifier queueOne -Action {
        Write-Host "enqueue"
        $fl = getOne
        f7z $fl $ARGS[0]
    }
    
    Register-EngineEvent -SourceIdentifier copyOne -Action {
        $desc = ("copy" + $ARGS[0])
        Write-Host $desc    
        Start-Job -Name $desc -ArgumentList $args[0] -ScriptBlock {  
            param(
                $path
            )  
            Copy-Item -Path $path -Destination "\\dc2sql02\u$\MESSAGESTATS" -Recurse -Force
        }
    }
}

$global:sch = New-Object collections.queue
function getOne {
    try{$global:sch.Dequeue()}
    catch{$null}
}

function getSch {
    $allfls = [collections.arraylist] ('G:\Last Backup File', 'Y:\Last Backup File', 'Z:\Last Backup File')
    $maxr = $allfls.Count
    $rht = @{}
    0..($maxr - 1) | %{
        $r = $_
        $rht[$allfls[$r]] = New-Object collections.queue
        dir $allfls[$r] -Filter *.bak | %{
            $rht[$allfls[$r]].enqueue($_)
        }
    }
    $tbl = [collections.arraylist] ($rht.GetEnumerator() | select-Object -Property key, value, @{
        name = "cnt"; express = {$_.value.count}} | Sort-Object -Property cnt)
    
    
    $rid = 0
    while($tbl.Count -ne 0){
        if($tbl[$rid].value.count -ge 1){
            $global:sch.Enqueue($tbl[$rid].value.dequeue())
            $tbl[$rid].value
        } else {
            $tbl.RemoveAt($rid)
            $maxr--            
        }   
        if($rid -lt $maxr) {$rid++}
        else{$rid = 0}     
    }
}



function f7z {
    param(
        $fl
        , $ID
    )
    IF($FL -ISNOT [io.fileinfo]){BREAK}
    1 > ($FL.FULLNAME -REPLACE "\.BAK", ".TXT")
    $jb = Start-Job -ScriptBlock {
        param($flnm, $ID)
        $fl = [io.fileinfo] $flnm
        Write-Host "ID: $ID start to compress $($fl.fullname)"
        $m = $fl.name -match "-(?<fd>\d+)"
        $TGF = $($fl.DIRECTORYNAME + "\" + $Matches.fd + "\")
        $TGT = $TGF + $($FL.BASENAME)+ ".7z"
        "fl : $fl" > "G:\Last Backup File\tgt.txt"
        $params = @(
            "a", "`"$TGT`"", ("`"" + $fl.fullname + "`""), "-t7z", "-mx9", "-o`"$TGF`"", 
            "-w`"$TGF`"", "-v20M")
        & "C:\Documents and Settings\ayeh\Desktop\7za.exe"  $params
    } -ArgumentList $fl.fullname, $ID
    $TGF = $($fl.DIRECTORYNAME + "\" + $Matches.fd + "\")
    $reg = "Register-ObjectEvent -InputObject `$jb -EventName StateChanged -SupportEvent -Action {
        Write-Host `"ID: $ID finished to compress ```"$($fl.fullname)```"`"
        2 > (`"$($FL.FULLNAME)`" -REPLACE `"\.BAK`", `".TXT`")
        New-Event -SourceIdentifier copyOne -EventArguments `"$TGF`"
        New-Event -SourceIdentifier queueOne -EventArguments $ID
    }"
    iex $reg
}


INIT
getSch
$fl = getOne
f7z $fl 1
#break
$fl = getOne
f7z $fl 2
$fl = getOne
f7z $fl 3
Comments