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