-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshove.ps1
158 lines (121 loc) · 4.11 KB
/
shove.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<#
.synopsis
Group files from specified folder by bunch size into subfolders
.description
New subfolders created and each subfolder will contain approx `MaxSubFolderSize` in sum.
*_NOTE_* that you must specify max size in bytes or use `Kb/Mb/GB/..` notation.
Numeration of files starts from 01. You may restart numeration in each subfolder with
use `SubfolderCounters` switch.
*_NOTE_* before you'll actually Move/Copy files try to use `WhatIf` switch to viw
how the Shove will distribute and rename your files
Also you can gather files from subfolders using `Recurse` switch even you had used
the Shove earlier
.example
-- Example 1 --
# Copy files by bunches about 80mb into subfolders
# and rename files by ordinal number
# with restarting numeration in each subfolder
`shove -Max 315mb -N -S -C`
-- Example 2 --
# Show how SHOVE will MOVE files by banches about 100mb
# -WhatIf can help to view how files will be distributed
# by subfolders and tells about additional actions
`shove . *.mp3 -r -max 205mb -N -K -WhatIf`
# OR
`shove . *.mp3 -r -max 205mb -n -k -j`
#>
[CmdletBinding(
# ConfirmImpact = 'Medium',
SupportsShouldProcess = $true
)]
param (
# Folder where to look for files
[Parameter (Position = 0)]
[String] $SrcDir = $PWD,
# Target folder where subfolders will be created
[String] $TargetDir = $SrcDir,
# Mask of files which process to
[Parameter (Position = 1)]
[String] $Mask = '*',
# Max size of bunch, i.e. maximum size of files in each subfolder
# Note: real size of bunches may overweiht this value, to preview
# exact sizes use -WhatIf first
[Alias('Max')]
[int] $MaxSubFolderSize = 250Mb,
# COPY files instead of MOVE
[Alias('c')]
[Switch] $Copy,
# Use autonumeration for new names of files.
[Alias('N')]
[Switch] $Numeration,
# SHOVE will restart numerations in each subfolder.
[Alias('S')]
[Switch] $SubfolderCounters,
# Just calculate repositions and renames
# Almost like to use `-WhatIf` but less verbose
[Alias('J')]
[Switch] $JustCalc,
# This is It!
[Alias('R')]
[Switch] $Recurse,
# Find and Remove all Empty subfolders
[Alias('K')]
[Switch] $KillEmpty
)
# if ($PSCmdlet.ShouldProcess((Join-Path $PSScriptRoot 'shove-deps.psm1'), 'Load dependancies')) {
# Import-Module (Join-Path $PSScriptRoot 'shove-deps.psm1') -Force
# }
& '.\shove-deps.ps1'
# разделитель с указанием размера
filter div($sz) {
"$(hr)`e[33m {0:n1}`e[36mM`e[0m" -f ($sz / 1Mb)
}
hr
"Source `e[33m$SrcDir`e[0m"
hr
$FileList = Get-ChildItem $SrcDir -Filter $Mask -File -Recurse:$Recurse
$CntFls = 1;
$CntDir = 1;
$SzAcc = 0;
$TemplateSubFName = '{0:d3}';
foreach($File in $FileList) {
$RelativeFileName = Resolve-Path -Relative $File.FullName
Write-Debug "File - $RelativeFileName"
if(($SzAcc + $File.Length) -gt $MaxSubFolderSize){
div $SzAcc
$CntDir++
$SzAcc = 0
if ($SubfolderCounters) {
$CntFls = 1
}
};
$NewName = $Numeration ? "{0:d2}{1}" -f $CntFls,$File.Extension : $File.Name
$TargetFileName = ( Join-Path -Path $TargetDir -ChildPath ("$TemplateSubFName\{1}" -f $CntDir,$NewName) )
$DestDir =($TemplateSubFName -f $CntDir)
$Destination = (Join-Path $TargetDir $DestDir)
$SzAcc += $File.Length
$CntFls++
"{0} `e[35m`u{f553}`e[33m {1}`t`e[36m{2,7:n1}`e[0m" -f $RelativeFileName,$TargetFileName,($File.Length / 1Mb)
if (-not $JustCalc -and -not (Test-Path $Destination) -and $PSCmdlet.ShouldProcess($DestDir, 'Create subfolder')) {
New-Item -Path $Destination -ItemType "directory" > $null
}
if(
-not $JustCalc -and $PSCmdlet.ShouldProcess(
($RelativeFileName),
"$( $Copy ? 'Copy' : 'Move' ) file to `e[36m$DestDir`e[0m\`e[96m$($File.Name)`e[0m"
)
) {
$Params = @{
Path = $File.FullName;
Destination = $TargetFileName;
Force = $true;
Confirm = $false
}
if (!$Copy) {Move-Item @Params} else {Copy-Item @Params}
}
}
div $SzAcc
if ($KillEmpty) {
Remove-EmptySubfolders $Path -JustCalc:$JustCalc
}
Show-FolderSizes $SrcDir | Format-Table