Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-02-25
5
Medium Priority
?
1,465 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 35

Expert Comment

by:Dan Craciun
ID: 39886735
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
ID: 39886754
Hello,

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

Expert Comment

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

Author Comment

by:wind-surfer
ID: 39886792
It is preferable to rename any duplicate files automatically as Windows Explorer GUI would do.
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 80 total points
ID: 39886838
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 Creator Update has just been released and I have it working very well on my laptop. Read below for issues, fixes and ideas.
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

824 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