PowerShell Compress-Archive By File Extension
Date : March 29 2020, 07:55 AM
I hope this helps you . I would suggest copying the files to a temporary directory and compress that. Ex: $path = "test"
$filter = "*.config"
#To support both absolute and relative paths..
$pathitem = Get-Item -Path $path
#If sourcepath exists
if($pathitem) {
#Get name for tempfolder
$tempdir = Join-Path $env:temp "CompressArchiveTemp"
#Create temp-folder
New-Item -Path $tempdir -ItemType Directory -Force | Out-Null
#Copy files
Copy-Item -Path $pathitem.FullName -Destination $tempdir -Filter $filter -Recurse
#Get items inside "rootfolder" to avoid that the rootfolde "test" is included.
$sources = Get-ChildItem -Path (Join-Path $tempdir $pathitem.Name) | Select-Object -ExpandProperty FullName
#Create zip from tempfolder
Compress-Archive -Path $sources -DestinationPath config-files.zip
#Remove temp-folder
Remove-Item -Path $tempdir -Force -Recurse
}
|
Using Compress-Archive in Powershell
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I am attempting to create a zip from from a folder using powershell 5 on Windows 10. After looking at this stackoverflow post I am trying out the Compress-Archive method. , This message can appear if the folder you are compressing is empty.
|
Windows 10 powershell ise compress-archive files at root .in .zip
Date : March 29 2020, 07:55 AM
it fixes the issue With your -Path being a folder you explicitly include it in the archive. As already mentioned by Tomalak giving a wildcard changes that. Compress-Archive -Path C:\wamp64\www\myFolder\* -DestinationPath C:\wamp64\www\myFolder\myZip.zip
Compress-Archive C:\wamp64\www\myFolder\* C:\wamp64\www\myFolder\myZip.zip
Get-ChildItem C:\wamp64\www\myFolder\* -File | Compress-Archive C:\wamp64\www\myFolder\myZip.zip
|
Exclude sub-directories from Compress-Archive Powershell Cmd
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I apologize in advance if this question seems odd, this is my first time trying to use powershell for anything like this. , 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"
|
How can I loop through specific file names with the Compress-Archive command in Windows PowerShell?
Date : March 29 2020, 07:55 AM
wish helps you I am not too familiar with Windows PowerShell (and Windows console commands in general), but I wish to write a batch file which can create sperated .zip archives from specific files in a folder, then delete all but the .zip files. So, I have a folder as "C:\myfolder\" and some files in it like: myfile0001.a myfile0001.b myfile0001.c myfile0002.a myfile0002.b myfile0002.c myfile0003.a myfile0003.b myfile0003.c ... and so on. , In PowerShell, you could do: Get-ChildItem -Path 'C:\myfolder' -File | Where-Object { $_.BaseName -match 'myfile\d{4}' } | Group-Object BaseName | ForEach-Object {
$target = Join-Path -Path ($_.Group[0].DirectoryName) -ChildPath ('{0}.zip' -f $_.Group[0].BaseName)
$_.Group | Compress-Archive -CompressionLevel Optimal -DestinationPath $target
$_.Group | Remove-Item -Force
}
powershell -Command "& {Get-ChildItem -Path 'C:\myfolder' -File | Where-Object { $_.BaseName -match 'myfile\d{4}' } | Group-Object BaseName | ForEach-Object { $target = Join-Path -Path ($_.Group[0].DirectoryName) -ChildPath ('{0}.zip' -f $_.Group[0].BaseName); $_.Group | Compress-Archive -CompressionLevel Optimal -DestinationPath $target; $_.Group | Remove-Item -Force}}"
|