Solved

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

Posted on 2014-02-25
5
1,227 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
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…

734 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