-
Notifications
You must be signed in to change notification settings - Fork 3
/
ordinaps.ps1
79 lines (71 loc) · 2 KB
/
ordinaps.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
# OrdinaPS, a Windows Powershell script
# It sorts your files each in a folder named as its extension
#
# v0.3.1
#
# Made by giuscond
# https://giuscond.com
#
# store all filenames in the folder in an array
$file_list=Get-ChildItem $pwd
# set initial state for blacklist variable
$black=0
# store script path wheren config file is located
$scriptpath = Split-Path $MyInvocation.MyCommand.Path
# check if config file exist
if (Test-Path -Path $scriptpath\ordineps.ini -PathType Leaf)
{
# read config data
# exclude [headers] and commented # lines
Get-Content -Path $scriptpath\ordineps.ini | foreach-object -begin {$config=@{}} -process { $k = [regex]::split($_,'='); if(($k[0].CompareTo("") -ne 0) -and ($k[0].StartsWith("[") -ne $True)-and ($k[0].StartsWith("#") -ne $True)) { $config.Add($k[0], $k[1]) } }
# extract blacklist
$blacklist=$config.Get_Item("blacklist")
# convert blacklist in array
$blacklist=$blacklist.Split(",")
}
# iterate through the array
ForEach ($f in $file_list)
{
# check if f is a file or directory
# proceed if it's a file
if (Test-Path -LiteralPath $f -PathType Leaf )
{
# check if file has extension
If ([bool]$f.Extension -eq $True)
{
# get the extension
$ext=$f.Extension -replace '^.'
# check each extension in blacklist config
ForEach ($c in $blacklist)
{
# check if ext is in the blacklist
if ($ext -eq $c)
{
# founded a file with extension blacklisted
$black=1
}
}
# if is not blacklisted, do the job
if ($black -eq 0)
{
# check if there is a custom folder name
$folder=''
if ($config.ContainsKey($ext))
{
# use custom folder name instead extension name
$folder = $config.$ext
}
else
{
# use extension name
$folder = $ext
}
# create the dir with ext as dir name
mkdir -p "$PWD\$folder" -ErrorAction SilentlyContinue
# move the file in that directory
Move-Item -LiteralPath $f "$PWD\$folder" -ErrorAction SilentlyContinue
}
$black=0
}
}
}