Batch job to delete files older than 2 days

nash Red 1 Reputation point
2021-01-04T23:00:59.023+00:00

Hi, I created a script to delete files and folders that are older than 2 days. I was able to run it but when there are no files the job fails. How to ignore/ by pass when there are no files, My scripts fail if no files exist at that location.

@Echo off
:: set folder path
net use Y: \networkDrive\QA
:: set min age of files and folders to delete
set max_days=2
:: remove sub directories from %dump_path%
forfiles.exe /p Y:\ErrorFolder/d -%max_days% /c "cmd /c IF @isdir == TRUE rd /S /Q @mutia keyza "
net use Y: /delete /Yes

Azure Batch
Azure Batch
An Azure service that provides cloud-scale job scheduling and compute management.
374 questions
{count} votes

1 answer

Sort by: Most helpful
  1. prmanhas-MSFT 17,946 Reputation points Microsoft Employee Moderator
    2021-01-05T07:29:12.987+00:00

    @nash Red Thank you for your query!!!

    I did some research and found below script.Can you check the below script and let me know if it worked for you or not:

    ## Iterate through each blob   
    foreach($blob_iterator in $blob_list){   
       
        $blob_date = [datetime]$blob_iterator.LastModified.UtcDateTime   
           
        # Check if the blob's last modified date is less than the threshold date for deletion   
        if($blob_date -le $date_before_blobs_to_be_deleted) {   
       
            Write-Output "-----------------------------------" | Out-File $local_log_file_path -Append   
            write-output "Purging blob from Storage: " $blob_iterator.name | Out-File $local_log_file_path -Append   
            write-output " " | Out-File $local_log_file_path -Append   
            write-output "Last Modified Date of the Blob: " $blob_date | Out-File $local_log_file_path -Append   
            Write-Output "-----------------------------------" | Out-File $local_log_file_path -Append   
       
            # Cmdle to delete the blob   
            #Remove-AzureStorageBlob -Container $container -Blob $blob_iterator.Name -Context $context   
       
            $blob_count_deleted += 1   
        }   
       
    }  
    

    You can refer to this for more info.

    Another thread which might be helpful to your use case.

    Hope it helps!!!

    Please "Accept as Answer" if it helped so it can help others in community looking for help on similar topics.

    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.