Search code examples
powershell7zip

Not able to find .7z archives across multiple directories


I'm trying to build a simple script where I test all .7z archives within multiple directories against a password a user provides upon input. Currently the script is only able to process the archives if those archives are within the same directory as the script, otherwise I get the following output regardless whether the password is correct or not:

================================================================
7-Zip [64] 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18
Error:
cannot find archive
    Directory:\Documents\Project\FOLDER1
Mode                LastWriteTime         Length Name                          
----                -------------         ------ ----                          
-a----       06/08/2018     16:30            223 Alpha.7z                      
Wrong Password!
================================================================

Please review my code and tell me where I'm going wrong.

$CaseDirectory = Read-Host 'Please input Case directory'
$Password = Read-Host 'Please input Case password'

$7ZipPath = "C:\Program Files\7-Zip\7z.exe"
$Files = Get-ChildItem -Path $CaseDirectory -Recurse -Force -Filter *.7z

$Output = Foreach ($File in $Files)
{
    & $7ZipPath "t" $File "-p$Password"
    if (-not $?)
    {  
       Write-Output $File "Wrong Password!"
    } else {
        Write-Output $File "Password Match"
    }
    Write-Output "================================================================"


} 
$Output | findstr /r /v "^$" | Out-File -FilePath $CaseDirectory\PasswordTest_Output.txt 

pause

Solution

  • Change

    & $7ZipPath "t" $File "-p$Password"
    

    into

    & $7ZipPath "t" $File.FullName "-p$Password"
    

    to use to files full path given by Get-ChildItem