# PackToRar.ps1
# This script compresses files and folders into [Name]_DD_MM_YYYY__HH_mm_ss.rar.
# It is designed to run from Windows Context Menu (Right-click) or SendTo shortcut.

param (
    [string[]]$Paths
)

$script:notifications = @()

# Function to show tray notifications
function Show-Notification {
    param (
        [string]$Title,
        [string]$Message
    )
    try {
        [void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
        [void] [System.Reflection.Assembly]::LoadWithPartialName('System.Drawing')
        $notification = New-Object System.Windows.Forms.NotifyIcon
        $notification.Icon = [System.Drawing.SystemIcons]::Information
        $notification.BalloonTipIcon = "Info"
        $notification.BalloonTipTitle = $Title
        $notification.BalloonTipText = $Message
        $notification.Visible = $true
        $notification.ShowBalloonTip(5000)
        $script:notifications += $notification
    } catch {
        Write-Host "[$Title] $Message"
    }
}

# 1. Locate WinRAR (Rar.exe)
$rarPath = "C:\Program Files\WinRAR\Rar.exe"
if (-not (Test-Path $rarPath)) {
    $rarPath = "C:\Program Files (x86)\WinRAR\Rar.exe"
}
if (-not (Test-Path $rarPath)) {
    # Check registry app paths
    $regPath = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe" -ErrorAction SilentlyContinue
    if ($regPath) {
        $rarPath = Join-Path (Split-Path $regPath.Path) "Rar.exe"
    }
}

if (-not (Test-Path $rarPath)) {
    [void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
    [System.Windows.Forms.MessageBox]::Show(
        "WinRAR (Rar.exe) bulunamadi. Lutfen WinRAR programinin sisteminizde yuklu oldugundan emin olun.",
        "WinRAR Bulunamadi",
        [System.Windows.Forms.MessageBoxButtons]::OK,
        [System.Windows.Forms.MessageBoxIcon]::Error
    )
    exit
}

# If no paths were provided
if (-not $Paths -or $Paths.Count -eq 0) {
    Show-Notification "Hata" "Paketlenecek dosya veya dizin secilmedi."
    exit
}

# 2. Process each path
foreach ($path in $Paths) {
    if (-not (Test-Path $path)) {
        continue
    }

    $item = Get-Item $path
    
    # Resolve the parent directory
    $parentDir = Split-Path -Parent $path
    if (-not $parentDir) {
        $parentDir = $item.Parent.FullName
    }
    if (-not $parentDir) {
        $parentDir = $PWD
    }

    # Extract base name
    if ($item.PSIsContainer) {
        $baseName = $item.Name
    } else {
        $baseName = $item.BaseName
    }

    # Format current Date & Time (DD_MM_YYYY__HH_mm_ss)
    # E.g., 22_07_2026__22_45_30
    $timestamp = Get-Date -Format "dd_MM_yyyy__HH_mm_ss"
    
    # Destination RAR file path
    $rarFileName = "${baseName}_${timestamp}.rar"
    $rarFullPath = Join-Path $parentDir $rarFileName

    # Run WinRAR Rar.exe
    # -ep1: excludes base folder path from archive to avoid absolute path nesting
    # -r: recurses subfolders
    # -y: automatically answers yes to any prompts
    $psi = New-Object System.Diagnostics.ProcessStartInfo
    $psi.FileName = $rarPath
    $psi.Arguments = "a -ep1 -r -y `"$rarFullPath`" `"$path`""
    $psi.CreateNoWindow = $true
    $psi.UseShellExecute = $false
    $psi.RedirectStandardOutput = $true
    $psi.RedirectStandardError = $true

    $process = [System.Diagnostics.Process]::Start($psi)
    $process.WaitForExit()

    if ($process.ExitCode -eq 0) {
        Show-Notification "Paketleme Basarili" "Arsiv olusturuldu:`n$rarFileName"
    } else {
        $errorMsg = $process.StandardError.ReadToEnd()
        if (-not $errorMsg) {
            $errorMsg = $process.StandardOutput.ReadToEnd()
        }
        [void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
        [System.Windows.Forms.MessageBox]::Show(
            "Paketleme sirasinda bir hata olustu (Hata Kodu: $($process.ExitCode)).`n`nDetay:`n$errorMsg",
            "Paketleme Hatasi",
            [System.Windows.Forms.MessageBoxButtons]::OK,
            [System.Windows.Forms.MessageBoxIcon]::Error
        )
    }
}

# Clean up notification icons after a brief display period
if ($script:notifications.Count -gt 0) {
    Start-Sleep -Seconds 4
    foreach ($notification in $script:notifications) {
        $notification.Visible = $false
        $notification.Dispose()
    }
}
AG 2 dakikada yazdı hocam. Antigravity CLI.

Denedim ve kusursuz çalışıyor.