diff --git a/Animelibk.ps1 b/Animelibk.ps1 new file mode 100644 index 0000000..f03a25c --- /dev/null +++ b/Animelibk.ps1 @@ -0,0 +1,173 @@ +param( + $Path, + [switch] $Single +) +#$TargetName = "俺だけ入れる隠しダンジョン〜こっそり鍛えて世界最強〜"; +$TargetName = "" +#$DestinName = "真の仲間" +#$Path = "" +#$Path = "H:\Data\Downloads\因為不是真正的夥伴而被逐出勇者隊伍\[Shin no Nakama][04][BIG5][1080P].mp4" +$JsonPath = $("..\Anime\* $TargetName", $($Path -replace '(.*\\[^\[\\\]]*)\\\[.*', '$1') | ? {Test-Path $_}) +$WorkData = $(if (Test-Path "$JsonPath\animelibk.json") { + Get-Content "$JsonPath\animelibk.json" | ConvertFrom-Json +} else { + "" | select ID, WorkName, TargetName, DestinName, Season, Episode +}) + +# 確認資料狀態,缺少則進行物件資料建立 +if (-not($WorkData.WorkName)) { + $WorkData.WorkName = Read-Host "請輸入作品名稱:"; +} +if (-not($WorkData.TargetName)) { + $WorkData.TargetName = $($JsonPath -replace '.*\\[0-9\s]*(.*)$', '$1'); +} +if (-not($WorkData.DestinName)) { + $WorkData.DestinName = Read-Host "請輸入目地的名稱:"; +} +if (-not($WorkData.Season)) { + [int] $WorkData.Season = Read-Host "請輸入季別:"; +} +if (-not($WorkData.Episode)) { + $WorkData.Episode = @(); +} +$WorkData + +$Seasons_TC = "", "第一季", "第二季", "第三季", "第四季", "第五季" +$Year = $(get-date -Format ‘yyyy’) + +# 取得目標資料夾是否存在,不存在建立資料夾,並取得資料夾物件 +#$DestDir = $(if ( -not(Test-Path -Path ".\$DestinName\$($Seasons_TC[$Season])*")) { +# mkdir -p ".\$DestinName\$($Seasons_TC[$Season]) ($Year)" +#} else { +# Get-Item -Path ".\$DestinName\$($Seasons_TC[$Season])*" +#}) +#$DestDir = $(New-Item -Path ".\$WorkData.DestinName\$($Seasons_TC[$WorkData.Season]) ($Year)" -ItemType Directory -Force) + +function Get-StandName { + param ( + $FileObj + ) + + # 進行傳入變數判斷,非 FileInfo 物件則視為 String,先進特殊字元處理,再取得物件 + $File = $(if ($FileObj.GetType().Name -eq 'FileInfo') { + $FileObj + } else { + Get-Item -Path $($FileObj -replace '([\[\]])', '`$1') + }) + + # 因應檔名不同而設定多組正規方式 + $Result = @( + #$($Path -match "\[([^\]]*)\]\[(\d{1,2})\]") + $($File.Name -match "[\[\s]([^\]]*?)(?:[\]\[\-\s]+)(\d{1,2})[\]\s]") + ) | % { if ($_) { $Matches }} + + # 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[1]; + $OutputData.Season = $WorkData.Season; + $OutputData.Episode = [int] $Result[2]; + $OutputData.newFullName = "{0} S{3:00}E{1}{2}" -f $Result[1], $Result[2], $File.Extension, $WorkData.Season; + #$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; +} + +### Select Object Mode START ### + +# 建立自定屬性 File 和運算公式 +$FileObj = @{ + label = "File"; + Expression = {$_}; +} +# 建立自定屬性 NewName 和運算公式 +$NewNameObj = @{ + label = "NewName"; + Expression = {Get-StandName $_}; +} +$StructPathObj = @{ + label = "StructPath"; + Expression = {$(New-Item -Path $(".\{0}\{1} ({2})" -f $WorkData.DestinName, $Seasons_TC[$WorkData.Season], $Year) -ItemType Directory -Force).FullName}; +} + +### # Select Object Mode END ### + +# 運作模式 +if ($Single) { + # 單集模式 + $List = @(Get-Item -Path $($Path -replace '([\[\]])', '`$1')) + $BuildList = $List | select $FileObj, $StructPathObj, $NewNameObj | ? {$_.NewName.Episode -notin $WorkData.Episode} + #$BuildList | ? {$_.NewName.Episode -notin $WorkData.Episode} | Format-List +} + +$ENDResult = $BuildList | % { + $Command = @{ + ItemType = 'HardLink' + Path = $("{0}\{1}" -f $_.StructPath, $_.NewName.newFullName) + Target = $($_.File.Fullname -replace '([\[\]])', '`$1') + } + #try { + if (New-Item @Command) { $_.NewName } + #} catch { + + #} +} +#$ENDResult +#$ENDResult.gettype(); +$ENDResult | % { if ($_) { $WorkData.Episode += $_.Episode }} +$WorkData | ConvertTo-Json | Out-File -Encoding "UTF8" "$JsonPath\animelibk.json" +exit 0; + +##### 以下試寫或測試用 ##### +# SIG # Begin signature block +# MIIFxwYJKoZIhvcNAQcCoIIFuDCCBbQCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB +# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR +# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUuwJuleFHAYU44/Bdnmy+R8Lg +# /zKgggNSMIIDTjCCAjagAwIBAgIQdMnAQ4BDAo9Lgfn+QeDdhjANBgkqhkiG9w0B +# AQUFADAqMRMwEQYDVQQDDApOaWNrIENoYW5nMRMwEQYDVQQKDAplYXJpY2sub3Jn +# MB4XDTIxMTAzMTEwMDI1MVoXDTIyMTAzMTEwMjI1MVowMzEiMCAGCSqGSIb3DQEJ +# ARYTcGNuaWNrMTAwQGdtYWlsLmNvbTENMAsGA1UEAwwETmljazCCASIwDQYJKoZI +# hvcNAQEBBQADggEPADCCAQoCggEBAMEnEk3/5rkEMibDbbfOXQaerk5xXSJmY8gf +# FAVUbrGfjzYHcRsPRzdb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