简体   繁体   中英

Powershell CreateFromDirectory Process cannot access file because it is being used by another process

I have massive 500GB index files on a Windows Server 2012R2 which are being constantly updated by an application.

I have to zip the file using PowerShell, but when I try to zip it using the following snippet of code I get this exception:

Exception calling "CreateFromDirectory" with "4" argument(s): "The process cannot access the file 'E:\\Program Files (x86)\\Application Folder\\File\\Status.FCS' because it is being used by another process."

$zip = "E:\Folder\File.zip"
$can = "E:\Program Files (x86)\Application Folder\File

Import-Module AWSPowerShell

# functions
function ZipFiles( $zipfilename, $sourcedir )
{
    Add-Type -Assembly System.IO.Compression.FileSystem
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
    $zipfilename, $compressionLevel, $false)
}

ZipFiles $zip $can

I don't have any issues if I am compressing the files using the Windows GUI, but problem seems to happen only when I am using a PowerShell script. Also if I stop application services, the PowerShell script works fine (which I can't do in prod environment).

One of the solution is to copy folder with 500Gb of index and compress it (which should work) but I don't have enough disk space on my Windows server to do so.

So is there any solution to compress the file while it is write locked using PowerShell script?

you need to set a different directory for the zip file : [System.IO.Directory]::SetCurrentDirectory($inPath) so

Function ZipFiles( $zipFileName, $pathTarget )
{
   Add-Type -Assembly System.IO.Compression.FileSystem
   # Emplacement de sortie
   [System.IO.Directory]::SetCurrentDirectory($inPath)
   $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
   [System.IO.Compression.ZipFile]::CreateFromDirectory($pathTarget, $zipFileName, $compressionLevel, $false)

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM