-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatten.ps1
80 lines (69 loc) · 2.26 KB
/
flatten.ps1
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
### CONFIG
param (
[parameter(mandatory)][string]$path,
[switch]$whatif
)
if (!(Test-Path -LiteralPath $path)) {
Write-Output "Source not available."
Exit
}
$folders = @()
$filesToMove = @()
$filesToDelete = @()
$folders += Get-ChildItem -LiteralPath $path -Directory -Recurse
$filesToMove += $folders | ForEach-Object {Get-ChildItem -LiteralPath $_.FullName | Where-Object { ! $_.PSIsContainer }}
foreach ($file in $filesToMove)
{
$destination = Join-Path $path $file.Name
# Check if file already exists
$num=1
while ((Test-Path -LiteralPath $destination) -and (Test-Path -LiteralPath $file.FullName)) {
# Compare the files if they are the same
if ((Get-FileHash $destination).Hash -eq (Get-FileHash $file.FullName).Hash) {
$filesToDelete += $file
break
} else {
# If they are not the same add a suffix to it
$destination = Join-Path $path ($file.BaseName + "_$num" + $file.Extension)
$num+=1
}
}
if (Test-Path -LiteralPath $file.FullName) {
if ($whatif -eq $true) {
$movedFile = Move-Item -LiteralPath $file.FullName -Destination $destination -passThru -whatif
} else {
$movedFile = Move-Item -LiteralPath $file.FullName -Destination $destination -passThru
}
}
Write-Output $movedFile
}
foreach ($file in $filesToDelete)
{
if (Test-Path -LiteralPath $file.FullName) {
if ($whatif -eq $true) {
$deletedFile = Remove-Item -LiteralPath $file.FullName -Force -WhatIf
} else {
$deletedFile = Remove-Item -LiteralPath $file.FullName -Force
}
}
Write-Output $deletedFile
}
# Delete all empty folders
$foldersToDelete = @()
foreach ($folder in (Get-ChildItem -LiteralPath $path -Recurse `
| Where-Object { $_.PSIsContainer }))
{
$foldersToDelete += New-Object PSObject -Property @{
Object = $folder
Depth = ($folder.FullName -split "\\").count
}
}
$foldersToDelete = $foldersToDelete | Sort-Object Depth -Descending
foreach ($folder in $foldersToDelete)
{
If ($folder.Object.GetFileSystemInfos().count -eq 0)
{
$deletedFolder = Remove-Item -LiteralPath $folder.Object.FullName
}
Write-Output $deletedFolder
}