Powershell 7zip check file integrity

时间:2016-04-04 17:32:08

标签: powershell 7zip

I'm using the following code to zip some files using 7zip.

$filePath = "c:\test"
$txt = Get-ChildItem -Recurse -Path $filePath | Where-Object { $_.Extension -eq ".txt" }
$limit = (Get-Date).AddDays(-30)

if (-not (test-path "$env:C:\Program Files\7-Zip\7z.exe")) {throw "$env:Program Files (x86)\7-Zip\7z.exe needed"} 
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"

Get-ChildItem -Path $filePath -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

foreach ($file in $txt) { 
$name = $file.name 
$directory = $file.DirectoryName 
$zipfile = $name.Replace(".txt",".zip") 
sz a -mx=9 "$directory\$zipfile" "$directory\$name" 
}

I need to check the integrity of the file once it is zipped and make sure that I can open it. These are some rather large files and I don't need to unzip them, just check them. I have found this command:

7z t somearchive.zip

But all it returns is something like this:

"c:\program files\7-zip\7z" t somefile.7z

 7-Zip [64] 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18

Processing archive: douglas.cx.txt.7z

Testing     somefile.txt

Everything is Ok

Size:       761
Compressed: 492

Once I check the file, it will be moved to another folder, that I can handle. Just not sure what to do with the text that is returned by 7zip.

Any help is appreciated.

1 个答案:

答案 0 :(得分:2)

Line:

  sz a -mx=9 "$directory\$zipfile" "$directory\$name" 

Should be either:

  sz a -mx=9 "$directory\$zipfile" "$directory\$name"  | out-null
  $ok = $LASTEXITCODE -eq 0

or if sz doesn't return exit code you have to parse text

  sz a -mx=9 "$directory\$zipfile" "$directory\$name"  |  set out
  $ok = $out -like '*Everything is Ok*'

This is from head, but one of those 2 should work.