
$time = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
$logfile = 'D:\ifsbosystem\log\FSV.EWOPS.Mover.log'
$destination = 'D:\IFSBOData\FSV\Outgoing\REJK-P-MDL003V\DCP_ud'
$notprocessedpath = 'D:\ifsbodata\fsv\outgoing_notprocessed_files\*'

Add-Content -Path $logfile -Value "$time - Job start"

$folders = Get-Item -Path $notprocessedpath | Where { [int]::Parse($_.Name) -ge 20161030 }
if(-not $folders) { 
    Add-Content -Path $logfile -Value "$time - Job stopping prematurely - No folders found"
        
}
else {
    $folderCount = @($folders).Count

    Add-Content -Path $logfile -Value "$time - Found $folderCount folder(s)"

    $files = $folders | Get-ChildItem -Recurse | Where { $_.Attributes -notcontains 'directory' -and $_.Name -notmatch 'csv.gz' }
    if(-not $files) { 
        Add-Content -Path $logfile -Value "$time - Job stopping prematurely - No files found"
    }
    else {

        $fileCount = @($files).Count

        Add-Content -Path $logfile -Value "$time - Found $fileCount file(s)"

        $files | % { 
            $file = $_
            try {
                Add-Content -Path $logfile -Value "$time - Moving $($file.Name)" 
                $file | Move-Item -Destination $destination -ErrorAction Stop 
            }
            catch {
                Add-Content -Path $logfile -Value "$time - Failed: $($_.ToString().Trim())" 
            }

        }

        Add-Content -Path $logfile -Value "$time - Job complete"
    }
}