I need to include empty directories along with files in a zip file. I can do this manually just fine with 7-Zip but I wanted to automate it because I do this quite a lot. I recently started learning powershell so I decided to give it a go.
My problem is that Compress-Archive
automatically discards empty directories. My workaround is ($Files
is a parameter to the script):
$items = Get-ChildItem -Path . | Where-Object { $_.Name -in $Files }
$placeholders = @()
foreach ($item in $items) {
if (($item | Get-ChildItem | Measure-Object).Count -eq 0 ) {
$placeholders += (New-Item -Path "$item\.placeholder")
}
}
and at the end of the script
foreach ($item in $placeholders) {
$item.Delete()
}
which works but it is not pretty as it results in placeholder files being in the final zip.
Is there a good way to compress empty directories in powershell?
EDIT whole script with version info at the bottom:
[CmdletBinding()]
param (
# Files and folders to compress, comma separated
[Parameter(Mandatory)]
[string[]]
$Files,
# zip file to create
[Parameter(Mandatory)]
[string]
$ZipName
)
if (-not $ZipName.Contains(".zip")) {
$ZipName += ".zip"
}
$items = Get-ChildItem -Path . | Where-Object { $_.Name -in $Files }
$placeholders = @()
foreach ($item in $items) {
if (($item | Get-ChildItem | Measure-Object).Count -eq 0 ) {
$placeholders += (New-Item -Path "$item\.placeholder")
}
}
if ((Get-ChildItem -Path . | Where-Object { $_.Name -eq $ZipName } | Measure-Object).Count -ne 0) {
Remove-Item -Path "$ZipName"
}
$items | Compress-Archive -DestinationPath $ZipName
foreach ($item in $placeholders) {
$item.Delete()
}
# output of Get-Host
# Name : ConsoleHost
# Version : 5.1.19041.610
# InstanceId : c799930e-ea5e-4ec9-9e5d-41d949bf4ee4
# UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
# CurrentCulture : en-GB
# CurrentUICulture : en-GB
# PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
# DebuggerEnabled : True
# IsRunspacePushed : False
# Runspace : System.Management.Automation.Runspaces.LocalRunspace
EDIT2 very weird stuff. Tested it again and I swear it does not work for me. If I give it the names of empty directories it doesn't even create a zip file. I'm going to reinstall Windows as soon as my new ssd arrives, maybe that fixes it.
By the way I needed this for Wordpress plugin development as you have to upload the plugins in a zip file. I uploaded the archive I created with this script and it produced a very weird result. Instead of correctly uncompressing the zip as Wordpress has done every single time before what it did was something like this:
some\file\which\should\be\in\a\directory.php
weird\file\again.php
normal.php
No, those are not paths, they are filenames. On Windows I can uncompress it just fine. I am so confused.
Be warned:
If your directory only contains hidden files it may appear to be empty (if you haven't told File Explorer to show hidden files). Then when you use
Compress-Archive
it will actually be empty due to that behavior.The docs go on to say:
This appears to be fixed in 2.0.0, but at time of writing that's been stuck as a preview release since Aug 2022.