I am trying to rename some MP4 files based on file size of mp4 files in another directory. I want to name all files with identical sizes to same name. Meaning if the file size of the source file matches the size of file in the comparison directory, the source file is renamed to whatever the compared file is named. Because both directories need to be read recursively I'm thinking it would be easier to make a list for comparison with the info in it in 2 columns by using the DIR /s /b echo %%~zs>>filesizelist.txt command giving me a list like

123456789 movie.mp4

987654321 movie2.mp4

Then pass all source mp4s to the batch file and if %%~za matches a value in first column then ren the file to the corresponding filename. Is this the best path? I tried to script it to work on the fly and that was both a no-go and the source of my 3 day headache(plus the reference list rarely changes and is obviously easily updated). Can someone please assist me with the script?

  • Have you considered powershell? Whilst I can see it should be possible via batch I still think powershell would be less work and would achieve exactly what you want with future possiblities...
    – CraftyB
    Oct 10, 2018 at 22:41
  • Thank you for the quick reply. I thought about powershell, but I am thoroughly unfamiliar with it. I can comprehend, but which commands should I research to try to write said script? Oct 11, 2018 at 3:45

1 Answer 1


Please see the below powershell script.

I have tested this against txt files only but it should work with any file providing that the file size/length is readable.

$Source = "C:\Test"
$Comparison = "C:\Test2"

$SourceFiles = Get-ChildItem -File -Path $Source | Select *
$ComparisonFiles = Get-ChildItem -File -Path $Comparison | Select *

Foreach ($File in $SourceFiles){
    Write-Host "Checking for" $File.Name
    Foreach ($CFile in $ComparisonFiles){
        if ($File.length -eq $CFile.length){
            Write-host $File.fullname "Matches `n" $CFile.fullname
            Rename-Item -Path $File.FullName -NewName $CFile.Name -WhatIf
            Write-Host "`n"

Change the path's of $Source and $Comparison to their respective directories, this is currently safe to run and will not make any changes initially. Once you have ran it and are happy with the results you can remove "-Whatif" from rename-item function and this will make the changes specified when you first run it.

This is a very quick example and does not take into consideration how to handle multiple files with the same size (Possible duplicate files etc), this only does the parent directory and not recursive.

  • Thank you. I will test and report back as soon as I get power since the hurricane has knocked out mine. I think I can read up a bit on Powershell in order to get it to be recursive. Thanks for the kick start! Oct 11, 2018 at 17:09
  • Works perfectly! Thanks a million! After some quick research I added - recurse after the $source and $comparison to achieve all my goals (for anyone reading this question with similar aims). I think brackets may have broken it since it failed on two files but once I deleted [crap] from the filename it executed fine. I can remedy that manually but I think I will do more powershell research to find the culprit. Elegant simple script you wrote. Thanks again! Oct 12, 2018 at 6:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .