File 无法删除脚本中的文件

File 无法删除脚本中的文件,file,powershell,file-io,delete-file,File,Powershell,File Io,Delete File,我在这里疯了。 我有一个脚本,在这个脚本中,我正在解析一个充满TIF的文件夹,并将文件分解为子文件夹,将每个文件夹的页数限制在60页左右。如果一个文档很大,它会有自己的文件夹 问题是进程正在锁定文件,因此无法删除它们。但并不是每个文件都能正常工作,而且我的脚本的最终清理部分会清除所有其他文件 为了解决这个问题,我在代码中写了很多关于部分的工作,现在看起来很糟糕 #Large Documents Get-ChildItem -Path "$directory" -recurse -filt

我在这里疯了。 我有一个脚本,在这个脚本中,我正在解析一个充满TIF的文件夹,并将文件分解为子文件夹,将每个文件夹的页数限制在60页左右。如果一个文档很大,它会有自己的文件夹

问题是进程正在锁定文件,因此无法删除它们。但并不是每个文件都能正常工作,而且我的脚本的最终清理部分会清除所有其他文件

为了解决这个问题,我在代码中写了很多关于部分的工作,现在看起来很糟糕

    #Large Documents
Get-ChildItem -Path "$directory" -recurse -filter "*.tif" | foreach {
    $file = [System.Drawing.Bitmap]::FromFile($_.Fullname);
    $pagecount = $file.GetFrameCount($file.FrameDimensionsList[0]); 
    if ($pagecount -gt $MaxSize){
        $total = $total + $pagecount;
        $name = $_.Basename;
        New-Item $name -ItemType directory;
        Copy-Item $_.fullname -Destination $name;
        #Copy-Item $name".DS" -Destination $processingDir;
        Write-Host "Sleeping in large doc loop";
        $file.Dispose;
        Write-Host "Dispose file object";
        Write-Host $_.Fullname
        $storename = $_.Fullname
        $largeFiles = $largeFiles + $storename      
        Write-Host "Storing to array: " $largeFiles[$index];
        $index = $index + 1;
        sleep(15);
    }
}
while ($delInd -lt $largeFiles.Count){
    Write-Host "Deleting: " $largeFiles[$delInd];
    Remove-Item $largeFiles[$delInd] -Force;
    $delInd = $delInd + 1;
}

我对此感到非常困惑。非常感谢您的帮助

据我所知,
$file.Dispose
不强制底层对象关闭文件
Dispose
是一种方法,在PowerShell中(如在C#中),要调用方法,必须使用
()
。因此,请尝试
$file.Dispose()


一条建议:你可以避免
在行尾

锁定可以通过文件索引器或防病毒软件完成。请尝试使用SysInternals工具(例如Handles.exe-->)查看哪些进程正在使用这些文件。是否收到错误消息?如果是的话,把它贴在这里。