无法删除 Azure 中的目录

vik*_*kky 7 file azure azure-storage azure-files azure-storage-files

我在 Azure 中有一个文件共享,其中包含文件夹,而文件夹内又包含许多文件夹。我试图通过右键单击该文件夹来手动删除该文件夹,该文件夹内有很多文件,并且显示

删除目录失败。错误:指定的目录不为空。

怎样才能删除该目录呢?需要删除的目录中有数千个文件,并且无法手动删除每个文件以删除该目录

Iva*_*ang 7

更新:

您可以使用Azure Storage Explorer(请参阅本文了解如何安装和使用它。),然后导航到您的文件共享 - >右键单击该文件夹 - >选择删除。这可以删除非空文件夹。

或者您可以使用AzCopy(有关此工具的更多详细信息,请参阅此处)与azcopy remove命令和--recursive参数。


原来的:

无法删除 azure 文件共享中的非空文件夹,您应该首先删除其中的所有文件。

请考虑为此目的编写一些代码。还有一篇文章使用powershell删除非空文件夹。以下是本文使用的 powershell 代码(您也可以在 github 中找到源代码

function RemoveFileDir ([Microsoft.Azure.Storage.File.CloudFileDirectory] $dir, [Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext] $ctx)
{   
    $filelist = Get-AzStorageFile -Directory $dir
    
    foreach ($f in $filelist)
    {   
        if ($f.GetType().Name -eq "CloudFileDirectory")
        {
            RemoveFileDir $f $ctx #Calling the same unction again. This is recursion.
        }
        else
        {
            Remove-AzStorageFile -File $f           
        }
    }
    Remove-AzStorageDirectory -Directory $dir
    
} 


#define varibales
$StorageAccountName = "Your Storage account name" 
$StorageAccountKey = "Your storage account primary key"
$AzShare = "your azure file share name"
$AzDirectory = "LatestPublish - your directory name under which you want to delete everything; including this directry"
 
 

#create primary region storage context
$ctx = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$ctx.ToString()

#Check for Share Existence
$S = Get-AzStorageShare -Context $ctx -ErrorAction SilentlyContinue|Where-Object {$_.Name -eq $AzShare}

# Check for directory
$d = Get-AzStorageFile -Share $S -ErrorAction SilentlyContinue|select Name

if ($d.Name -notcontains $AzDirectory)
{
    # directory is not present; no action to be performed
    
}
else
{    
    $dir = Get-AzStorageFile -Share $s -Path $AzDirectory    
    RemoveFileDir $dir $ctx    
}
Run Code Online (Sandbox Code Playgroud)