Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to write a batch file which will run a command for files with .ext extension excluding "duplicated" files which have same filename but two different extensions (e.g. .ext and .txe).

I tried this but I don't know how to exclude a file with same filename as found .txe file:

@echo off 
forfiles /c "cmd /c if @ext==\"ext\" 
    if not @ext==\"txe\" 
     echo @fname.ext" 
share|improve this question
Great story, what's the question? :) What have you got so far? Where are you getting stuck? – Ƭᴇcʜιᴇ007 Mar 17 '13 at 19:03
@techie007 I tried this but I don't know how to exclude a file with same filename as found .txe file. '@echo off forfiles /c "cmd /c if @ext==\"ext\" if not @ext==\"txe\" echo @fname.ext" pause' – Koboos Mar 17 '13 at 19:30
up vote 1 down vote accepted

The script below will traverse the current directory looking for files matching the pattern *.ext. For every file found, it will execute the command dir example.* (the modifier ~n strips the extension off %%i). The output of this dir command is piped into findstr to get the line indicating the number of files found. for /F parses this line and stores the number in %%j.

If 'duplicate' files with a different extension were found, this number would be greater than one. If the number is one, we're dealing with a non-duplicate and can start running commands on %%i. In this example, the script merely prints the filename using echo %%i.

@echo off
for %%i in (*.ext) do (
    for /F %%j in ('dir %%~ni.* ^| findstr /C:"File(s)"') do (
        if /I %%j EQU 1 (
            echo %%i
share|improve this answer
It looked complicated at first but after analysing it's clear for me. And of course it does the job perfectly. Thanks @MarcksThomas – Koboos Mar 17 '13 at 19:57

You must log in to answer this question.

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