Iofacture
Iofacture

Reputation: 685

Exclude sub-directories from Compress-Archive Powershell Cmd

I apologize in advance if this question seems odd, this is my first time trying to use powershell for anything like this.

I have a directory structure similar to the following:

DIR1/
    bin/
    obj/
    d1-file1
    d1-file2 
DIR2/
    bin/
    obj/
    d2-file1
    d2-file2
DIR3/
    bin/
    obj/
    d3-file1
    d3-file2

I am trying to compress this list of directories with a set of top-level and sub-level exclusions. For example, I'd like to exclude a specific top-level directory (eg. DIR2) while also excluding specific sub-directories from the final result set (eg. bin, obj).

I have tried a few different approaches but unfortunately, when I iterate through the sub-directories the compress-archive command will zip up the sub-level items without retaining the top level directory structure. I'd like to retain the top-level directory structure in the archive while also excluding the specified sub-directories.

$entries = New-Object System.Collections.Generic.List[System.Object] 

# Get initial directory listing
$dirlist = Get-ChildItem $solutionFileLocation -ad -Exclude "DIR2" | ForEach-Object { $_.FullName }

# Exclude bin and obj directories and add entry path to our zip entry list
foreach ($dir in $dirlist)
{
    $sub = Get-ChildItem $dir -Exclude "bin", "obj" | ForEach-Object { $_.FullName }

    foreach ($itm in $sub)
    {
        $entries.Add($itm)
    }
}

Compress-Archive -LiteralPath $entries -CompressionLevel Optimal -DestinationPath "C:\Filename01212019.zip"

The result is an archive containing the following:

d1-file1
d1-file2
d3-file1
d3-file2

Instead of the desired result:

DIR1/
    d1-file1
    d1-file2 
DIR3/
    d3-file1
    d3-file2

Upvotes: 0

Views: 2907

Answers (2)

Jevgenij Kononov
Jevgenij Kononov

Reputation: 1237

$exclude = $settings.appFolderName

$folders = get-ChildItem -Path $settings.sourceFolderPath | Where-Object {$_.name -notlike $exclude} | Select-Object FullName

$zipFold = @();
foreach( $item in $folders){    
    $zipFold +=$item.FullName
}

Compress-Archive -Path $zipFold -DestinationPath $zipDir -CompressionLevel Fastest

Upvotes: 1

Robert Cotterman
Robert Cotterman

Reputation: 2268

Here's a possible option (what i meant by my comment)

$entries = New-Object System.Collections.Generic.List[System.Object] 

$exclude = "DIR2,DIR6"

# Get initial directory listing
$dirlist = Get-ChildItem $solutionFileLocation -ad -Exclude "$exclude" | ForEach-Object { $_.FullName }

# Exclude bin and obj directories and add entry path to our zip entry list
foreach ($dir in $dirlist)
{
    $sub = Get-ChildItem $dir -Exclude "bin", "obj" | ForEach-Object { $_.FullName }

    $entries.Add("$($dir.split('/')[-1])/")
    foreach ($itm in $sub)
    {
        $entries.Add($itm)
    }
}

Compress-Archive -LiteralPath $entries -CompressionLevel Optimal -DestinationPath "C:\Filename01212019.zip"

Let me know if you'd like me to refine it more. I feel there's a much simpler way.

Upvotes: 0

Related Questions