Humberto Freitas
Humberto Freitas

Reputation: 483

List all files in a directory except for an extension

I wonder how do I show all files in a directory with the exception of an extension.

Example: .bat


My code:

Dir /ah-d /b /s "%temp%" >>"Temp.TxT"

Result:

Folder123
image.jpg
TESTE.bat
abc.TMP

Result I want:

Folder123
image.jpg
abc.TMP

Note: The other files may vary extensions.

Upvotes: 0

Views: 190

Answers (3)

Jason Faulkner
Jason Faulkner

Reputation: 6568

You can do this from the command line (no batch file needed):

FOR /F "usebackq tokens=* delims=" %A IN (`DIR /B /S "%temp%"`) DO (IF /I NOT "%~xA"==".bat" ECHO %A>>Out.txt)

The key piece of this is the %~xA part which will return the file extension of the file being processed. This is then compared to the extension you want to exclude (in this case .bat) and if it does not match, then the filename is written to the output.

Note that in your example, you are showing directories being returned so my DIR command does not exclude them. Additionally, since you are using the /S switch, then the full path will be returned. If you only want to get the file name part, just replace:

ECHO %A

With:

ECHO %~nxA

Upvotes: 0

dbenham
dbenham

Reputation: 130849

You can filter the output by piping to FINDSTR, rejecting lines that end with your extension.

The /L option treats the search as a literal search (as opposed to a regular expression). The /E option only matches if at end of line. The /I option makes it case insensitive. The /V option inverts the search, keeping lines that don't match, filtering out the lines that do match

dir /ah-d /b /s "%temp%" | findstr /live .bat >>"temp.txt"

Upvotes: 2

Serenity
Serenity

Reputation: 433

This picks up specific extensions - none, jp(eg|g), and tmp.

dir /ah-d /b /s "%temp%\*." "%temp%\*.jp*" "%temp%\*.tmp"

To exclude pipe the output

dir /ah-d /b /s "%temp%\*.*" | Findstr /i /v /c:".bat" >outputfile

Upvotes: 0

Related Questions