【问题标题】:Unable to delete a directory in Azure无法删除 Azure 中的目录
【发布时间】:2021-01-01 08:13:57
【问题描述】:

我在 Azure 中有一个文件共享,其中包含里面的文件夹,而里面又包含许多文件夹。 我正在尝试通过右键单击该文件夹手动删除一个文件夹,该文件夹里面有很多文件,它说

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

如何删除目录?目录中有数千个文件要删除,不能手动删除每个文件才能删除目录

【问题讨论】:

  • 如果您不喜欢使用代码删除非空文件夹,那么您可以使用工具Azure Storage ExplorerAzCopy。有关详细信息,请参阅下面的答案。

标签: azure file azure-storage azure-files azure-storage-files


【解决方案1】:

只需将其从连接的机器中移除即可。

Remove-Item -Recurse -Force "your directory"

【讨论】:

    【解决方案2】:

    更新:

    你可以使用Azure Storage Explorer(请参考this article关于如何安装和使用它。),然后导航到你的文件共享->右键单击文件夹->选择删除。这可以删除一个非空文件夹。

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


    原文:

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

    请考虑为此目的编写一些代码。还有一个article 使用powershell 删除一个非空文件夹。下面是本文用到的powershell代码(也可以在githubhere找到源码):

    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    
    }
    

    【讨论】:

    • 在给出的所有答案中,我已经尝试使用 Azcopy 命令删除非空文件夹,它工作得非常好。
    【解决方案3】:

    尝试使用Azure CLI

    export CONN_STRING="<YOUR-CONNECTION-STRING>"
    
    az storage share delete \
       --connection-string $CONN_STRING \
       --name filesharename
    

    【讨论】:

      【解决方案4】:
      1. 使用虚拟机连接到您的 Azure 容器(如果是文件共享,则转到文件共享 > 连接 > 并按照命令粘贴到您的虚拟机 - 以连接到文件共享)。
      2. 在虚拟机命令界面中连接到您的容器(cd
      3. 删除文件夹 (rm -rf)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-09
        • 2012-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多