This is unfortunately not possible with only the DIR
command, however it can be combined with the FINDSTR
command.
I'm focusing on the more general question, rather than the specific question regarding .exe
files, but you can easily modify this to narrow it down.
The FINDSTR
command supports regular expression searches on the output which allows us to filter out files with more than one period in the filename.
DIR /B | FINDSTR /V /R "[^\.]*\.[^\.]*\.[^\.]*$"
I would suggest wrapping into a FOR
loop as follows to do something with the returned file list:
FOR /F "tokens=*" %%a IN ('DIR /B ^| FINDSTR /V /R "[^\.]*\.[^\.]*\.[^\.]*$"') DO ECHO %%a
Breakdown
In this explanation, <...>
is a placeholder for things that will be explained later. It's only there to simplify.
FOR /F "tokens=*" %%a IN ('<...>') DO ECHO %%a
Using "tokens=*"
or "delims="
in your FOR
loop allows processing filenames with spaces. Replace ECHO %%a
with whatever command(s) you want to run.
DIR /B
Using the /B
switch on the DIR
command uses bare format, including only the filenames. This is required for the FINDSTR
command to function as we want.
^|
It is important to escape the |
character when used inside the command portion of a FOR
loop. Otherwise you'll get a syntax error because it thinks you're trying to pipe the incomplete FOR
loop into something else.
FINDSTR /V /R "<...>"
The /V
switch tells FINDSTR
to remove matches from the list and the /R
switch turns on Regular Expression mode.
[^\.]*\.[^\.]*\.[^\.]*$
[^\.]*
will match any string of characters that do not include the .
character.
\.
will match the .
character.
$
is the "end of line" character, meaning that this regular expression must be at the end of the filename. This allows us to match files with any number of periods in the filename such as test.vhost.exe.bak
.
Further Reading