-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimeRSSMove.ps1
151 lines (125 loc) · 7.29 KB
/
AnimeRSSMove.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
#================================================================================
# AnimeRSSMove v5.0 by Neo
# AnimeRSSMove.ps1 -OutputPath "<file destination>" -InputFile "<file path>"
# AnimeRSSMove.ps1 -OutputPath "<files destination>" -InputPath "<folder path>"
#================================================================================
### Parameters, Title, Checks, Variables, Script
# Set parameters
Param(
[Parameter(Mandatory=$true, Position=0)]
[string]$OutputPath = "",
[Parameter(Mandatory=$false, Position=1)]
[string]$InputFile = "",
[Parameter(Mandatory=$false, Position=2)]
[string]$InputPath = ""
)
# Set PowerShell title.
$host.ui.RawUI.WindowTitle = "AnimeRSSMove v5.0 by Neo"
# Wait 5 seconds to bypass errors created by other software.
# Start-Sleep -Seconds 5
# Define default variables.
$OutputPath = $OutputPath.TrimStart("'").TrimEnd("'")
$InputFile = $InputFile.TrimStart("'").TrimEnd("'")
$InputPath = $InputPath.TrimStart("'").TrimEnd("'")
$TrueOutputPath = $OutputPath.TrimEnd("\")
# Define error codes and messages variables.
$ErrorInputFileDoesNotExist = "Error code 1: Input file doesn't exist."
$ErrorInputPathDoesNotExist = "Error code 2: Input path doesn't exist."
$ErrorOutputPathDoesNotExist = "Error code 3: Output path doesn't exist."
$ErrorMovingFile = "Error code 4: Error moving file."
# Define log file path variable.
$LogFilePath = "C:\temp\AnimeRSSMove.log"
# Get current date and time.
$DateTime = Get-Date
# Script if InputFile variable exists.
If ($InputFile) {
# Check if input file exists, and write error message to log and exit script if it doesn't.
If (-Not ([System.IO.File]::Exists($InputFile))) {
# Write error message to log file.
Add-Content -Path $LogFilePath -Value "$DateTime - $ErrorInputFileDoesNotExist - $InputFile"
# Write error message to console and exit script.
Write-Error "$DateTime - $ErrorInputFileDoesNotExist - $InputFile" -Action Stop
}
# Check if output path exists, and write error message to log and exit script if it doesn't.
If (-Not (Test-Path $TrueOutputPath)) {
# Write error message to log file.
Add-Content -Path $LogFilePath -Value "$DateTime - $ErrorOutputPathDoesNotExist - $OutputPath"
# Write error message to console and exit script.
Write-Error "$DateTime - $ErrorOutputPathDoesNotExist - $OutputPath" -Action Stop
}
# Script variables.
$InputFileName = Split-Path $InputFile -Leaf
# Welcome to REGEX HELL.
$FolderName = $InputFileName -replace "\d+(\.[a-zA-Z]{3,4})$", "" -replace '(\d+)(\s\[TV\sx264\s10bit\s1080p\sAAC\s2.0\sDual\]).*', '' -replace '( - \d{1,2}(S\d{1,2}E\d{1,2})?$).*', '' -replace '(\d+)(\s\(BD\s1080p\)\.[mkv|avi|mp4]+)', '' -replace '(_\d+v?\d*).*', '' -replace '_', ' ' -replace " - SP\d+", "" -replace '\[.*?\]', '' -replace '\.mkv|\.avi|\.mp4', '' -replace "\.", " " -replace '\(.*?\)', '' -replace '( - \d{1,2}(S\d{1,2}E\d{1,2})?$).*', '' -replace '( - S\d+E\d+| - \d+).*', '' -replace '(S\d+E\d+).*', '' -replace '(.(SP|EP)\d+)*', '' -replace '(e\d+.*)', '' -replace '(\sep\s*\d+).*', '' -replace '( -$)', '' -replace '( - $)', '' -replace '- S\d+E?\b', ''
$FolderName = $FolderName.Trim()
# Replace "SX -" with "Season X -" and remove any 0's in front to circumvent any problems in Plex.
$FolderName = $FolderName -replace 'S0*(\d+)$', 'Season $1'
$FolderPath = $TrueOutputPath + "\" + $FolderName
$FolderPathFileName = $FolderPath + "\" + $InputFileName
# Check if folder path exists, if it doesn't create folder.
If (-Not (Test-Path $FolderPath)) {
New-Item -Path $TrueOutputPath -Name $FolderName -ItemType "directory"
}
# Check if file already exists in the path, if it does delete it.
If ([System.IO.File]::Exists($FolderPathFileName)) {
[System.IO.File]::Delete($FolderPathFileName)
}
# Move input file to output folder and check if move was sucessfully ran.
if (-Not [System.IO.File]::Move($InputFile, $FolderPathFileName)) {
# Write error message to log file.
Add-Content -Path $LogFilePath -Value "$DateTime - $ErrorMovingFile - $InputFile to $FolderPathFileName"
# Write error message to console.
Write-Error "$DateTime - $ErrorMovingFile - $InputFile to $FolderPathFileName" -Action Stop
}
}
# Script if InputPath variable exists.
If ($InputPath) {
# Check if input path exists, and write error message to log and exit script if it doesn't.
If (-Not (Test-Path $InputPath)) {
# Write error message to log file.
Add-Content -Path $LogFilePath -Value "$DateTime - $ErrorInputPathDoesNotExist - $InputPath"
# Write error message to console and exit script.
Write-Error "$DateTime - $ErrorInputPathDoesNotExist - $InputPath" -Action Stop
}
# Check if output path exists, and write error message to log and exit script if it doesn't.
If (-Not (Test-Path $TrueOutputPath)) {
# Write error message to log file.
Add-Content -Path $LogFilePath -Value "$DateTime - $ErrorOutputPathDoesNotExist - $OutputPath"
# Write error message to console and exit script.
Write-Error "$DateTime - $ErrorOutputPathDoesNotExist - $OutputPath" -Action Stop
}
# Get files from InputPath.
$InputPathFiles = Get-ChildItem -File -Path $InputPath -Recurse
# Run ForEach loop on all files in InputPath.
ForEach ($Item in $InputPathFiles) {
# Check if input file exists, if it doesn't exit script.
If (-Not ([System.IO.File]::Exists($Item.FullName))) {
Write-Host $Item "in input path array doesn't exist."
break
}
# Script variables.
$InputFileName = Split-Path $Item -Leaf
# Welcome to REGEX HELL.
$FolderName = $InputFileName -replace "\d+(\.[a-zA-Z]{3,4})$", "" -replace '(\d+)(\s\[TV\sx264\s10bit\s1080p\sAAC\s2.0\sDual\]).*', '' -replace '( - \d{1,2}(S\d{1,2}E\d{1,2})?$).*', '' -replace '(\d+)(\s\(BD\s1080p\)\.[mkv|avi|mp4]+)', '' -replace '(_\d+v?\d*).*', '' -replace '_', ' ' -replace " - SP\d+", "" -replace '\[.*?\]', '' -replace '\.mkv|\.avi|\.mp4', '' -replace "\.", " " -replace '\(.*?\)', '' -replace '( - \d{1,2}(S\d{1,2}E\d{1,2})?$).*', '' -replace '( - S\d+E\d+| - \d+).*', '' -replace '(S\d+E\d+).*', '' -replace '(.(SP|EP)\d+)*', '' -replace '(e\d+.*)', '' -replace '(\sep\s*\d+).*', '' -replace '( -$)', '' -replace '( - $)', '' -replace '- S\d+E?\b', ''
$FolderName = $FolderName.Trim()
# Replace "SX -" with "Season X -" and remove any 0's in front to circumvent any problems in Plex.
$FolderName = $FolderName -replace 'S0*(\d+)$', 'Season $1'
$FolderPath = $TrueOutputPath + "\" + $FolderName
$FolderPathFileName = $FolderPath + "\" + $InputFileName
# Check if folder path exists, if it doesn't create folder.
If (-Not (Test-Path $FolderPath)) {
New-Item -Path $TrueOutputPath -Name $FolderName -ItemType "directory"
}
# Check if file already exists in the path, if it does delete it.
If ([System.IO.File]::Exists($FolderPathFileName)) {
[System.IO.File]::Delete($FolderPathFileName)
}
# Move input file to output folder and check if move was sucessfully ran.
if (-Not [System.IO.File]::Move($InputFile, $FolderPathFileName)) {
# Write error message to log file.
Add-Content -Path $LogFilePath -Value "$DateTime - $ErrorMovingFile - $InputFile to $FolderPathFileName"
# Write error message to console.
Write-Error "$DateTime - $ErrorMovingFile - $InputFile to $FolderPathFileName" -Action Stop
}
}
}