简体   繁体   中英

Windows command line search for exact extension with dir

When I do a search:

dir /b /s *.txt

I get all files/folders with the extension .txt . But I also get them when they have an extension like .txtx . How can I search for the exact extension?

you can try this

dir  /s /b *.txt | findstr /v .txt.

or

dir  /s /b *.txt | findstr /e .txt

or

dir  /b *.txt | findstr  .txt$

I can't figure out why it behaves like this, but this works: dir /b /s .txt | findstr /e .txt dir /b /s .txt | findstr /e .txt . Ugly but works.

dir C:\XML /s /b | findstr /e.xml > C:\XML\list.txt

this works for me, just change the.xml extension to.txt

Splitting the commands for better understanding:

dir /s /b 
     ^  ^
     |  |
     |  Displays a **bare** list of directories and files, with no additional information.
     |
      ~ Lists every occurrence of the specified file name within the 
        specified directory and all **subdirectories**.


findstr /e
        ^
        |
        Matches the text pattern if it is at the **end** of a line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM