param( $Path, $Index = "H:\AnimeLib", $VarConfig, $RecorDB = "animelibk.ini", $Include = @(".rmvb", ".mp4", ".mkv", ".ass", ".ssa"), [switch] $Single, [switch] $Force, [switch] $Sub, [switch] $testrun ) Import-Module -Name ($PSScriptRoot + "\PathChar.psd1") Import-Module -Name ($PSScriptRoot + "\NameRegex.psd1") Edit-PathChar $Path #$TargetName = "俺だけ入れる隠しダンジョン〜こっそり鍛えて世界最強〜"; $TargetName = "" #$DestinName = "真の仲間" Write-Output $Path #$Index = "L:\AnimeLib" # scriptblock 減少重複 $RegexpSet = { # 全域化 # 因應檔名不同而設定多組正規方式 Set-Variable -Name "EpisodeRegex" -Scope global -Value @( "(?:S\d{2})?[\s]?-[\s]E?(?\d{2})" "[\[\b\s](?\d{2})[\s_]?(?:[vV]\d|END|FIN)[\]\b\s]" "\[(?\d{1,2})\D{2}\]" "\[(?\d{1,2})\]" ) Set-Variable -Name "NameRegex" -Scope global -Value @( "[\[\b\s](?[^\[\]]*)[\]\b\s]?$" ) } if ($VarConfig) { if (Test-Path $PSScriptRoot\$VarConfig) { $T = Get-Content -Encoding UTF8 $PSScriptRoot\$VarConfig | % { $H = @{} ; $S = $null} { if ($_ -match "^\[(?\w*)\]" ) { $S = $Matches["T"] $H.add($S, @()) } elseif ($_ -match ".+" ) { $H[$S] = $H[$S] + @($_ -replace '"', '') } } { $H ; Clear-Variable -Name "H"} $T.GetEnumerator() | % { Set-Variable -Name $_.name -Value $_.value} } else { &$RegexpSet $EpisodeRegex | % {"[EpisodeRegex]"}{ """$_""" }{""} | Out-File -Encoding UTF8 $PSScriptRoot\$VarConfig $NameRegex | % {"[NameRegex]"}{ """$_""" } | Out-File -Encoding UTF8 -Append $PSScriptRoot\$VarConfig } } else { &$RegexpSet } #$Path = "L:\Anime\202001 ネコぱら\[Nekomoe kissaten][Nekopara][01-12][BDRip][1080p][CHT]\[Nekomoe kissaten][Nekopara][12][1080p][CHT].mp4" #$Path = "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" #$Path = "H:\Anime\201901 盾の勇者の成り上がり\[LKSUB][Tate no Yuusha no Nariagari][01-25][GB][720P][MP4]\[LKSUB][Tate no Yuusha no Nariagari][01][GB][720P].mp4" $JsonPath = $("$Index\Anime\* $TargetName", $($Path -replace '(.*\\[^\[\\\]]*)\\\[.*', '$1') | ? {Test-Path $_}) $WorkData = $(if (Test-Path $RecorDB) { Get-Content $RecorDB | ConvertFrom-Json } else { "" | Select-Object ID, WorkName, TargetName, DestinName, Season, Episode }) if ($RecorDB -notmatch ":") { $RecorDB = "$JsonPath\$RecorDB" } # 確認資料狀態,缺少則進行物件資料建立 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 = "", "第一季", "第二季", "第三季", "第四季", "第五季" $Seasons_TC = "", "Seasons 1", "Seasons 2", "Seasons 3", "Seasons 4", "Seasons 5" $Lang = @{ Season = "Season {0:00}" } $Year = @($($JsonPath -replace '.*\\((?\d{4})?(?\d{2})?).*$', '${Y}'), $(get-date -Format 'yyyy')) | ? {-not([string]::IsNullOrEmpty($_))} | Select -First 1 # 取得目標資料夾是否存在,不存在建立資料夾,並取得資料夾物件 $LinkPath = "{2}\{0}\{1}" -f $WorkData.DestinName, $($Lang["Season"] -f $WorkData.Season), $Index $LinkDir = $(if (-not(Test-Path -Path "$LinkPath*")) { New-Item -Path "$LinkPath ($Year)" -ItemType Directory -Force } else { Get-Item -Path "$LinkPath*" }) #$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 $(Edit-PathChar $FileObj) }) $Result = $File.Name | % { Get-NameRegex $_ -Regex $EpisodeRegex } | % { $(Get-NameRegex $_["L"] -Regex $NameRegex -Exclude "(BD|DVD|HDTV)RIP|BIG5" -End) + $_ } | Select -First 1 # 建立回傳資料物件,以方便後續資料處理。 $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"}); 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}; Expression = {$LinkDir}; } ### # Select Object Mode END ### # 運作模式 if ($Single) { # 單集模式 $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 $(Edit-PathChar $Path)) $BuildList = $List | select $FileObj, $StructPathObj, $NewNameObj | ? {$_.File.Extension -in @(".ass", ".ssa")} } elseif ($testrun) { $List = @(Edit-PathChar $Path | Get-Item | ? { $_.GetType().Name -eq "FileInfo" }) + @(Edit-PathChar $Path | Get-Item | ? { $_.GetType().Name -eq "DirectoryInfo" } | % { $_.GetFiles() }) $BuildList = $List | select $FileObj, $StructPathObj, $NewNameObj if ($Include -ne "") { $BuildList = $BuildList | ? {$_.File.Extension -in $Include} } if (-not($Force)) { $BuildList = $BuildList | ? {$_.NewName.Episode -notin $WorkData.Episode} } } else { exit 0; } $ENDResult = $BuildList | % { $Command = @{ ItemType = 'HardLink' Path = $("{0}\{1}" -f $_.StructPath, $_.NewName.newFullName) Target = $(Edit-PathChar $_.File.Fullname) } #try { if (New-Item @Command) { Write-Host $_.NewName.newFullName $_.NewName } #} catch { #} } #$ENDResult #$ENDResult.gettype(); if (-not($Sub)) { $ENDResult | % { if ($_.Episode) { $WorkData.Episode += $_.Episode }}} $WorkData | ConvertTo-Json | Out-File -Encoding "UTF8" $RecorDB exit 0;