Technical Article

Cleanup backup file folder future 7 days

,

In my project, every one taking the Adhoc backup on different folder which was created an issue to cleanup backup files to find and cleanup. We proposed a standard format as F:\AdhocBackup\Until\YYYYMMDD

Eg: F:\AdhocBackup\Until\200250823

The two scripts below can to achieve the above requirements

  • Create subfolder while taking the backup
  • Cleanup the folder files -> It should create in Job and schedule every day

Note: Add the backup script as per your standard while taking the backup

 

 

--Powershell script

--- Backup folder future 7 days

# Get date after 7 days in yyyyMMdd format
$futureDate = (Get-Date).AddDays(7).ToString("yyyyMMdd")

# Build folder path
$folderPath = "D:AdhochBackupsUntil$futureDate"

# Print to verify
Write-Output "Creating folder: $folderPath"

# Create the folder if it doesn't exist
if (-not (Test-Path $folderPath)) {
New-Item -ItemType Directory -Path $folderPath | Out-Null
Write-Output "Folder created successfully."
}
else {
Write-Output "Folder already exists."
}


------Cleanup folder

$today = Get-Date -Format "yyyyMMdd"
$path = "D:AdhochBackupsUntil$today"
if (Test-Path $path) {
Remove-Item -Path $path -Recurse -Force
}








--- Backup folder future 7 days

# Get date after 7 days in yyyyMMdd format
$futureDate = (Get-Date).AddDays(7).ToString("yyyyMMdd")

# Build folder path
$folderPath = "D:AdhochBackupsUntil$futureDate"

# Print to verify
Write-Output "Creating folder: $folderPath"

# Create the folder if it doesn't exist
if (-not (Test-Path $folderPath)) {
New-Item -ItemType Directory -Path $folderPath | Out-Null
Write-Output "Folder created successfully."
}
else {
Write-Output "Folder already exists."
}


------Cleanup folder

$today = Get-Date -Format "yyyyMMdd"
$path = "D:AdhochBackupsUntil$today"
if (Test-Path $path) {
Remove-Item -Path $path -Recurse -Force
}

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating