diff --git a/Animelibk.ps1 b/Animelibk.ps1 index 9ade618..d4778a1 100644 --- a/Animelibk.ps1 +++ b/Animelibk.ps1 @@ -5,6 +5,9 @@ [switch] $Force, [switch] $Sub ) +Import-Module -Name ($PSScriptRoot + "\PathChar.psd1") +Import-Module -Name ($PSScriptRoot + "\NameRegex.psd1") +Edit-PathChar $Path #$TargetName = "俺だけ入れる隠しダンジョン〜こっそり鍛えて世界最強〜"; $TargetName = "" #$DestinName = "真の仲間" @@ -42,8 +45,6 @@ $WorkData $Seasons_TC = "", "第一季", "第二季", "第三季", "第四季", "第五季" $Year = @($($JsonPath -replace '.*\\((?\d{4})?(?\d{2})?).*$', '${Y}'), $(get-date -Format 'yyyy')) | ? {-not([string]::IsNullOrEmpty($_))} | Select -First 1 -#$($JsonPath -replace '.*\\((?\d{4})?(?\d{2})?).*$', '${Y}'); - # 取得目標資料夾是否存在,不存在建立資料夾,並取得資料夾物件 $LinkPath = "{2}\{0}\{1}" -f $WorkData.DestinName, $Seasons_TC[$WorkData.Season], $Index $LinkDir = $(if (-not(Test-Path -Path "$LinkPath*")) { @@ -62,61 +63,29 @@ function Get-StandName { $File = $(if ($FileObj.GetType().Name -eq 'FileInfo') { $FileObj } else { - Get-Item -Path $($FileObj -replace '([\[\]])', '`$1') + Get-Item -Path $(Edit-PathChar $FileObj) }) - + # 因應檔名不同而設定多組正規方式 - $Result = @($( - #$($TestName[-1] -match "\[([^\]]*)\]\[(\d{1,2})\]") - #$($File.Name -match "[\[\s]([^\]]*?)(?:[\]\[\-\s]+)(\d{1,2})[\]\s]") - $($File.Name -match "[\[\s](?\b[^\]]*\b)(?:[\]\[\-\s]+)(?\d{1,2})(?:[vV]\d|FIN|END)?[\]\s]") - #$($TestName[-1] -match "[\[\s](?[^\]]*?)(?:[\]\[\-\s]+)(?\d{1,2})(?:[vV]\d)?[\]\s]") - ) | % { if ($_) { $Matches }}) + $EpisodeRegex = @( + "S\d{2}[\s\-]+E?(?\d{2})" + "[\[\b\s](?\d{2})[\s_]?(?:[vV]\d|END|FIN)[\]\b\s]" + "\[(?\d{1,2})\w{2}\]" + "\[(?\d{1,2})\]" + ) + + $NameRegex = @( + "[\[\b\s](?[^\[\]]*)[\]\b\s]$" + ) - # 檔名分析測試用 -# $Pathes = @( -# "L:\Anime\200910 聖剣の刀鍛冶(聖劍鍛造師)\[DMG][The Sacred Blacksmith][01-12 END][848x480][BIG5]\[DMG][The Sacred Blacksmith][04V2][848x480][BIG5].rmvb" -# "H:\Anime\201901 盾の勇者の成り上がり\[LKSUB][Tate no Yuusha no Nariagari][01-25][GB][720P][MP4]\[LKSUB][Tate no Yuusha no Nariagari][01][GB][720P].mp4" -# "L:\Anime\202004 ストライク・ザ・ブラッド OVA IV\[Suzu-Kaze][Strike_the_Blood_IV_OVA][01-12FIN][BDRip_1080P][HEVC_YUV420P10]\[Suzu-Kaze] Strike the Blood IV 12FIN [BDRip 1920x1080 HEVC YUV420P10 FLAC].mkv" -# "L:\Anime\200910 アスラクライン II(機巧魔神)\[DMG][Asura Cryin2][01-13 END][848x480][BIG5][RMVB]\[DMG][Asura Cryin 2][01][848x480][Big5].rmvb" -# "H:\Data\Downloads\異世界食堂\[Lilith-Raws] Isekai Shokudou S02 - 06 [Baha][WEB-DL][1080p][AVC AAC][CHT][MP4].mp4" -# ) -# $TestName = @($Pathes | % {$_.Split("\\")[-1]}) -# $Result = @( $TestName | % { @( - #$($_ -match "\[([^\]]*)\]\[(\d{1,2})\]") -# $($_ -match "[\[\s](?\b[^\]]*\b)(?:[\]\[\-\s]+)(?\d{1,2})(?:[vV]\d|FIN|END)?[\]\s]") -# ) | % { if ($_) { $Matches }}}) -# Write-Output $Result -# $Matches + $Result = $File.Name | % { Get-NameRegex $_ -Regex $EpisodeRegex } | % { $(Get-NameRegex $_["L"] -Regex $NameRegex -Exclude "(BD|DVD|HDTV)RIP" -End) + $_ } - - # OLD Edition - #$File.Name -match "\[([^\]]*)\]\[(\d{1,2})\]" | Out-Null - #$Matches - #$newName = "{0} S{3:00}E{1}{2}" -f $Matches[1], $Matches[2], $File.Extension, $Season; - #$newName = "{0} S{3:00}E{1}{2}" -f $Result[1], $Result[2], $File.Extension, $WorkData.Season; - # 建立回傳資料物件,以方便後續資料處理。 $OutputData = "" | Select newName, Season, Episode, newFullName; $OutputData.newName = $Result.Name; $OutputData.Season = $WorkData.Season; $OutputData.Episode = [int] $Result.Episode; $OutputData.newFullName = "{0} S{3:00}E{1}{4}{2}" -f $Result.Name, $Result.Episode, $File.Extension, $WorkData.Season, $(if($Sub) {".default"}); - #$OutputData | ConvertTo-Json; - - # Type 2,在輸出顯示上,此型態建立較傾向 Hash,而上面 select 建立的,較偏向 pscustomobject - #$OutputData2 = @{ - # newName = $Result[1]; - # Season = [int] $WorkData.Season; - # Episode = [int] $Result[2]; - # newFullName = "{0} S{3:00}E{1}{2}" -f $Result[1], $Result[2], $File.Extension, $WorkData.Season; - #}; - #$OutputData2 | ConvertTo-Json; - - #Write-Output $File; - #Write-Output $File.Name; - #Write-Output $newName; - #return $newName; return $OutputData; } @@ -144,11 +113,11 @@ $StructPathObj = @{ # 運作模式 if ($Single) { # 單集模式 - $List = @(Get-Item -Path $($Path -replace '([\[\]])', '`$1')) + $List = @(Get-Item -Path $(Edit-PathChar $Path)) $BuildList = $List | select $FileObj, $StructPathObj, $NewNameObj | ? {$_.NewName.Episode -notin $WorkData.Episode} #$BuildList | ? {$_.NewName.Episode -notin $WorkData.Episode} | Format-List } elseif ($Sub) { - $List = @(Get-Item -Path $($Path -replace '([\[\]])', '`$1')) + $List = @(Get-Item -Path $(Edit-PathChar $Path)) $BuildList = $List | select $FileObj, $StructPathObj, $NewNameObj | ? {$_.File.Extension -in @(".ass", ".ssa")} } @@ -156,7 +125,7 @@ $ENDResult = $BuildList | % { $Command = @{ ItemType = 'HardLink' Path = $("{0}\{1}" -f $_.StructPath, $_.NewName.newFullName) - Target = $($_.File.Fullname -replace '([\[\]])', '`$1') + Target = $(Edit-PathChar $_.File.Fullname) } #try { if (New-Item @Command) { @@ -176,8 +145,8 @@ exit 0; # SIG # Begin signature block # MIIFxwYJKoZIhvcNAQcCoIIFuDCCBbQCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR -# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU8mPuUodwDIN2Q4E0vUnnZGUG -# r4GgggNSMIIDTjCCAjagAwIBAgIQdMnAQ4BDAo9Lgfn+QeDdhjANBgkqhkiG9w0B +# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUoj6O9AeGdF5M/FWDj5z18zw0 +# 4JqgggNSMIIDTjCCAjagAwIBAgIQdMnAQ4BDAo9Lgfn+QeDdhjANBgkqhkiG9w0B # AQUFADAqMRMwEQYDVQQDDApOaWNrIENoYW5nMRMwEQYDVQQKDAplYXJpY2sub3Jn # MB4XDTIxMTAzMTEwMDI1MVoXDTIyMTAzMTEwMjI1MVowMzEiMCAGCSqGSIb3DQEJ # ARYTcGNuaWNrMTAwQGdtYWlsLmNvbTENMAsGA1UEAwwETmljazCCASIwDQYJKoZI @@ -198,11 +167,11 @@ exit 0; # AgEBMD4wKjETMBEGA1UEAwwKTmljayBDaGFuZzETMBEGA1UECgwKZWFyaWNrLm9y # ZwIQdMnAQ4BDAo9Lgfn+QeDdhjAJBgUrDgMCGgUAoHgwGAYKKwYBBAGCNwIBDDEK # MAigAoAAoQKAADAZBgkqhkiG9w0BCQMxDAYKKwYBBAGCNwIBBDAcBgorBgEEAYI3 -# AgELMQ4wDAYKKwYBBAGCNwIBFTAjBgkqhkiG9w0BCQQxFgQUs/WMayHdj7voQ+1i -# xWSLfdeejpcwDQYJKoZIhvcNAQEBBQAEggEAsgkEEH0srYtf8UzIuYMmWBaDEa2t -# xC3bcCkuP+FFqln47A8bA3zEMvp69Ox9+bJIzSDsoeT/lEqKxPYt/JS+JgIYg5op -# o1DjvzzTo6rGvpOW/cFTcoaP8tKoXWeRGjAD0lex06tiqMio49wOrsY/m4RGW13c -# tQvdLGJTFWD7ovFAfC14BE7Fl3WMLwljD70DuWistL7fYm3bspj2cJTFjiIDWmML -# xDCx7sVyGBF5krwJB3DefO5Dq006ANCMU94gpSwQzOUFN6ZYvufa8EniaHYUhI+9 -# 6Z+pEHvR+MOwT6LA6F0pe7Vel8O2bGmRTaH8+K9mj6r/JOky1ZcFebKDzg== +# AgELMQ4wDAYKKwYBBAGCNwIBFTAjBgkqhkiG9w0BCQQxFgQUZh3cBrwgHvdFbQwV +# WcdOMvw1legwDQYJKoZIhvcNAQEBBQAEggEAGR0QzX3t1drjorcTh759QopFrvNd +# bb9VtZx5OJsLD8/v4YuNBB0IYT+03OSK7jWyIyH5OEj/jTn/Uj1WG37vu/GDdsmm +# dp/3PWziGclgh0QrJmZjQPavqFXHpUrRCIQqT2J9Er4zMf95zWEu1Zsgpb/DNJqu +# 4gz5d7Yn+wllud0DuOiQDwz9fpSz+lgMhMybqR+wsW8ROLvzCuTHJaNvKffm7vFG +# NjbwZccH9xULGbAakX6H2lbcaMqGlkLYVkP3FRmk2zTG+qR4jxHzYhsttKb2ClSt +# L2qRWv7KJCPnw4RpFx/+L9buDHH25QERiINEOPF/pfpqHhaENTolaUe7vg== # SIG # End signature block diff --git a/NameRegex.psd1 b/NameRegex.psd1 new file mode 100644 index 0000000..c88caf6 Binary files /dev/null and b/NameRegex.psd1 differ diff --git a/NameRegex.psm1 b/NameRegex.psm1 new file mode 100644 index 0000000..4020d82 --- /dev/null +++ b/NameRegex.psm1 @@ -0,0 +1,85 @@ +function Get-NameRegex { + param( + $Str, + $Regex, + $Exclude, + [switch] $End + ) + # 查詢 Regex 字串中定義的 TAG,並將其作為 Scriptblock + $Keyfind = { + process{ + if ($_ -match "\(\?\<(.*?)\>") { + $Matches[1] + } else { + Write-Error "$_ 沒有定義(?)" + } + } + } + + # 將原函式轉為 Scriptblock,以方便 Exclude 功能遞迴呼叫 + $InnerScrip = { + # 取用管線傳入的 hash 進行 match + process { + $Key = $_["Regex"] | &$Keyfind + if ($_["Target"] -match $_["Regex"]) { + $Result = @{ + $Key = $Matches[$Key] + # 將錨定字串中的特殊字元轉換為一般字串 + Anchor = $Matches[0] -replace "([\[\]\(\)])", '\$1' + } + $LR = $_["Target"] -split $Result["Anchor"] + $Result.Add("L", $LR[0]) + $Result.Add("R", $LR[1]) + # 對 Anchor 結果,以 $Exclude 進行比對,符合則加入剩餘字串重新 match + if ($Exclude -and($Result["Anchor"] -match $Exclude)) { + $Result.Add("Regex", $_["Regex"]) + $Result.Add("Target", $Result["L"]) + return $Result | &$InnerScrip + } + # 結尾模式,僅輸出符合 TAGKEY 的 hash + if ($End) { return @{ $Key = $Result[$Key] } } else { return $Result } + } + } + } + + #Write-Host $Str + # Regex 可輸入複數個 + $Regex | % { @{ Target = $Str; Regex = $_; } } | &$InnerScrip +} + +# Example +# $RS1 | % { Get-NameRegex $_["L"] -Regex $NameRegex -Exclude "(BD|DVD|HDTV)RIP" -End } +# $TestName | % { Get-NameRegex $_ -Regex $Regex } | % { $(Get-NameRegex $_["L"] -Regex $NameRegex -Exclude "(BD|DVD|HDTV)RIP" -End) + $_ } +# SIG # Begin signature block +# MIIFxwYJKoZIhvcNAQcCoIIFuDCCBbQCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB +# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR +# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU2SfubhaWpugweGH+53eZ/igq +# VQOgggNSMIIDTjCCAjagAwIBAgIQdMnAQ4BDAo9Lgfn+QeDdhjANBgkqhkiG9w0B +# AQUFADAqMRMwEQYDVQQDDApOaWNrIENoYW5nMRMwEQYDVQQKDAplYXJpY2sub3Jn +# MB4XDTIxMTAzMTEwMDI1MVoXDTIyMTAzMTEwMjI1MVowMzEiMCAGCSqGSIb3DQEJ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eDdhjAJBgUrDgMCGgUAoHgwGAYKKwYBBAGCNwIBDDEK +# MAigAoAAoQKAADAZBgkqhkiG9w0BCQMxDAYKKwYBBAGCNwIBBDAcBgorBgEEAYI3 +# AgELMQ4wDAYKKwYBBAGCNwIBFTAjBgkqhkiG9w0BCQQxFgQUoqQp8TNHQrFdDrpM +# 6+iDq3QvqIUwDQYJKoZIhvcNAQEBBQAEggEApo8NtKRWXRdAxdmJMSYorrOWC1ra +# NaN0N3UbhQKTAjbN3sYP4oZmOiEftQn5Ot0JztpYAb29Ge//R+0EGQR69IoGMgr0 +# X5toFyWFle3ddKu3swsFv9dJI4vYSW1Pce13kBdp+2a7hCF/6kS1aYDLEdEYhcwq +# 8rqRSNtr73Ro00eMVSm72GRv709ouWK5jCqCPq6r/7O4UZvyiqyF4Oq8fZDAsWlK +# Tc5zLTtnaIysgvAZm7izn0oSD/kLx6hv7wGNp7yVSUkNq7a7qu34Brs5eQtZyblj +# JbZPtDYY0++t5xgHskpigkAVej3B1TkNg1rH1EJ49JX10yeIq7JEMS4u8Q== +# SIG # End signature block diff --git a/PathChar.psd1 b/PathChar.psd1 new file mode 100644 index 0000000..61ff9d3 Binary files /dev/null and b/PathChar.psd1 differ diff --git a/PathChar.psm1 b/PathChar.psm1 new file mode 100644 index 0000000..450ac2b --- /dev/null +++ b/PathChar.psm1 @@ -0,0 +1,41 @@ +function Edit-PathChar { + param( + $Str, + $Char = "[\[\]]" + ) + $Str -replace "(${Char})", '`$1' +} + +# SIG # Begin signature block +# MIIFxwYJKoZIhvcNAQcCoIIFuDCCBbQCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB +# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR +# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUNYO6iFH1B+kW/b06vFyGuFhF +# TYSgggNSMIIDTjCCAjagAwIBAgIQdMnAQ4BDAo9Lgfn+QeDdhjANBg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