1

I wrote a PowerShell command to zip a folder and its subdirectories while excluding files from the main directory.

When I execute the command it does create the zip file with all subdirectories within, but it doesn't seem to understand that subdirectories are in the same pipeline as the main files.

├── _subfolder
|   └── file.json
├── file.js
└── file.html

So, when I try to open the zip in Firefox's Debugger, I get the following error:

Loading locale file _subfolder/file.json: Error: Error while loading 'jar:file:///C:/Users/xxx/Desktop/folder/archive.zip!/_subfolder/file.json' (NS_ERROR_FILE_NOT_FOUND)

Here's my current code:

# Target Path
$path = "c:\users\xxx\desktop\folder-to-zip\"
# Archive Path
$destination = "C:\users\xxx\desktop\folder\archive.zip"
# Exclude some files in the main folder
$exclude = "file.js"
# Choose files to zip
$files = Get-ChildItem $path -Exclude $exclude
#Compressing archive
Compress-Archive -Update -Path $files -DestinationPath $destination -CompressionLevel Fastest

How can I make sure the zip includes some information about the hierarchy of the files? When I zip manually with WinRAR, I get no error.

Marwann
  • 163
  • 3
  • 14
  • 1
    `$path` is never defined and parameter `Exclude` for [Get-ChildItem](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7#parameters) needs to either be used together with the `Recurse` switch, OR the path needs to end with `\*` – Theo Feb 10 '20 at 11:32
  • My bad, I made a mistake while creating the dummy script for the purpose of this question. Corrected, thanks for notifying me. I have tried both adding the `Recurse` switch or the wildcard, it still does not work. The zip file does have all files inside, including subdirectories and their files, but I still get the error. – Marwann Feb 10 '20 at 17:11

0 Answers0