3

I am counting all files in my Pictures folder with

Get-ChildItem C:\pictures -force | Group-Object extension | Sort-Object count -descending | ft count,name -auto

I am then copying all my MTS-files (video) to a separate folder with

Get-ChildItem C:\pictures -force -recurse -include *.MTS | Copy-Item -Destination c:\video

This works nicely. But, how can I create a folder for each year in c:\video and then copy the corresponding files?

UPDATE:

Shay has helped me with accomplishing this and I now have the following code:

# Create a folder for each year and move the specified files to the corresponding folders
Get-ChildItem $fromFolder -Force | 
Group-Object {$_.CreationTime.Year} | Foreach-Object {

    # Testing to see if the folder exist
    if(!(Test-Path $toFolder\$($_.Name))) { 
        $folder = New-Item -Path "$toFolder\$($_.Name)" Itemtype Directory -Force 
        echo "Created $toFolder\$($_.Name)"
    } else {
        echo "Folder $toFolder\$($_.Name) exist"
    }

    # Testing to see if the file exist in the target directory
    if(!(Test-Path $_.group)) { 
        $_.group | Copy-Item -Destination $folder.FullName
        echo "Copyied $_ to $folder"
        } else {
            echo "File exist"
        }
}    

It tests the folders OK, but skips all the Test-Path on files. Am I breaking the loop somehow? Or messing up the pipeline?

Sune
  • 3,080
  • 16
  • 53
  • 64

1 Answers1

4

Try this:

Get-ChildItem C:\pictures -Filter *.MTS -Force -Recurse | 
Group-Object {$_.CreationTime.Year} | Foreach-Object{
    $folder = New-Item -Path "c:\video\$($_.Name)" ItemType Directory -Force
    $_.Group | Where-Object { -not (Test-Path "$($folder.FullName)\$($_.Name)") } | Copy-Item -Destination $folder.FullName
}
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Thank you very much Shay! This works! I would however like to copy, not move:) I am also trying to test if the folder or file already have been moved (I'll be running this script from time to time.). I have updated my question with my code. Thanks again Shay:) – Sune Aug 02 '12 at 13:30
  • 1
    To copy the files replace Move-Item with Copy-Item. I update the code. I also added a Where-Object that passes on (tested by file name) only files that don not exist on the desination path. – Shay Levy Aug 02 '12 at 13:59
  • Thank you very much! Fantastic! (I know about copy-item, I was just trying to FOR ONCE know better than you;) My script is scanning over 100GB of data and I'm trying to display some kind of status but I am confused about how the script runs.. Does it scan all the directories and then pass the results down the pipeline and foreach-ing each object? How would I make a proper on-screen status? And again, thank you Shay, you've been one of my best teachers on learning Powershell:) – Sune Aug 04 '12 at 21:25
  • It scans all directories under C:\pictures recursively, groups the objects by the year they were created and then each group members are piped to the foreach-object cmdlet (processing them one at a time). To output a status message, add a Write-Host line inside the foreach-object scriptblock. – Shay Levy Aug 05 '12 at 06:23