3

I have a bunch of files in a directory (with sub directories) with similar names except the last digit is a different number. I would like to keep the version with the largest file size. However some files will not have any duplicates but I do need to keep that file.

files will look like

111~1.mp4    (1mb)
111~2.mp4    (5mb)
111~3.mp4    (2mb)

222~1.mp4    (3mb)

333~1.mp4    (2mb)
333~2.mp4    (4mb)

444~1.mp4    (1mb)
444~2.mp4    (5mb)
444~3.mp4    (3mb)
444~4.mp4    (7mb)

I would like to keep only the largest version size.

111~2.mp4    (5mb)

222~1.mp4    (3mb)

333~2.mp4    (4mb)

444~4.mp4    (7mb)

Im afraid I got stumped, Ive been searching but havent been able to get it going what I have done so far is able to get the size of the files

set "filename=*.*"
for %%A in (%filename%) do echo.Size of "%%A" is %%~zA bytes

but now I have to compare the duplicates to each other and delete the smaller size version and only keep the larger size version and if their is no duplicate than keep that version.

0

1 Answer 1

4

This should do the job:

@ECHO OFF
SETLOCAL EnableDelayedExpansion



REM **************************************************

REM Source directory
SET source=C:\adjust\path\to\folder

REM Set folder name
SET folder_name=folder

REM **************************************************



REM Creating a new directory to sort out files
IF NOT EXIST "%source% TEMP" MD "%source% TEMP"

REM Sorting out files without duplicates
FOR /F "tokens=1,* delims=~" %%A IN ('DIR /S/B/A-D "%source%"') DO (
    IF NOT EXIST "%%~fA~2%%~xB" COPY "%%~fA~1%%~xB" "%source% TEMP\%%~nxA~1%%~xB" >nul 2>&1
)

REM Sorting out files with biggest size
FOR /F "tokens=1,* delims=~" %%F IN ('DIR /S/B/A-D "%source%"') DO (
    SET path=%%~dpF
    SET name_1=%%~nF~
    FOR /F "delims=" %%A IN ('DIR "%%~fF*" /S/B/O:-S') DO (SET biggest=%%A && CALL :copy)
)

REM Deleting all duplicates
RD /S /Q "%source%"

REM Renaming TEMP to source
REN "%source% TEMP" "%folder_name%"
CLS
ECHO.
ECHO  Done^^!
ECHO.
PAUSE



:copy
SET name_2=%biggest:*~=%
COPY "%biggest%" "%source% TEMP\%name_1%%name_2%" >nul 2>&1 && DEL "%path%%name_1%*" >nul 2>&1
EXIT /B

Bare in mind: this will delete the entire folder and then rename the temporary folder accordingly. If you have any files which do not have a ~ in their name then those files are going to be deleted!!!

1
  • @JuanLopez Glad I could help! :) Commented Feb 3, 2018 at 16:40

You must log in to answer this question.

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