2017-06-29 82 views
2

我在尋找這樣的一個我已經找到了PowerShell腳本:PowerShell的 - 以文件的修復量,並將其移動到新的文件夾

Get-ChildItem -File | # Get files 
    Group-Object { $_.Name -replace '_.*' } | # Group by part before first underscore 
    ForEach-Object { 
    # Create directory 
    $dir = New-Item -Type Directory -Name $_.Name 
    # Move files there 
    $_.Group | Move-Item -Destination $dir 
    } 

但不同,該組對象應從文件夾A中獲取5個文件的固定量,創建一個由第一個文件命名的新文件夾,然後將這5個文件移動到新文件夾中。見下面的示例圖片(文件名稱不同)。 我在PowerShell中血腥beginer所以如果可能的話,請保留意見簡單;)

enter image description here
enter image description here

感謝和問候!

+0

@保羅:本'集團Object'基團通過共享文件名前綴輸入文件之前(第一)'_'中的文件名。然後,ForEach-Object對每個結果組進行操作,創建一個以共享前綴命名的目錄,並將該組中的所有文件移動到該新目錄中。 – mklement0

+0

@nosediver:你是否只想從每個group_中取出前5個文件?如果你想分組,你不清楚你在找什麼邏輯。請通過直接更新您的問題來澄清。 – mklement0

+0

將分組更改爲除以5的計數器並四捨五入至最接近的整數'$ n = 0; Get-ChildItem -File | Group-Object -Property {$ script:n ++; [數學] ::天花板($ n/5)} | ForEach-Object {你的代碼在這裏}'可以做到這一點 – TessellatingHeckler

回答

0

感謝@Vitaly!該解決方案運作良好。
下面是完整的腳本:

$n = 0; Get-ChildItem -File | Group-Object -Property {$script:n++; [math]::Ceiling($n/5)} | 
ForEach-Object { 
$dname = $_.group.name  
$fname=$dname 
$fl = $fname[0].Split(".")[0] 
    $dir = New-Item -Type Directory -Name $fl      

    $_.Group | Move-Item -Destination $dir  
} 
0

謝謝@TessellatingHeckler,它運作良好。

$n = 0; Get-ChildItem -File | Group-Object -Property {$script:n++; [math]::Ceiling($n/5)} | 
ForEach-Object {        
    $dir = New-Item -Type Directory -Name $_.Name # Create directory 
    $_.Group | Move-Item -Destination $dir   # Move files there 
} 

對於我使用的是從@Dennis麪包車GILS(How to rename a folder the same as the filename of the first file inside that folder using Windows batch programming?) 一批改名部分我相信有一種方法與PowerShell的部分一起做,但是,這個解決方案適用於我。

0

Hello. You can also try this, if you want to creates a new folder named by the first file:

ForEach-Object { 
$dname = $_.group.name  
$fname=$dname.replace(".","")  
$fl = $fname[0] 
    $dir = New-Item -Type Directory -Name $fl      

    $_.Group | Move-Item -Destination $dir 
+0

謝謝維塔利。完美的作品!它只需要刪除以文件夾名稱結尾的文件。 – nosediver

+0

如果你不需要使用文件擴展名,你可以在這裏替換這個$ fname = $ dname.replace(「。」,「」) $ fl = $ fname [0]:$ fname = $ dname $ fl = $ FNAME [0] .Split( 「」)[0] – Vitaly

相關問題