<#
===========================================================================================
| |
| Variables |
| |
===========================================================================================
#>
$rarExecutablePath = "${env:ProgramFiles}\WinRAR\rar.exe"
$recursiveSearch = $true
$logErrorFilePath = "$PSScriptRoot\RAR_Test_Files.log"
<#
===========================================================================================
| |
| Functions |
| |
===========================================================================================
#>
function Show-WelcomeScreen {
Clear-Host
Write-Host ""
Write-Host " $($host.ui.RawUI.WindowTitle)"
Write-Host " +===================================================+"
Write-Host " | |"
Write-Host " | This script will use RAR.exe to test each file in |"
Write-Host " | the current working directory, and inform about |"
Write-Host " | any errors found during the test. |"
Write-Host " | |"
Write-Host " +===================================================+"
Write-Host ""
Write-Host " Script Settings " -ForegroundColor DarkGray
Write-Host " ===========================" -ForegroundColor DarkGray
Write-Host " RAR Executable Path: $([System.IO.Path]::GetFullPath($rarExecutablePath))" -ForegroundColor DarkGray
Write-Host " Recursive Search...: $recursiveSearch" -ForegroundColor DarkGray
Write-Host ""
}
function Confirm-Continue {
Write-Host " Press 'Y' key to continue or 'N' to exit."
Write-Host ""
Write-Host " -Continue? (Y/N)"
do {
$key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$char = $key.Character.ToString().ToUpper()
if ($char -ne "Y" -and $char -ne "N") {
[console]::beep(1500, 500)
}
} while ($char -ne "Y" -and $char -ne "N")
if ($char -eq "N") {Exit(1)} else {Clear-Host}
}
function Test-RarFiles {
Write-Host "Fetching RAR files, please wait..." -ForegroundColor DarkGray
Write-Host ""
$rarFiles = $null
if ($recursiveSearch) {
$rarFiles = Get-ChildItem -LiteralPath "$PSScriptRoot" -Filter "*.rar" -Recurse -File
} else {
$rarFiles = Get-ChildItem -LiteralPath "$PSScriptRoot" -Filter "*.rar" -File
}
$rarFileCount = $rarFiles.Count
$currentFileCount = 0
If ($rarFileCount -eq 0) {return}
$testSuccessCount = 0
$testFailCount = 0
$logStream
= [System.IO.StreamWriter]::new($logErrorFilePath, $false
) foreach ($rarFile in $rarFiles) {
$currentFileCount +=1
Write-Host "RAR File $currentFileCount of $($rarFileCount):" -ForegroundColor DarkGray
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = $rarExecutablePath
$psi.Arguments = "t -idcdn `"$($rarFile.FullName)`""
$psi.RedirectStandardOutput = $false
$psi.UseShellExecute = $false
$psi.CreateNoWindow = $false
$process = [System.Diagnostics.Process]::Start($psi)
$process.WaitForExit()
$exitCode = $process.ExitCode
if ($exitCode -eq 0) {
Write-Host "RAR process exited with code: $exitCode" -ForegroundColor Green
$testSuccessCount +=1
} else {
Write-Warning "RAR process exited with code: $exitCode"
$logStream.WriteLine("Failed test with exit code $($exitCode): $($rarFile.FullName)")
$testFailCount +=1
}
Write-Host ""
}
$logStream.Close()
$SuccessPercent = [string]::Format("{0:F0}", ($testSuccessCount / $currentFileCount) * 100)
if ($testSuccessCount -eq $currentFileCount) {
Write-Host "RAR Tests Successful: $testSuccessCount of $currentFileCount ($SuccessPercent%)" -BackgroundColor Black -ForegroundColor Green
} else {
Write-Warning "RAR Tests Successful: $testSuccessCount of $currentFileCount ($SuccessPercent%)"
}
$FailPercent = [string]::Format("{0:F0}", ($testFailCount / $currentFileCount) * 100)
if ($testFailCount -eq 0) {
Write-Host "RAR Tests Failed....: $testFailCount of $currentFileCount ($FailPercent%)" -BackgroundColor Black -ForegroundColor Green
} else {
Write-Warning "RAR Tests Failed....: $testFailCount of $currentFileCount ($FailPercent%)"
}
}
function Show-GoodbyeScreen {
Write-Host ""
Write-Host "Operation Completed!" -BackgroundColor Black -ForegroundColor Green
Write-Host ""
Write-Host "Press any key to exit..."
$key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
Exit(0)
}
<#
===========================================================================================
| |
| Main |
| |
===========================================================================================
#>
[System.Console]::Title = "Test RAR Files - by Elektro"
#[System.Console]::SetWindowSize(150, 45)
[CultureInfo]::CurrentUICulture = "en-US"
try { Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Scope "Process" } catch { }
Show-WelcomeScreen
Confirm-Continue
Test-RarFiles
Show-GoodbyeScreen