Skip to main content

Windows: Alle Dateien neuer als <Datum> verschieben

 

Aktuellste Dateien verschieben, wenn Nextcloud Desktop Client neu installiert werden soll um Sync-Probleme zu beheben oder auf virtuelle Dateien umzusteigen

$Source = "C:\Users\lbrin\Nextcloud_2025_01_14"
$Target = "C:\Users\lbrin\Nextcloud_2025_01_14_backup"
$Cutoff = [datetime]::ParseExact("01.12.2025","dd.MM.yyyy",$null)

$log = Join-Path $Target ("move-log-" + (Get-Date -Format "yyyyMMdd-HHmmss") + ".txt")
New-Item -ItemType Directory -Path $Target -Force | Out-Null

function Get-FilesSafe {
    param([string]$Root)

    $stack = New-Object System.Collections.Generic.Stack[string]
    $stack.Push($Root)

    while ($stack.Count -gt 0) {
        $dir = $stack.Pop()

        try {
            # Inhalte dieses Ordners lesen (kann fehlschlagen, wenn Nextcloud gerade umräumt)
            $items = Get-ChildItem -LiteralPath $dir -Force -ErrorAction Stop
        }
        catch {
            Add-Content -LiteralPath $log -Value ("SKIP DIR (not found/accessible): " + $dir + " | " + $_.Exception.Message)
            continue
        }

        foreach ($it in $items) {
            if ($it.PSIsContainer) {
                $stack.Push($it.FullName)
            } else {
                $it
            }
        }
    }
}

$files = Get-FilesSafe -Root $Source | Where-Object {
    $_.CreationTime -gt $Cutoff -or $_.LastWriteTime -gt $Cutoff
}

foreach ($f in $files) {
    try {
        $relative = $f.FullName.Substring($Source.TrimEnd('\').Length).TrimStart('\')
        $destPath = Join-Path $Target $relative
        $destDir  = Split-Path -Path $destPath -Parent

        New-Item -ItemType Directory -Path $destDir -Force | Out-Null

        Move-Item -LiteralPath $f.FullName -Destination $destPath -Force -ErrorAction Stop
        Add-Content -LiteralPath $log -Value ("MOVED: " + $f.FullName + " -> " + $destPath)
    }
    catch {
        Add-Content -LiteralPath $log -Value ("FAIL FILE: " + $f.FullName + " | " + $_.Exception.Message)
    }
}

"Fertig. Log: $log"