2015-05-29 62 views
0

我有幾個帶有重複命名約定的文件,例如,將每個n個文件移動到一個單獨的文件夾中

1*Intro* 
2* 
3* 
… 
10*intro* 
…. 

我想將每個模塊移動到一個單獨的文件夾中。所以,我應該從每個*intro*分開,直到下一個。 另外,我應該注意文件編號和排序。 我想,要做到這一點最簡單的方法是:

1. Get a list of intros. 
2. Separate their numbers. 
3. Start moving files starting from one number till their smaller than the next one. 


$i = 1 
Ls *intro* | ft {$_.name -replace '\D.*', ''} 
// The reason for .* is that the files are `mp4`. 
Ls * | ? {$_.name -match '[^firstNumber-SecondNumber-1]'} | move-item -literalpath {$_.fullname} -destination $path + $i++ + '/' +{$_.name} 

所以最後的命令應該是這樣的:

Ls *intro* | % { ls * | ? {…} | move-item … } 

或者,也許move-item本身可以做篩選工作。

正則表達式不起作用,我沒有足夠的Powershell知識來寫更好的東西。你能想到任何腳本來做到這一點嗎?另外,我應該如何允許move-item創建文件夾?

如果有人能以更好的標題編輯此帖子,我將非常感激。

+0

你的文件實際上叫什麼名字? 「*」不是有效的文件名字符。 –

+0

它是任何可能角色的佔位符!通常只是按字母順序。 – Akbari

回答

3

這可以用簡單的Switch來完成。該開關將針對當前文件夾中的所有項目運行(通過別名'LS'使用的Get-ChildItem cmdlet獲得的項目)。它看起來是否在文件名中包含字符串「Intro」。如果是,則會創建一個包含該文件名的新文件夾,並將該文件夾的信息存儲在$TargetFolder變量(以前創建的變量以避免範圍問題)中。然後它將文件移動到該文件夾​​,並繼續到下一個文件。如果該文件的文件名中沒有「Intro」,則只是將文件移動到最後分配的$TargetFolder

$TargetFolder = "" 
Switch(Get-ChildItem .\*){ 
    {$_.BaseName -match "intro"} {$TargetFolder = New-Item ".\$($_.BaseName)" -ItemType Directory; Move-Item $_ -Destination $TargetFolder; Continue} 
    default {Move-Item $TargetFolder} 
} 
相關問題