3

I have Storage Account in Azure which contains files inside the folders. I want to move the .txt and .csv files from one folder to other folder in same Storage Account Container using PowerShell script.

So, can anyone suggest me how to do that?

Pradeep
  • 5,101
  • 14
  • 68
  • 140
  • It seems no direct method to move files, you need to copy first then delete them. – unknown Dec 01 '20 at 09:52
  • I know this is an old question but it just popped up. Can anyone confirm that you can use `Move-AzDataLakeStoreItem` to do this more easily https://learn.microsoft.com/en-us/powershell/module/az.datalakestore/move-azdatalakestoreitem?view=azps-10.0.0 – Nick.Mc Jun 21 '23 at 06:52

5 Answers5

1

Add the complete steps with Swishonary's reply:

$context = New-AzureStorageContext -StorageAccountName {accountName} -StorageAccountKey {Enter your storage account key here}
$Blobs = Get-AzStorageBlob -Container "SourceContainer" -Blob SourceFolder/*.csv -Context $context
foreach ($blob in $Blobs) {
    $blob.Name

    # Copy to DestinationFolder
    Start-AzStorageBlobCopy -SrcBlob "SourceFolder/SourceFile" -SrcContainer "<SourceContainer>" -DestContainer "<DestinationContainer>" -DestBlob "DestinationFolder/DestinationFile"

    # Delete the source blob
    Remove-AzStorageBlob -Container "SourceContainer" -Blob $blob.Name
}
unknown
  • 6,778
  • 1
  • 5
  • 14
0

Please use azcopy in Powershell to acheive this.

azcopy login
azcopy copy 'https://<SourceStorageaccount>.blob.core.windows.net/<SourceContainer>/<SourceFile>' 'https://<DestinationStorageaccount>.blob.core.windows.net/<DestinationContainer>/<DestinationFile>'

The AzCopy command-line utility offers high-performance, scriptable data transfer for Azure Storage. You can use AzCopy to transfer data to and from Blob storage and Azure Files.

Alternatively, I think this can also be used:

Start-AzStorageBlobCopy -SrcBlob "SourceFolder/SourceFile" -SrcContainer "<SourceContainer>" -DestContainer "<DestinationContainer>" -DestBlob "DestinationFolder/DestinationFile"

At the moment, there seems to be no Powershell cmdlets in the Az.Storage module to move files. They would have to be copied to the destination and deleted from the source.

Swishonary
  • 93
  • 1
  • 6
0

Adding more info to the above suggestion, the easiest way is to use Storage Explorer

This So thread provides some idea (PS) on your query.

0

Please note the copy is asynchronous, so make sure the blob is in the destination location before you delete the source. It is said that a copy within the same container is ‘instantaneous’, more like a move operation (which does not exist, nor needs to be in the API because of this)

AndrePKI
  • 31
  • 4
0

This is my code from the answer above.

$context = New-AzStorageContext -StorageAccountName TESTACC -StorageAccountKey PpzcfuKhruAhQL0ZMR

$Blobs = Get-AzStorageBlob -Container "raw-voi-2021" -Blob 2021-11/*2021-09* -Context $context

foreach ($blob in $Blobs) {
     $blob.Name
     $file=$blob.Name.Split("/")[1]
     Start-AzStorageBlobCopy -SrcBlob $blob.Name -SrcContainer "raw-voi-2021" -context $context -DestContainer "raw-voi-2021" -DestBlob "2021-09/$file" -DestContext $context
     Get-AzStorageBlob -Container "raw-voi-2021" -blob "2021-09/$file" -Context $context |select name, length,lastmodified
 }
Nash A
  • 87
  • 6
  • If this is something you are working on right now, can you confirm if you can use `Move-AzDataLakeStoreItem` to do this more easily? https://learn.microsoft.com/en-us/powershell/module/az.datalakestore/move-azdatalakestoreitem?view=azps-10.0.0 – Nick.Mc Jun 21 '23 at 06:53