-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistributeFilesIntoFolders.ps1
More file actions
93 lines (77 loc) · 3.3 KB
/
Copy pathDistributeFilesIntoFolders.ps1
File metadata and controls
93 lines (77 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<#
.SYNOPSIS
Organisiert Dateien in einem Verzeichnis, indem es sie in Unterordner basierend auf einer festgelegten Anzahl sortiert.
.DESCRIPTION
Dieses Skript nimmt einen Startpfad und eine Dateianzahl entgegen. Es sortiert Dateien im Startpfad in neu erstellte Unterordner, wobei jeder Unterordner eine spezifische Anzahl von Dateien enthält. Dateinamen werden vor dem Verschieben sanitisiert, um ungültige Zeichen zu entfernen.
.PARAMETER startPath
Der Pfad, von dem aus die Dateien organisiert werden sollen. Standardmäßig auf "i:\" gesetzt.
.PARAMETER filesCount
Die maximale Anzahl von Dateien pro Unterordner. Standardmäßig auf 150 gesetzt.
.EXAMPLE
PS> .\DistributeFilesIntoFolders.ps1 -startPath "i:\" -filesCount 150
.NOTES
Autor: Erhard Rainer
Datum: 2023-12-10
Version: 1.0
#>
param (
[string]$startPath = "i:\",
[int]$filesCount = 150
)
function Sanitize-FileName($fileName) {
# Entfernt ungültige Zeichen aus Dateinamen
$invalidChars = [System.IO.Path]::GetInvalidFileNameChars() -join ''
$re = "[{0}]" -f [RegEx]::Escape($invalidChars)
return $fileName -replace $re, ''
}
# Prüft, ob der angegebene Startpfad existiert
if (-not (Test-Path -Path $startPath)) {
Write-Error "Der angegebene Pfad existiert nicht."
return
}
# Holt alle Dateien im Startpfad und sortiert sie nach dem Erstellungsdatum
Write-Host "Scanne das Verzeichnis: $startPath"
$allFiles = Get-ChildItem -Path $startPath -File | Sort-Object CreationTime
# Erstellt eine Liste der tatsächlich vorhandenen Dateien
$existingFiles = @()
foreach ($file in $allFiles) {
if (Test-Path -Path $file.FullName) {
$existingFiles += $file
} else {
Write-Warning "Datei $($file.FullName) wurde nicht gefunden und wird übersprungen."
}
}
# Erstellt Unterordner und verschiebt Dateien
$folderIndex = 1
while ($existingFiles.Count -gt 0) {
$folderName = "{0:D4}" -f $folderIndex
$folderPath = Join-Path -Path $startPath -ChildPath $folderName
# Prüft, ob der Unterordner existiert und wie viele Dateien darin verschoben werden sollen
if (Test-Path -Path $folderPath) {
$existingFilesInFolder = (Get-ChildItem -Path $folderPath -File).Count
$filesToMove = $filesCount - $existingFilesInFolder
if ($filesToMove -le 0) {
Write-Host "Verzeichnis $folderPath ist bereits voll."
$folderIndex++
continue
}
} else {
New-Item -Path $folderPath -ItemType Directory
$filesToMove = $filesCount
}
Write-Host "Befülle Verzeichnis $folderPath"
$filesToProcess = $existingFiles[0..($filesToMove - 1)]
foreach ($file in $filesToProcess) {
$sanitizedFileName = Sanitize-FileName($file.Name)
$destinationPath = Join-Path -Path $folderPath -ChildPath $sanitizedFileName
# Verschiebt die Datei, falls sie im Zielverzeichnis noch nicht existiert
if (-not (Test-Path -Path $destinationPath)) {
Move-Item -Path $file.FullName -Destination $destinationPath
} else {
Write-Warning "Eine Datei mit dem Namen $sanitizedFileName existiert bereits im Zielverzeichnis und wurde übersprungen."
}
}
# Aktualisiert die Liste der zu verarbeitenden Dateien
$existingFiles = $existingFiles[$filesToMove..$existingFiles.Count]
$folderIndex++
}