Powershell command to move jpeg files found in all subdirectories to my Pictures folder

I have jpeg files in various subdirectories of a folder, let's call is C:\SOURCE. What is the Powershell command to move (not just copy) all jpeg files found within C:\SOURCE and its subdirectories to my Pictures folder, C:\PICTURES ?
wind-surferAsked:
Who is Participating?
 
Dan CraciunConnect With a Mentor IT ConsultantCommented:
OK. But since it's not automatic in PS, this will do it:
$source = "C:\SOURCE"
$dest = "C:\PICTURES"
GCI $source -Include "*.jpg" -Recurse |
    % {
        $newName = $dest + "\" + $_.Name
        $i = 1
        while (Test-Path $newName) {
            $newName = $dest + "\" + $_.BaseName + "(" + "$i" + ")" + $_.Extension
            $i++
        }
       Move-Item -Path $_.FullName -Destination $newName
}

Open in new window

0
 
Dan CraciunIT ConsultantCommented:
This should work:
$source = "C:\SOURCE"
$dest = "C:\PICTURES"
GCI $source -Include "*.jpg" -Recurse |
    % {
        $path = $dest + ($_.DirectoryName).substring($source.length)
        if (!(Test-Path $path)) {New-Item $path -type directory -Force}
        Move-Item -Path $_.FullName -Destination "$path\" -Force
}

Open in new window

HTH,
Dan
0
 
lciprianionutCommented:
Hello,

Try next one:
Get-ChildItem -Path C:\SOURCE -Filter *.jpeg -Recurse | Move-Item -Destination C:\PICTURES
0
 
Dan CraciunIT ConsultantCommented:
@ciprian: and if some items have the same name, but in different subfolders?
0
 
wind-surferAuthor Commented:
It is preferable to rename any duplicate files automatically as Windows Explorer GUI would do.
0
All Courses

From novice to tech pro — start learning today.