Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to unzip a bunch of archives including the new xlsx files into a TMP folder and then work on that folder and then remove it. And shit just doesnt want to go my way.

$spath = "C:\_PS\TestFolder"
$destination=”C:\TestFolder\Testzip"

Get-ChildItem $spath -include *.xlsx -Recurse  | foreach-object  {
    # Remove existing TMP folder and create a new one
    Remove-Item -Recurse -Force $destination
    New-Item $destination -type directory

    $archiveFile = $_.fullname | out-string -stream

    "Extract this: " + $archiveFile
    "To here: " + $destination

    $shellApplication = new-object -com shell.application
    $zipPackage = $shellApplication.NameSpace($archiveFile)
    $destinationFolder = $shellApplication.NameSpace($destination)
    $destinationFolder.CopyHere($zipPackage.Items())

}

And it always gives me these errors

Exception calling "NameSpace" with "1" argument(s): "Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))" At C:\_PS\FindCC.ps1:62 char:46
+     $zipPackage = $shellApplication.NameSpace <<<< ($archiveFile)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation   You cannot call a method on a null-valued expression. At C:\_PS\FindCC.ps1:64 char:50
+     $destinationFolder.CopyHere($zipPackage.Items <<<< ())
    + CategoryInfo          : InvalidOperation: (Items:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
share|improve this question
    
Easy. vcsjones.com/2012/11/11/… –  Jupaol Mar 25 at 17:33
add comment

2 Answers

up vote 2 down vote accepted

Your script works fine for ZIP files:

Get-ChildItem $spath -include *.zip -Recurse

I'm not sure why you're using:

 -include *.xlsx

as this is excluding your archive files. If you want to move XLSX files then you'll need to write another block of code to handle the file move.

share|improve this answer
    
xlsx files are just zipped xml files so unzip does/should work –  Elvar Sep 21 '11 at 12:23
    
Yes they are, and it does work if you rename the file extension to ZIP before trying to unpack it - not sure why xlsx aren't 'seen' as archives. –  nimizen Sep 21 '11 at 13:25
    
Copy file to temp, rename file, extract file, do stuff with file, remove temp file and folder. Think thats the order of business then :) –  Elvar Sep 21 '11 at 15:08
add comment
function Extract-Zip 
{ 
    param([string]$zipfilename, [string] $destination) 
    if(test-path($zipfilename)) 
    { 
        $shellApplication = new-object -com shell.application 
        $zipPackage = $shellApplication.NameSpace($zipfilename) 
        $destinationFolder = $shellApplication.NameSpace($destination) 
        $destinationFolder.CopyHere($zipPackage.Items()) 
    }
    else 
    {   
        Write-Host $zipfilename "not found"
    }
} 

Use of the function:
Suppose u want to extract a zip file of name "myzip.zip" into a directory "C:\myFolder".
Make sure "C:\myFolder" directory exists, then run Extract-Zip C:\myzip.zip C:\myFolder

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.