PS Ortamında Klasör/Dosya Kopyalama İşlemleri

Programlama ve Script dilleri konusunda bilgi paylaşım alanıdır.
Cevapla
Kullanıcı avatarı
TRWE_2012
Zettabyte1
Zettabyte1
Mesajlar: 15148
Kayıt: 25 Eyl 2013, 13:38
cinsiyet: Erkek
Teşekkür etti: 2505 kez
Teşekkür edildi: 5301 kez

PS Ortamında Klasör/Dosya Kopyalama İşlemleri

Mesaj gönderen TRWE_2012 »

Merhaba

KOPYALAMA BOYUTU : 50-100GB altında ise bu basit PS betiğini kullanın : Win_PS_Folder_Copy Manager v1.0.ps1

Kod: Tümünü seç

Add-Type -AssemblyName System.Windows.Forms

# Kaynak seçimi
$choice = Read-Host "Kaynak için [1] Yol gir, [2] Pencereden seç"

if ($choice -eq "1") {
    $source = Read-Host "Kaynak dizin veya dosyanın tam yolunu giriniz"
    if (Test-Path $source -PathType Container) { $isFolder = $true } else { $isFolder = $false }
}
elseif ($choice -eq "2") {
    $ofd = New-Object System.Windows.Forms.OpenFileDialog
    $ofd.Title = "Dosya veya klasör seçiniz"
    $ofd.CheckFileExists = $false
    $ofd.CheckPathExists = $true
    $ofd.ValidateNames = $false
    $ofd.FileName = "Select" # Klasör seçimi için
    if ($ofd.ShowDialog() -eq "OK") {
        $selectedPath = $ofd.FileName
        if (Test-Path $selectedPath -PathType Container) {
            $source = $selectedPath
            $isFolder = $true
        } else {
            $source = $selectedPath
            $isFolder = $false
            Write-Host "Dosya seçildi: $source"
        }
    } else { Write-Host "Seçim iptal edildi."; exit }
}
else { Write-Host "Geçersiz seçim."; exit }

# Hedef dizin
$destination = Read-Host "Hedef dizinin tam yolunu giriniz"
if (!(Test-Path -Path $destination)) {
    New-Item -ItemType Directory -Path $destination | Out-Null
    Write-Host "Hedef dizin oluşturuldu: $destination"
}

# Dosya listesi
if ($isFolder) {
    $files = Get-ChildItem -Path $source -Recurse -File
} else {
    $files = @()
    $files += Get-Item -Path $source
}

# Toplam boyut ve değişkenler
$totalSize = ($files | Measure-Object Length -Sum).Sum
$copiedSize = 0
$startTime = Get-Date
$lastPercent = -1

Write-Host "Kopyalama işlemi başlatılıyor..."

# Kopyalama ve % ilerleme
foreach ($file in $files) {
    if ($isFolder) {
        $relativePath = $file.FullName.Substring($source.Length)
        $targetFile = Join-Path $destination $relativePath
        $targetDir = Split-Path $targetFile
        if (!(Test-Path $targetDir)) { New-Item -ItemType Directory -Path $targetDir | Out-Null }
    } else {
        $targetFile = Join-Path $destination $file.Name
    }

    Copy-Item $file.FullName -Destination $targetFile -Force

    $copiedSize += $file.Length
    $percent = [math]::Floor(($copiedSize / $totalSize) * 100)
    if (($percent -ge ($lastPercent + 5)) -and ($percent % 5 -eq 0)) {
        Write-Host "$percent % kopyalandı..."
        $lastPercent = $percent
    }
}

# Ortalama hız
$endTime = Get-Date
$duration = ($endTime - $startTime).TotalSeconds
$mbPerSec = [math]::Round(($totalSize/1MB)/$duration,2)
Write-Host "Kopyalama tamamlandı. Ortalama hız: $mbPerSec MB/sn"

# Hash doğrulama
Write-Host "Hash doğrulaması yapılıyor..."
$errors = 0
foreach ($file in $files) {
    if ($isFolder) {
        $relativePath = $file.FullName.Substring($source.Length)
        $targetFile = Join-Path $destination $relativePath
    } else {
        $targetFile = Join-Path $destination $file.Name
    }

    if (Test-Path $targetFile) {
        $srcHash = (Get-FileHash $file.FullName -Algorithm SHA256).Hash
        $dstHash = (Get-FileHash $targetFile -Algorithm SHA256).Hash
        if ($srcHash -ne $dstHash) {
            Write-Host "UYARI: Hash uyuşmadı -> $file"
            $errors++
        }
    } else {
        Write-Host "UYARI: Eksik dosya -> $file"
        $errors++
    }
}

if ($errors -eq 0) { Write-Host "Doğrulama başarılı. Tüm dosyalar sorunsuz kopyalandı." }
else { Write-Host "$errors dosyada hata bulundu!" }

Read-Host "Çıkmak için Enter'a bas"
KOPYALAMA BOYUTU : 50-100GB'TAN BÜYÜK İSE bu PRO Sürümü (içinde windows ms-dos robocopy.exe var) ps betiğini kullanın : WinRoboCopyPS Manager v1.0.ps1

Kod: Tümünü seç

Add-Type -AssemblyName System.Windows.Forms

# -------------------------
# Kaynak Seçimi
# -------------------------
Write-Host "================================================"
$choice = Read-Host "Kaynak için [1] Yol gir, [2] Pencereden seç"
Write-Host "================================================"

if ($choice -eq "1") {
    $source = Read-Host "Kaynak dizin veya dosyanın tam yolunu giriniz"
    if (!(Test-Path $source)) { Write-Host "Geçersiz kaynak yolu."; exit }
    if (Test-Path $source -PathType Container) { 
        $isFolder = $true
        if ($source[-1] -ne '\') { $source += '\' }
    } else { $isFolder = $false }
} elseif ($choice -eq "2") {
    $ofd = New-Object System.Windows.Forms.OpenFileDialog
    $ofd.Title = "Dosya veya klasör seçiniz"
    $ofd.CheckFileExists = $false
    $ofd.CheckPathExists = $true
    $ofd.ValidateNames = $false
    $ofd.FileName = "Select"
    if ($ofd.ShowDialog() -eq "OK") {
        $selectedPath = $ofd.FileName
        if (Test-Path $selectedPath -PathType Container) {
            $source = $selectedPath
            $isFolder = $true
            if ($source[-1] -ne '\') { $source += '\' }
        } else {
            $source = $selectedPath
            $isFolder = $false
            Write-Host "Dosya seçildi: $source"
        }
    } else { Write-Host "Seçim iptal edildi."; exit }
} else { Write-Host "Geçersiz seçim."; exit }

# -------------------------
# Hedef Seçimi
# -------------------------
Write-Host "================================================"
$choiceDest = Read-Host "Hedef için [1] Yol gir, [2] Pencereden seç"
Write-Host "================================================"

if ($choiceDest -eq "1") {
    $destination = Read-Host "Hedef dizinin tam yolunu giriniz"
    if (!(Test-Path -Path $destination)) { New-Item -ItemType Directory -Path $destination | Out-Null }
    Write-Host "Hedef dizin hazır: $destination"
} elseif ($choiceDest -eq "2") {
    $fbd = New-Object System.Windows.Forms.FolderBrowserDialog
    $fbd.Description = "Hedef dizini seçiniz"
    if ($fbd.ShowDialog() -eq "OK") {
        $destination = $fbd.SelectedPath
        if (!(Test-Path -Path $destination)) { New-Item -ItemType Directory -Path $destination | Out-Null }
        Write-Host "Hedef dizin hazır: $destination"
    } else { Write-Host "Seçim iptal edildi."; exit }
} else { Write-Host "Geçersiz seçim."; exit }

Write-Host "================================================"
Write-Host "**************************************"
Write-Host "İşlem Sonuç Bilgilendirmesi."
Write-Host "*************************************"

# -------------------------
# Kopyalama
# -------------------------
if ($isFolder) {
    Write-Host "Klasör seçildi, Robocopy ile kopyalama başlatılıyor..."
    
    # Kaynak ve hedef yollarını tırnak içine al
    $sourceQuoted = '"' + $source.TrimEnd('\') + '"'
    $destinationQuoted = '"' + $destination.TrimEnd('\') + '"'

    $robocopyCmd = @(
        $sourceQuoted,
        $destinationQuoted,
        "/E","/Z","/J","/R:3","/W:5","/TEE","/NP"
    )
    $startTime = Get-Date
    & robocopy @robocopyCmd
    $endTime = Get-Date
    $duration = ($endTime - $startTime).TotalSeconds
    Write-Host "Kopyalama tamamlandı. Süre: $([math]::Round($duration,2)) saniye."
} else {
    Write-Host "Tek dosya seçildi, blok bazlı kopyalama başlatılıyor..."
    $bufferSize = 10MB
    $fileInfo = Get-Item $source
    $totalBytes = $fileInfo.Length
    $destinationFile = Join-Path $destination $fileInfo.Name

    $fsSource = [System.IO.File]::OpenRead($source)
    $fsDest = [System.IO.File]::OpenWrite($destinationFile)

    $bytesCopied = 0
    $lastPercent = 0
    $startTime = Get-Date

    while ($bytesCopied -lt $totalBytes) {
        $remaining = $totalBytes - $bytesCopied
        $readSize = [math]::Min($bufferSize, $remaining)
        $buffer = New-Object byte[] $readSize
        $read = $fsSource.Read($buffer, 0, $readSize)
        $fsDest.Write($buffer, 0, $read)
        $bytesCopied += $read

        $percent = [math]::Floor(($bytesCopied / $totalBytes) * 100 / 5) * 5
        if ($percent -gt $lastPercent) {
            Write-Host "$percent % kopyalandı..."
            $lastPercent = $percent
        }
    }

    $fsSource.Close()
    $fsDest.Close()
    $endTime = Get-Date
    $duration = ($endTime - $startTime).TotalSeconds
    $speed = [math]::Round(($totalBytes / 1MB)/$duration,2)
    Write-Host "Kopyalama tamamlandı. Süre: $([math]::Round($duration,2)) saniye, Ortalama hız: $speed MB/sn"
}

# -------------------------
# Hash Doğrulama
# -------------------------
$hashCheck = Read-Host "Hash doğrulaması yapmak istiyor musunuz? [E/H]"
if ($hashCheck -eq "E" -or $hashCheck -eq "e") {
    Write-Host "Hash doğrulaması başlatılıyor..."
    if ($isFolder) { $files = Get-ChildItem -Path $source -Recurse -File }
    else { $files = @(Get-Item -Path $source) }

    $errors = 0
    foreach ($file in $files) {
        if ($isFolder) { $relativePath = $file.FullName.Substring($source.Length); $targetFile = Join-Path $destination $relativePath }
        else { $targetFile = Join-Path $destination $file.Name }

        if (Test-Path $targetFile) {
            $srcHash = (Get-FileHash $file.FullName -Algorithm SHA256).Hash
            $dstHash = (Get-FileHash $targetFile -Algorithm SHA256).Hash
            if ($srcHash -ne $dstHash) { Write-Host "UYARI: Hash uyuşmadı -> $file"; $errors++ }
        } else { Write-Host "UYARI: Eksik dosya -> $file"; $errors++ }
    }

    if ($errors -eq 0) { Write-Host "Doğrulama başarılı. Tüm dosyalar sorunsuz kopyalandı." }
    else { Write-Host "$errors dosyada hata bulundu!" }
}

Read-Host "Çıkmak için Enter'a bas"
Kodları YZ'ye okuturabilir(bu betik ne işe yarar sorusunun cevabı), kendisinden teorik çıktı üretmesini isteyebilirsiniz.Ben kendi sistemimde denedim birebir.SSD üzerinde 10GB bir yazma TWB 'si yaptı.Yani SSD'imi biraz yordum.Ondan dolayı ekran görüntü vermeyeceğim.Hataları tek tek giderildi,her iki betiğin...(ben kendime sonuna kadar güveniyorum çünkü gece yarısından beridir uğraşıyorum sonunda oldu-İsteyen kullansın isteyen kullanmasın ama ben kullanacağım)

Güle güle kullanın.

NOT:

YZ modelleri :

https://chatgpt.com
https://duckduckgo.com/?q=DuckDuckGo+AI ... atb=v496-1
Kullanıcı avatarı
TRWE_2012
Zettabyte1
Zettabyte1
Mesajlar: 15148
Kayıt: 25 Eyl 2013, 13:38
cinsiyet: Erkek
Teşekkür etti: 2505 kez
Teşekkür edildi: 5301 kez

Re: PS Ortamında Klasör/Dosya Kopyalama İşlemleri

Mesaj gönderen TRWE_2012 »

Resim
Cevapla

“Programlama ve Script dilleri” sayfasına dön