【发布时间】:2014-08-02 12:57:29
【问题描述】:
我有以下脚本可以获取早于指定日期的文件并将它们作为存档保存在不同的文件夹中。该脚本适用于大多数文件,但对于某些文件,它会出现此错误并且不会将这些文件包含在存档中。
Move-Item : The process cannot access the file because it is being used by another process.
At C:\SRS\SRSLogArchieveScript.ps1:49 char:13
+ Move-Item <<<< $item.FullName $destPathController -force;
+ CategoryInfo : WriteError: (C:\SRS\Controll...apter0611-1.log:FileInfo) [Move-Item], IOException
+ FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
但是我将-force 与-move item 一起使用,但它仍然存在这个问题。有没有办法可以强制关闭所有正在使用的文件,以确保所有文件都包含在存档中。
try
{
foreach( $item in (Get-ChildItem $sourcePath -recurse | Where-Object { $_.CreationTime -le $archiveTillDate }) )
{
$item.FullName;
Move-Item $item.FullName $destPath -force;
}
}
catch
{
$_.Exception.Message;
}
Finally
{
#Executing Zip Command to Archieve:
& $pathTo7ZipExe $arguments;
#Delete temporary log archieve directory
Remove-Item $logArchieve -recurse;
}
【问题讨论】:
-
您可以使用Handle.exe 来识别正在使用该文件的进程。然后你可以杀死这个进程。你的意思是覆盖文件当它被锁定时,我认为这是不可能的。
标签: powershell powershell-2.0 windows-server-2008-r2