Excluding files of particular extension using DIR command on windows command line

72,584

Solution 1

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$" 

Solution 2

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 :)

Solution 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] *
Share:
72,584

Related videos on Youtube

Johnydep
Author by

Johnydep

Updated on September 18, 2022

Comments

  • Johnydep
    Johnydep almost 2 years

    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?

  • JdeBP
    JdeBP over 12 years
    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.
  • Johnydep
    Johnydep over 12 years
    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.
  • phuclv
    phuclv about 7 years
    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
  • Sopalajo de Arrierez
    Sopalajo de Arrierez over 5 years
    Why findstr over just find?
  • Sopalajo de Arrierez
    Sopalajo de Arrierez over 5 years
    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).
  • IUnknown
    IUnknown over 5 years
    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.