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?
Answer
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.
No comments:
Post a Comment