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?

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

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:)
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.
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:)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.