Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compress-Archive Error: Cannot access the file because it is being used by another process

Tags:

powershell

I would like to zip a path (with a service windows running inside).

When the service is stopped, it works perfectly, when the service is running, I have the exception:

The process cannot access the file because it is being used by another process.

However, when I zip with 7-zip, I don't have any exception.

My command:

Compress-Archive [PATH] -CompressionLevel Optimal -DestinationPath("[DEST_PATH]") -Force

Do you have any idea to perform the task without this exception?

like image 994
Bob Avatar asked Mar 08 '23 15:03

Bob


1 Answers

Copy-Item allows you to access files that are being used in another process.

This is the solution I ended up using in my code:

Copy-Item -Path "C:\Temp\somefolder" -Force -PassThru | 
Get-ChildItem |
Compress-Archive -DestinationPath "C:\Temp\somefolder.zip"

The idea is that you pass through all the copied items through the pipeline instead of having to copy them to a specific destination first before compressing.

I like to zip up a folder's content rather than the folder itself, therefore I'm using Get-ChildItem before compressing in the last line.

Sub-folders are already included. No need to use -recurse in the first line to do this

like image 117
monadwhat Avatar answered Apr 26 '23 15:04

monadwhat