PowerShell + 7Zip + Long File Paths + Delimon.Win32.IO.dll

Hello PowerShell Guru’s,

I am wondering if someone can tell me why this code isn’t working. I’ve reviewed and reviewed it, searched Google up and down with no love.

 #Set variables for subversion paths
$ScriptPath = (Split-Path $myinvocation.MyCommand.path -Parent)
[Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Delimon\Delimon.Win32.IO\Delimon.Win32.IO.dll")
Add-Type -AssemblyName Delimon.Win32.IO
Use Delimon.Win32.IO

$scriptDir = "C:\FII"
$z = "C:\Program Files\7-Zip\7z.exe"

$files=gci $scriptDir -r | where {$_.Extension -match "zip"}
foreach ($file in $files) {
    $source = $file.FullName
    $destination = Delimon.Win32.IO.Directory.Join-Path (Split-Path -parent $file.FullName) $file.BaseName
    write-host -fore green $destination
    $destination = "-o" + $destination
    & ".\$z" x -y $source $destination
}

$files=gci $scriptDir -r | where {$_.Extension -match "prd"}
foreach ($file in $files) {
    $source = $file.FullName
    $destination = Delimon.Win32.IO.Directory.Join-Path (Split-Path -parent $file.FullName) $file.BaseName
    write-host -fore green $destination
    $destination = "-o" + $destination
    Start-Process 'C:\Program Files\7-Zip\7z.exe x -y $source $destination'
}

Here are the errors I am currently receiving:

GAC    Version        Location                                                                    
---    -------        --------                                                                    
False  v4.0.30319     C:\Program Files (x86)\Delimon\Delimon.Win32.IO\Delimon.Win32.IO.dll        
Add-Type : Cannot add type. The assembly 'Delimon.Win32.IO' could not be found.
At C:\SVN_Repo\Untitled7.ps1:2 char:1
+ Add-Type -AssemblyName Delimon.Win32.IO
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Delimon.Win32.IO:String) [Add-Type], Exception
    + FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeCommand
 
Add-Type : Cannot add type. One or more required assemblies are missing.
At C:\SVN_Repo\Untitled7.ps1:2 char:1
+ Add-Type -AssemblyName Delimon.Win32.IO
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
    + FullyQualifiedErrorId : ASSEMBLY_LOAD_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
 
Use : The term 'Use' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path 
is correct and try again.
At C:\SVN_Repo\Untitled7.ps1:3 char:1
+ Use Delimon.Win32.IO
+ ~~~
    + CategoryInfo          : ObjectNotFound: (Use:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
Delimon.Win32.IO.Directory.Join-Path : The term 'Delimon.Win32.IO.Directory.Join-Path' is not 
recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\SVN_Repo\Untitled7.ps1:11 char:20
+     $destination = Delimon.Win32.IO.Directory.Join-Path (Split-Path -parent $fil ...
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Delimon.Win32.IO.Directory.Join-Path:String) [], C 
   ommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

& : The term '.\C:\Program Files\7-Zip\7z.exe' is not recognized as the name of a cmdlet, 
function, script file, or operable program. Check the spelling of the name, or if a path was 
included, verify that the path is correct and try again.
At C:\SVN_Repo\Untitled7.ps1:14 char:7
+     & ".\$z" x -y $source $destination
+       ~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\C:\Program Files\7-Zip\7z.exe:String) [], Comman 
   dNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Start-Process : This command cannot be run due to the error: The system cannot find the file 
specified.
At C:\SVN_Repo\Untitled7.ps1:23 char:5
+     Start-Process 'C:\Program Files\7-Zip\7z.exe x -y $source $destination'
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProces 
   sCommand

You don’t need anything external to perform a Join-Path

$ScriptPath = (Split-Path $myinvocation.MyCommand.path -Parent)

#-match is a regex, so you can do || which is OR
$files=gci "C:\FII" -recurse | where {$_.Extension -match "zip||prd"}
foreach ($file in $files) {
    $source = $file.FullName
    #Join-Path is a standard Powershell cmdLet
    $destination = Join-Path (Split-Path -parent $file.FullName) $file.BaseName
    write-host -fore green $destination
    $destination = "-o" + $destination
    #Start-Process needs the path to the exe and then the arguments passed seperately. You
    #can also add -wait to have the process complete before moving to the next
    Start-Process -FilePath "C:\Program Files\7-Zip\7z.exe" -ArgumentList "x -y $source $destination" -Wait
}

No need to use 3rd party tools to extract zip files.

Add-Type -assembly “system.io.compression.filesystem"

Get-ChildItem \\path\to\files -Filter *.zip | ForEach-Object {
$zipfile = $_.FullName ; $dest = $_.FullName -replace "\.zip$"
[io.compression.zipfile]::ExtractToDirectory($zipfile,$dest)
Write-Verbose "$($_.Name) Extracted" -Verbose
}

Rob,

Thank you so very much! I’ve tested your suggestions on a directory where I have test zip and prd archives. Now to test on the full SVN repository where I have thousands of zip archives. The reason I need to use 7zip is to overcome the 260 character max limit for file path names.

Thank you so much for helping move me along,

Richard

Question about this topic. Is there a way that I can skip two directories that I do not want to change the names of the zip files?