Powershell - Copy all jpg files and rename duplicates

Dave Ramsay 21 Reputation points
2021-10-25T17:58:25.483+00:00

Hi, I am new to Poweshell and it seems to be a solution for what I am trying to do but I cannot figure out the correct code to use. I am trying to copy all .jp files from my backup drive E: to a directory on the same drive E:\Photo_Backup

I have many duplicate file names in many back up directories which I would like to move to E:\Photo_Backup with no other sub directories. How can this be accomplished by keeping the original file names but appending 1,2,3, etc. to each name to differentiate between the files?

My goal is to review the files that are the same size and similar name and then delete the ones I don't want which are filling up my back up drive.

Thanks.

Dave

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,346 questions
{count} votes

Accepted answer
  1. Rich Matheisen 44,621 Reputation points
    2021-10-25T21:25:46.743+00:00

    See if this works:

    Function fcopy ($SourceDir, $DestinationDir)
    {
        Get-ChildItem $SourceDir -Recurse -File -Include *.jpg |
            Where-Object {$_.FullName -notlike "$DestinationDir*"} |    # Do *NOT* venture into the directory you're moving files to!!!
                ForEach-Object {
                    $SourceFile = $_.FullName
                    $DestinationFile = $DestinationDir + $_.Name
                    if (Test-Path $DestinationFile) {
                        $i = 0
                        while (Test-Path $DestinationFile) {
                            $DestinationFile = "{0}{1}{2}{3}" -f $DestinationDir, $_.Basename, $i++, $_.Extension
                        }
                    }
                    Copy-Item -Path $SourceFile -Destination $DestinationFile -Verbose -Force
                }
    }
    
    fcopy -SourceDir "E:\" -DestinationDir "E:\Photo_Backup\"
    

    The problem is that the Get-ChildItem is including the files your destination directory because the destination directory is a child of your source directory.


2 additional answers

Sort by: Most helpful
  1. Michael Taylor 47,471 Reputation points
    2021-10-25T18:21:51.217+00:00

    To filter on only the files you care about add the Include parameter to your Get-ChildItem call.

    Get-ChildItem $sourceDir -Recurse -Include *.jpg
    

    Note that you should also probably put a -File on the call so you only get the files you care about.


  2. Dave Ramsay 21 Reputation points
    2021-10-25T19:22:12.72+00:00

    Thank you again. It seems that this may not like the sub directory folder names that Seagate created or my code needs some enhancing? When I look at the error message it appears that the Destination is not only E:\Photo_Backup, but also the sub directories the files reside in. Hope my comment makes sense.

    The copy worked before I added the filter but the code grabbed all files regardless of the extension and put them in the E:\Photo_Backup directory. Not sure why I am getting error messages now.

    VERBOSE: Performing the operation "Copy File" on target "Item: E:\Seagate Dashboard 2.0\DAVES-NEW-HP-AL\2rams\Backup\6f4eff08-67aa-44c7-a519-a0a459d1f9e5\20211018_235910_2ramsInc117\C\Users\2rams\OneDriv e\Pictures\Emma and Ivey Fall 2021 Photo Shoot\highlights\21Fall.jpg Destination: E:\Photo_Backup\E:\Seagate Dashboard 2.0\DAVES-NEW-HP-AL\2rams\Backup\6f4eff08-67aa-44c7-a519-a0a459d1f9e5\20211018_235910_2ramsInc117\C\Users\2rams\OneDriv e\Pictures\Emma and Ivey Fall 2021 Photo Shoot\highlights\21Fall.jpg". Copy-Item : The given path's format is not supported. At line:13 char:13

    •         Copy-Item -Path $SourceFile -Destination $DestinationFile ...
      
    •         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      
      • CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException
      • FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand

    =====================================================================================

    Here is the code I ran which resulted in the error message above..........................

    *function fcopy ($SourceDir,$DestinationDir) { Get-ChildItem $sourceDir -Recurse -Include .jpg -File | Where-Object { $.PSIsContainer -eq $false } | ForEach-Object ($) { $SourceFile = $.FullName $DestinationFile = $DestinationDir + $ if (Test-Path $DestinationFile) { $i = 0 while (Test-Path $DestinationFile) { $i += 1 $DestinationFile = $DestinationDir + $.basename + $i + $.extension } } else { Copy-Item -Path $SourceFile -Destination $DestinationFile -Verbose -Force } Copy-Item -Path $SourceFile -Destination $DestinationFile -Verbose -Force } } fcopy -SourceDir "E:" -DestinationDir "E:\Photo_Backup"