2014-10-20 73 views
0

我正在處理一個腳本,將電視節目轉移到我的驅動器上相應的文件夾中。我遇到了將節目與其文件夾相匹配的問題。這是我有一個問題的代碼片段:PowerShell與正則表達式匹配的字符串

#Remove all non-alphanumeric characters from the name 
$newname = $Episode.Name -replace '[^0-9a-zA-Z ]', ' ' 

#Split the name at S01E01 and store the showname in a variable (Text before S01E01) 
$ShowName = [regex]::Split($newname, 'S*(\d{1,2})(x|E)')[0] 

#Match and get the destination folder where the names are similar 
################## THIS IS WHERE THE ISSUE IS ####################### 
$DestDir = Gci -Path $DestinationRoot | Where { $ShowName -like "*$($_.Name)*" } | foreach {$_.Name } 

例如,一個名爲秀「神祕博士2005 S02E02牙齒和Claw.mp4」沒有返回一個類似文件夾,命名爲「DoctorWho 」。

問題: 我可以修改$ DestDir以便我可以匹配名稱?有沒有更好的方法來做到這一點?

工作代碼:從測試輸出

# Extract the name of the show (text before SxxExx) 
$ShowName = [regex]::Split($Episode.Basename, '.(\d{1,3})(X|x|E|e)(\d{1,3})')[0] 

# Assumption: There is a folder in TV shows directory that is named correctly, and the input file is named correctly 
# Try to match by stripping all non-Alphabet characters from both names and check if the folder name contains the file name 
$Folder = gci -Path $DestinationRoot | 
      Where {$_.PSisContainer -and ` 
      (($_.Name -replace '[^A-Za-z]','') -match ($ShowName -replace '[^A-Za-z]','')) } | 
      select -ExpandProperty fullname 

一些示例:

Input file name: Arrow S01E02.mp4 
Show name:   Arrow 
Matching folder: C:\Users\Public\Videos\TV Shows\Arrow 
----------------------------------------------------------------------- 
Input file name: Big Bang Theory S3E03.avi 
Show name:   Big Bang Theory 
Matching folder: C:\Users\Public\Videos\TV Shows\The Big Bang Theory 
----------------------------------------------------------------------- 
Input file name: Doctor Who S08E03.mp4 
Show name:   Doctor Who 
Matching folder: C:\Users\Public\Videos\TV Shows\Doctor Who (2005) 
----------------------------------------------------------------------- 
Input file name: GameOfThronesS01E01.mp4 
Show name:   GameOfThrones 
Matching folder: C:\Users\Public\Videos\TV Shows\Game Of Thrones 
----------------------------------------------------------------------- 
+0

請原諒我陳述明顯,但爲什麼你不確定文件夾是否符合節目名稱,即將「DoctorWho」重命名爲「Doctor Who 2005」? PowerShell不知道你的「類似」的想法是什麼,你必須說明每個案例。你可以做的一件事是添加'.Trim()',這樣開始和結束處的空格都被刪除了,但是你必須明確地說出進一步的轉換。 – 2014-10-20 15:31:29

+0

@JeroenMostert這對我來說很合適。但是,我已經計劃在完成後與其他人分享。我不確定他們的設置是什麼樣的,所以我試圖讓它儘可能通用。 – 2014-10-20 15:37:01

+0

好吧,你可以做一些事情,比如「去掉所有的空格和尾部的數字,並顯示名字和文件夾名稱,然後開始匹配」,但似乎很可能無論你做什麼,有人會抱怨說它不能正常工作。至少簡單的算法有一個好處,人們可以理解發生了什麼。 – 2014-10-20 15:45:50

回答

2

使用,你要弄清楚什麼節目名稱是基於你的建議的方法相同。隨着Doctor Who 2005 S02E02 Tooth and Claw.mp4

$showName = $Episode -replace '[^0-9a-zA-Z ]' 
$showName = ($showName -split ('S*(\d{1,2})(x|E)'))[0] 
$showName = $showName -replace "\d" 

我增加了行$showName = $showName -replace "\d"考慮在本賽季的一年。如果節目中間包含一個數字,但應該適用於大多數情況,這是一個警告。繼續進行$DestDir的確定。部分問題是你有你的Where比較倒退。您想要查看節目名稱是否是潛在文件夾的一部分,而不是其他方式。另外,由於潛在的文件夾可能包含空格,因此共同性也應該包含該假設。

Get-ChildItem -Path $DestinationRoot -Directory | Where-Object { ($_.name -replace " ") -like "*$($showName)*"} 

我會去使用Choice選擇讓用戶確認文件夾,因爲它可能有多個匹配。我想指出的是,可能很難說明所有的命名約定和差異,但是你擁有的是一個好的開始。

+0

我完成了一些類似於你所建議的事情。我做了一個包含,而不是一個像。至於選擇,我已經決定腳本將發送電子郵件給用戶讓他們知道有文件不匹配,這將需要手動干預。謝謝您的幫助! – 2014-10-20 20:06:35

+0

@Kevin_'-contains'只適用於數組。在'where-object'中你不會使用數組。供參考 – Matt 2014-10-20 20:17:59

+0

哎呀,意思是說 - 匹配。 – 2014-10-20 20:23:22

相關問題