0

到这里就想不通了。我有一个脚本,我在其中解析一个充满 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;
}

我对此感到非常困惑。任何帮助是极大的赞赏。

4

1 回答 1

2

据我了解,$file.Dispose不要强制底层对象关闭文件。Dispose是一种方法,并且在 PowerShell(如在 C# 中)中,要调用您必须使用的方法()。所以试试$file.Dispose()

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

于 2012-11-14T04:18:49.280 回答