Skip to content

Instantly share code, notes, and snippets.

@JohnL4
Last active February 7, 2020 16:31
Show Gist options
  • Save JohnL4/4bd8fd056fdf41ca0eb55580192c9c0a to your computer and use it in GitHub Desktop.
Save JohnL4/4bd8fd056fdf41ca0eb55580192c9c0a to your computer and use it in GitHub Desktop.
PowerShell cmd to zip up a directory while excluding multiple subdirectories
ls -rec | ? {-not ($_.FullName -match '\\(NUnitConsole|TestRun)\\')} | write-zip -output "$(datefn).zip"
#### If you want to get fancier...
# (1) Make a list of files touched in the last hour.
$fs = ls c:\work\sxa\18.4cu2\Projects\VisitRecord\VRDotNet\Portal\SXA.VR.Portal\bin `
| ? {$_.LastWriteTime -ge (Get-Date).AddMinutes(-60)}
# (2) Add to it. The '+=' operator actually makes a new Array by copying the old Array + the entry(ies).
# This is how you get another file, even if it wasn't touched in the last hour.
$fs += (ls c:\work\sxa\18.4cu2\Projects\VisitRecord\VRDotNet\Portal\SXA.VR.Portal\Views\Home\Index.cshtml)
# (3) Zip it all up, sans directory structure. Write-Zip is from PSCX. Sadly, its -Append option seems to be broken,
# so we have to build our list BEFORE we write the zip file. Oh well, good learning list-management skillz. :)
pushd c:/tmp
$fs | write-zip -out web-server-dlls.zip -flat -entry /
@JohnL4
Copy link
Author

JohnL4 commented Nov 15, 2016

Apparently, the -exclude option is borked, so we do it with regular expressions.
'datefn' is a function that returns a string timestamp suitable for use as a filename.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment