Search code examples
powershellfilerenamebulk-rename-utility

How to batch rename multiple files across multiple folders and combine them into one massive folder?


I have a list of 100+ folders listed already in order.

Folder 1 Folder 2 Folder 3 etc...

In each of the folder contains images that are also in order

001.jpg 002.jpg 003.jpg etc...

Ultimately, I am trying to combine these 100+ folders into one massive folder with all the images inside according to their order, renaming each image from 000000001 - 99999999.

I am a complete beginner in coding. I have tried using Bulk Renaming Utility but i cant get it to combine all the images from multiple folders into one folder.

Any help is appreciated.

Thank you!


Solution

  • Here's the minimum script needed to meet your requirements. There's no error checking.

    Set $topSourceFolderName and $targetFolderName as needed for your computer

    I assume your 100+ folders, Folder 1 Folder 2 Folder 3 etc. all exist under a single parent folder ($topSourceFolderName)

    I assume the single parent folder and all of the 100+ folders under the single parent folder contain only jpg files

    I assume your "one massive folder" ($targetFolderName) is NOT anywhere under the single parent folder ($topSourceFolderName)

    I assume the PowerShell script is stored in a folder other than $topSourceFolderName or $targetFolderName

    Email me at william.s.charlton@outlook.com if you have any questions

    In my example, the PowerShell script is stored in c:\temp\bulkConsolidate.ps1

    C:\Temp\massiveFolder is the target folder. It is initially empty.

    C:\Temp\topFolder\Folder 1 has 001.jpg and 002.jpg

    C:\Temp\topFolder\Folder 2 has 001.jpg and 002.jpg

    After the script is run, C:\Temp\massiveFolder has 00000001.jpg, 00000002.jpg, 00000003.jpg, and 00000004.jpg

    00000001.jpg is copied from C:\Temp\topFolder\Folder 1\001.jpg

    00000002.jpg is copied from C:\Temp\topFolder\Folder 1\002.jpg

    00000003.jpg is copied from C:\Temp\topFolder\Folder 2\001.jpg

    00000004.jpg is copied from C:\Temp\topFolder\Folder 2\002.jpg

    cls
    
    $topSourceFolderName = "C:\Temp\topFolder"
    $targetFolderName = "C:\Temp\massiveFolder"
    
    #define empty PowerShell array 
    $fullyQualifiedJpgSourceFileNameList = @()
    
    #get fully-qualified file name (FullName) for all files in and below $topFolderNmae
    $fullyQualifiedJpgSourceFileNameList = @((Get-ChildItem -recurse -LiteralPath $topSourceFolderName -File) | Select -Property FullName | sort -Property FullName)
    
    #Copy each file to the single target directory, renaming each file as it is copied
    
    $targetFileNameIndex = 0
    
    foreach ($fullyQualifiedJpgSourceFileName in $fullyQualifiedJpgSourceFileNameList)
    {
        #create fully qualified target file name for this jpg file
        #format $targetFileNameIndex so it has 8 leading digits
        #https://devblogs.microsoft.com/scripting/hey-scripting-guy-how-can-i-use-leading-zeroes-when-displaying-a-value-in-windows-powershell/
        $fullyQualifiedJpgTargetFileName = ($targetFolderName + "\" + $targetFileNameIndex.ToString("00000000") + ".jpg")
    
        #increment index for next jpg file copy
        $targetFileNameIndex = ($targetFileNameIndex + 1);
    
        Copy-Item -LiteralPath $fullyQualifiedJpgSourceFileName.FullName -Destination $fullyQualifiedJpgTargetFileName -Force
    }