Last active
September 9, 2025 08:40
-
-
Save obar1/0b67cc25a7506a8f0876dfa3c18c07bf to your computer and use it in GitHub Desktop.
Creates a dated subfolder in the current directory, adds a readme.md file if missing, and compresses the entire folder into a ZIP backup. Includes error handling for the backup process.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Creates a dated subfolder in the current directory, adds a readme.md file if missing, and compresses the entire folder into a ZIP backup. Includes error handling for the backup process. | |
$currentDate = (Get-Date).ToString("yyMMdd") | |
# TODO: check backupFilePath | |
$backupFilePath = "$env:USERPROFILE\.bkp\$currentDate.zip" | |
$currentDirectory = Get-Location | |
$datedSubfolderPath = Join-Path -Path $currentDirectory -ChildPath $currentDate | |
$readmeFilePath = Join-Path -Path $datedSubfolderPath -ChildPath "readme.md" | |
# Create dated subfolder if it doesn't exist | |
if (-not (Test-Path -Path $datedSubfolderPath)) { | |
New-Item -Path $datedSubfolderPath -ItemType Directory | Out-Null | |
} | |
# Create readme.md if it doesn't exist | |
if (-not (Test-Path -Path $readmeFilePath)) { | |
New-Item -Path $readmeFilePath -ItemType File | Out-Null | |
"# readme of $currentDate" | Out-File -FilePath $readmeFilePath -Encoding UTF8 | |
Write-Output "File readme created in $currentDate with actual date." | |
# Create backup using Windows built-in ZIP utility | |
try { | |
# Attempt to create the backup | |
Compress-Archive -Path "$currentDirectory\*" -DestinationPath $backupFilePath -ErrorAction Stop | |
Write-Output "Backup created: $backupFilePath from $currentDirectory" | |
} catch { | |
Write-Output "Backup failed: $($_.Exception.Message)" | |
} | |
} else { | |
Write-Output "Readme skipped, already exists in $datedSubfolderPath" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment