'Is it possible to check if a two files shares the same name using batch file?

I have here a example directory that contains files with the same name but different extensions.

- MAINFOLDER  
  ˪ TEST.JPG
  ˪ TEST.PNG
  ˪ RANDOM.ZIP
  ˪ RANDOM.MP4
  ˪ UNKNOWN.MP3
  ˪ UNKNOWN.DOC

Is it possible to automatically identify if two files share the same name? For example; The TEST.jpg and TEST.png have the same name but different extensions. I need the batch file to identify these scenarios.



Solution 1:[1]

Nice challenge.

@echo off
setlocal enabledelayedexpansion
attrib -a *
for %%a in (*) do (
  for /f "delims=" %%b in ('dir /b /a-d-a "%%~na.*" 2^>nul^|find /c /v ""') do set count=%%b
  if !count! gtr 1 (
    echo There are !count! files named %%~na.*:
    dir /b /a-d-a "%%~na.*"
    attrib +a "%%~na.*"
  )
)
attrib +a *

Note 1: this has problems with file names containing exclamation marks.
Note 2: this uses the 'archive' attribute to avoid doublets (shouldn't be a problem, as this attribute is rarely used (at least in a home environment), but you should be aware of that)

How it works (simplified):

  • remove the archive attribute from all files
  • for each file do
  • list all files with that basename (%%~na.*) and count them
  • if the count is greater than 1, list those files and...
  • ... set the archive attribute to avoid processing those files again
  • set the archive attribute for all files (it wasn't set for files with "count=1")

Solution 2:[2]

Here's an alternative that doesn't change file attributes. REMarks are included to try to explain what's going on.

It doesn't address the renaming aspect of your question, as I haven't yet applied my brain to this. I suppose the need for this to be batched is determined by how many duplicate files you're dealing with.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM Loop over (DIR)ectory list of (/B)are format files (/A-D)
FOR /F "tokens=*" %%A IN ('DIR /A-D /B') DO (
  REM Have we already made a check for this file?
  IF NOT [!NEW!]==[!OLD!] (
    REM Again, use DIR, this time to list the instances of the named file and pipe to FIND for (/C)ounting
    REM Then set the variable COUNT with the count value returned by FIND
    REM %%~nA is a variable modifier that returns the filename without extension. The ~ strips surrounding quotes
    FOR /F "tokens=*" %%B IN ('DIR /A-D /B "%%~nA.*" ^| FIND /C "%%~nA."') DO SET "COUNT=%%B"
    REM If 2 or more files counted, build an output line
    IF !COUNT! GEQ 2 ( 
        REM Funny SET syntax used to prevent newlines
        ECHO | SET /p=File '%%~nA' counted !COUNT! times [ 
        REM Again, use DIR, this time to print the instances of the named file
        FOR /F "tokens=*" %%C IN ('DIR /A-D /B "%%~nA.*"') DO ECHO | SET /P='%%C' 
        ECHO ]
    )
    SET "OLD=%%~nA"
   )
   SET "NEW=%%~nA"
)

Sample output:

File '1 of 2' counted 2 times [ '1 of 2.doc' '1 of 2.rtf' ]
File '1' counted 2 times [ '1.rtf' '1.txt' ]
File '2' counted 4 times [ '2.aac' '2.flac' '2.mp3' '2.mp4' ]
File 'logitech speakers' counted 2 times [ 'logitech speakers.odt' 'logitech speakers.rtf' ]
File 'paddle - Copy' counted 4 times [ 'paddle - Copy.bmp' 'paddle - Copy.doc' 'paddle - Copy.rtf' 'paddle - Copy.vbs' ]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jimadine