29

if i want to see e.g. files of a particular extension only using dir listing, i can do that using the command:

DIR *.txt 

And it shows all files with .txt extension.
Now i want to know is there any command with wich i can exclude certain extensions?
For example, i don't want to see any file with extension .exe, how can i do that?

4 Answers 4

39

DIR wont allow what you are trying to do. However DIR along with FINDSTR can solve this.

e.g. The following ignores all .txt files in the DIR listing.

dir | findstr /v /i "\.txt$" 
3
  • 4
    you may want to use dir /b | findstr /v /i "\.txt$" because dir will output the header and the possible incorrect files and folders count at the end
    – phuclv
    Commented Apr 6, 2017 at 4:08
  • Why findstr over just find? Commented Jan 1, 2019 at 16:59
  • 2
    Because find searches for text and findstr supports regexes I suggested Findstr. If you just want to search an exact string find would work fine too.
    – IUnknown
    Commented Jan 2, 2019 at 11:37
14
dir /B | find /V ".txt"

This would list all files and find would filter out anything that doesn't contain ".txt". It's far from perfect, but maybe it's enough :)

1
  • 1
    No need for /B, I would say, but I prefer this answer as long as I don't see the need for 'findstr` (the actual accepted one). Commented Jan 1, 2019 at 17:00
3

It depends on your command interpreter.

Microsoft's cmd doesn't have such a facility, as you can see from the other answers where one has to post-process the output of dir. However, the tool from JP Software's TCC/LE has this feature. It is called a file exclusion range and is used like this for the example in your question:

dir /[!*.exe] *

3
  • Thank you, but i think this tool run as standalone program. I am making my application where i run external process using windows shell or bash in ubuntu, so this tool will not fit, If they provide cmd line switches then it will be useful.
    – Johnydep
    Commented Jan 30, 2012 at 0:01
  • 1
    The aforegiven clearly is a command-line switch. Read the hyperlinked documentation. Of course, in most programming languages it is fairly silly to go to the length of using the shell for obtaining directory contents, and you are on the wrong StackExchange for writing applications.
    – JdeBP
    Commented Jan 30, 2012 at 8:09
  • thanks for the explanation, that's true but it is a workaround for scanning those directories which require Elevation and i don't want to make my code trigger UAC prompt, when i can get results from cmd prompt without requiring higher privilege.
    – Johnydep
    Commented Jan 31, 2012 at 16:21
0

For this purpose, define a macro using DOSKEY. I excluded directories and ignored case – YMMV. Also, /B seems like the most reasonable DIR option for filtering, /D and /W being inappropriate for the task, and the long formats /N (default) and /X containing, in some cases, additional text after the filename (for symlinks). Check it out on the command line:

DOSKEY  FIR=DIR/B/A-D^|FINDSTR /VIE $*
DOSKEY FLIR=DIR/X/A-D^|FINDSTR /VIE $*

FIR for filtered DIR, get it? :) And FLIR for the long version if needed. Then use it like this:

fir .jpg
flir jpg
fir "jpg htm"

The latter is because of how FINDSTR reads patterns supplied as arguments, as documented in the /?. Only the first argument is treated as a pattern, additional ones are interpreted as file names, even when running in a pipe.

I prefer FINDSTR over plain FIND because it has the funky /E switch which filters only at the end of the line, which seems preferable when filtering by file extension (but again, YMMV).

The handy SS64 site explains how to set up macros in a TXT file for permanent reuse. Beware, no caret ^ to escape special characters for macros stored in a text file.

To understand the basics of how macros ("Console aliases") work under the hood, read Eryk Sun's brief comments on SO.

You must log in to answer this question.

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