powershell 无法删除空文件夹(拒绝访问云文件)

x4shl7ld  于 2023-04-06  发布在  Shell
关注(0)|答案(2)|浏览(222)

在使用Remove-Item递归删除空文件夹的PS脚本中,我得到了一些空文件夹的异常“拒绝访问云文件”。

y53ybaqx

y53ybaqx1#

即使我没有安装OneDrive,本文https://evotec.xyz/remove-item-access-to-the-cloud-file-is-denied-while-deleting-files-from-onedrive/中的解决方案$dir.Delete($true)也可以工作

$directoriesToDelete = Get-ChildItem -Path $pathToPictures -Recurse -Directory
foreach ($directoryToDelete in $directoriesToDelete) {
    $pathToDirectory = $directoryToDelete.FullName
    if ((Get-ChildItem $pathToDirectory | Measure-Object).Count -eq 0) {
        try {
            Remove-Item $pathToDirectory -Force
            Add-LogEntry -PathToLog $logFile -LogEntry "$pathToDirectory deleted"
        } catch {
            if ($_.Exception.Message -eq 'Access to the cloud file is denied') {
                $dir = Get-Item -LiteralPath $pathToDirectory
                try {
                    $dir.Delete($true)
                } catch {
                    Add-LogEntry -PathToLog $logFile -LogEntry "Error deleting '$pathToDirectory' (using dir.Delete): $_"
                }
            } else {
                Add-LogEntry -PathToLog $logFile -LogEntry "Error deleting '$pathToDirectory': $_"
            }
        }
    }
}
0yg35tkg

0yg35tkg2#

我得到了这个错误的Windows.old文件夹有OneDrive子文件夹.什么工作是运行
chkdsk c: /r /f

相关问题