Solved

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

Posted on 2014-02-25
5
1,283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 20 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Suggested Courses

617 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