首页 > 解决方案 > Azure CLI - az storage file delete-batch:除一个特定文件外的所有模式

问题描述

我正在尝试从 Azure 存储文件共享中删除所有文件并排除一个特定的文件名。我只是无法弄清楚模式。

我已经尝试过了,但模式现在不匹配任何东西。

az storage file delete-batch --source "https://myst.file.core.windows.net/myshare" --sas-token $sastoken --pattern "[!importantfile.py]"

标签: azureazure-storageazure-clifnmatch

解决方案


您需要在单引号中添加模式参数,如下所示

az storage file delete-batch --source "https://myst.file.core.windows.net/myshare" --sas-token $sastoken --pattern '[!importantfile.py]*' 
  • 如果文件共享中有类似的文件,例如 (test.json.test1.json) ,
    则无法使用带有模式过滤器的 az storage delete-batch 来排除特定文件的删除。

  • 参考 SO 线程如何在az storage blob delete-batch中使用模式

或者,如果希望将特定文件从文件共享中的删除中排除,您可以使用以下 power shell 脚本

connect-azaccount
$accountName = '<accountname>';
$accountKey = '<accountkey>';
$myshare = '<file sharename >';
$notremovalfile = '<file that need to be excluded from deletion>';

 $filelist = az storage file list -s $myshare --account-name $accountName --account-key $accountKey

    $fileArray = $filelist|ConvertFrom-Json 


    foreach ($file in $fileArray)
    {
       if($file.name -ne $notremovalfile)
       {
            Write-Host $file.name
            az storage file delete --account-name $accountName --account-key $accountKey -s $myshare -p $file.name
            Write-Host "deleting $file.name"
        }
          

    }  

推荐阅读