Skip to content

Instantly share code, notes, and snippets.

@ChadDevOps
Created July 9, 2021 17:36
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ChadDevOps/aab175a982a368b371d721c458e4698c to your computer and use it in GitHub Desktop.
Recursively extract all zip files into filename subdirectory
$folderPath=Get-Location;
$dash = '-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-'
Get-ChildItem $folderPath -recurse | %{
if($_.Name -match "^*.`.zip$")
{
write-host $dash
write-host '-+-+-+-+-+-+ EXTRACTING'
write-host $dash
$filenameandext = Split-Path $_.Name -leaf
$parent="$(Split-Path $_.FullName -Parent)";
$parent = (New-Object -ComObject Scripting.FileSystemObject).GetFolder($parent).ShortPath
$fullPath = $parent + '\' + $filenameandext
$filename = [io.path]::GetFileNameWithoutExtension($_.FullName)
$outpath = $parent + '\' + $filename
$temp = New-Item -ItemType Directory -Force -Path $outpath
$OutShortPath = (New-Object -ComObject Scripting.FileSystemObject).GetFolder($outpath).ShortPath
write-host "From: $fullPath"
write-host "To: $OutShortPath"
$arguments=@("-aoa", "e", "`"$fullPath`"", "-o`"$OutShortPath`"");
$ex = start-process -FilePath "`"C:\Program Files\7-Zip\7z.exe`"" -ArgumentList $arguments -wait -PassThru -NoNewWindow;
if( $ex.ExitCode -eq 0)
{
write-host "Extraction successful, deleting $fullPath"
rmdir -Path $fullPath -Force
}
write-host $dash
write-host
write-host
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment