first commit
This commit is contained in:
118
BackupFolder.ps1
Normal file
118
BackupFolder.ps1
Normal file
@@ -0,0 +1,118 @@
|
||||
# BackupFolder.ps1
|
||||
# Script to backup and compress folders from Windows context menu
|
||||
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$FolderPath
|
||||
)
|
||||
|
||||
# Function to show message
|
||||
function Show-Message {
|
||||
param(
|
||||
[string]$Message,
|
||||
[string]$Title = "Klasör Yedekleme",
|
||||
[int]$Type = 0 # 0=Info, 1=Error, 2=Warning
|
||||
)
|
||||
|
||||
switch ($Type) {
|
||||
0 { $icon = 64 } # Information
|
||||
1 { $icon = 16 } # Error
|
||||
2 { $icon = 48 } # Warning
|
||||
default { $icon = 64 }
|
||||
}
|
||||
|
||||
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
|
||||
[System.Windows.Forms.MessageBox]::Show($Message, $Title, 0, $icon) | Out-Null
|
||||
}
|
||||
|
||||
# Check if folder exists
|
||||
if (-not (Test-Path $FolderPath)) {
|
||||
Show-Message -Message "Belirtilen klasör mevcut deðil: $FolderPath" -Type 1
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Get folder name
|
||||
$FolderName = Split-Path $FolderPath -Leaf
|
||||
|
||||
# Get current date in YYYYMMDD format
|
||||
$Date = Get-Date -Format "yyyyMMdd"
|
||||
|
||||
# Determine output directory (same as source folder)
|
||||
$OutputDir = Split-Path $FolderPath -Parent
|
||||
|
||||
# Find existing backups with same name and date to determine next number
|
||||
$Pattern = "^${FolderName}_${Date}_\d{3}\.(rar|zip)$"
|
||||
$ExistingBackups = Get-ChildItem -Path $OutputDir -Filter "${FolderName}_${Date}_*" | Where-Object { $_.Name -match $Pattern }
|
||||
|
||||
$NextNumber = 1
|
||||
if ($ExistingBackups) {
|
||||
$Numbers = $ExistingBackups.Name | ForEach-Object {
|
||||
if ($_ -match "_([0-9]{3})\.(rar|zip)$") { [int]$matches[1] }
|
||||
} | Sort-Object
|
||||
if ($Numbers) {
|
||||
$NextNumber = ($Numbers | Select-Object -Last 1) + 1
|
||||
}
|
||||
}
|
||||
|
||||
$NumberFormatted = "{0:D3}" -f $NextNumber
|
||||
|
||||
# Try WinRAR first
|
||||
$WinRarPath = "${env:ProgramFiles}\WinRAR\rar.exe"
|
||||
$UseWinRar = Test-Path $WinRarPath
|
||||
|
||||
# If WinRAR not found, try 7-Zip
|
||||
$SevenZipPath = "${env:ProgramFiles}\7-Zip\7z.exe"
|
||||
$UseSevenZip = Test-Path $SevenZipPath
|
||||
|
||||
# If neither found, use PowerShell Compress-Archive
|
||||
$UsePowerShellArchive = -not ($UseWinRar -or $UseSevenZip)
|
||||
|
||||
# Create backup filename
|
||||
if ($UseWinRar) {
|
||||
$OutputFile = Join-Path $OutputDir "${FolderName}_${Date}_${NumberFormatted}.rar"
|
||||
} else {
|
||||
$OutputFile = Join-Path $OutputDir "${FolderName}_${Date}_${NumberFormatted}.zip"
|
||||
}
|
||||
|
||||
try {
|
||||
if ($UseWinRar) {
|
||||
# Use WinRAR
|
||||
# Change to the parent directory and use relative path to avoid full path in archive
|
||||
Push-Location $OutputDir
|
||||
try {
|
||||
$Arguments = "a -r `"$OutputFile`" `".\$FolderName`""
|
||||
Start-Process -FilePath $WinRarPath -ArgumentList $Arguments -Wait -NoNewWindow
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
}
|
||||
}
|
||||
elseif ($UseSevenZip) {
|
||||
# Use 7-Zip
|
||||
# Change to the parent directory and use relative path to avoid full path in archive
|
||||
Push-Location $OutputDir
|
||||
try {
|
||||
$Arguments = "a `"$OutputFile`" `".\$FolderName`""
|
||||
Start-Process -FilePath $SevenZipPath -ArgumentList $Arguments -Wait -NoNewWindow
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
}
|
||||
}
|
||||
else {
|
||||
# Use PowerShell built-in Compress-Archive
|
||||
# Fix the path issue by changing to the parent directory and using relative path
|
||||
Push-Location $OutputDir
|
||||
try {
|
||||
Compress-Archive -Path ".\$FolderName" -DestinationPath $OutputFile -Force
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
}
|
||||
}
|
||||
|
||||
Show-Message -Message "Yedekleme baþarýyla tamamlandý!`n`nDosya: $OutputFile"
|
||||
}
|
||||
catch {
|
||||
Show-Message -Message "Yedekleme sýrasýnda hata oluþtu:`n$($_.Exception.Message)" -Type 1
|
||||
}
|
||||
Reference in New Issue
Block a user