Solved

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

Posted on 2014-02-25
5
1,015 Views
Last Modified: 2014-08-01
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 ?
0
Comment
Question by:wind-surfer
  • 3
5 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
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
 
LVL 13

Expert Comment

by:lciprianionut
Comment Utility
Hello,

Try next one:
Get-ChildItem -Path C:\SOURCE -Filter *.jpeg -Recurse | Move-Item -Destination C:\PICTURES
0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
@ciprian: and if some items have the same name, but in different subfolders?
0
 

Author Comment

by:wind-surfer
Comment Utility
It is preferable to rename any duplicate files automatically as Windows Explorer GUI would do.
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 20 total points
Comment Utility
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

Featured Post

Do email signature updates give you a headache?

Do you feel like you are constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Join & Write a Comment

Our Group Policy work started with Small Business Server in 2000. Microsoft gave us an excellent OU and GPO model in subsequent SBS editions that utilized WMI filters, OU linking, and VBS scripts. These are some of experiences plus our spending a lo…
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now