0

I am trying to create a list of all files contained in the directory and subdirectories of %dir%, which contain "."s in their filename (not including the extension "."). I have tried the following line:

dir "%dir%\*.*.*" /a-d /b /s /-p

but that returns every file, as if I had just used the mask "asterisk.asterisk". I have also tried the masks "asterisk.asterisk.???" and "asterisk.?asterisk.asterisk" with no luck.

Is this possible using just the dir command?

If the answer is no and I would have to use for loops and / or pipe outputs to findstr commands, then I can probably do this myself, but I wanted to ask first, just in case there is some blindingly obvious file mask or pattern trick, or other trick to achieve my aim with just a dir command.

EDIT I had to write "asterisk" literally in some places above as for some reason the symbol wasn't showing up in the posted message.

9
  • "Is this possible using just the dir command?" No
    – DavidPostill
    Commented Mar 12, 2022 at 15:42
  • 1
    It’s so much easier to do things like this with powershell. Commented Mar 12, 2022 at 15:45
  • 2
    @barlop that’s because you’re not realizing the benefits of a modern, object oriented scripting language with all the power and functionality of the .net framework. Clearly you’re not trying to do anything complex so of course it is “simpler.” Once you have to do something “complex” those old command interpreters make it extremely complex if not impossible. Same reason if anybody wants to do anything complex on Linux they switch to python scripts. Commented Mar 12, 2022 at 16:27
  • 1
    @barlop powershell is very different because it was designed based on lessons on previous shells. As a result it's much more powerful. It's also available for other platforms. Even if you install GNU tools to Windows you'll still be limited to the cmd's legacy issues
    – phuclv
    Commented Mar 12, 2022 at 16:36
  • 2
    Does this answer your question? Why does `dir *.*` give me all files and folders?
    – phuclv
    Commented Mar 12, 2022 at 16:37

2 Answers 2

1

This batch one-liner will do it :

dir /b | findstr "[^.]*\.[^.]*\.[^.]*"

Explanation :

  • [^.]* - zero or more characters that are not dots
  • \. - the escaped dot character

For more information see findstr.

0

DIR probably won't do it on its own

You certainly wouldn't need to use loops!!!!

You just need a list of filenames (which you can get from DIR /b... >a.a), redirecting output to a file eg a file called a.a, and apply a regex to them.

Lots of programs use regexes and let you test the regex.. Notepad++ probably supports regexes and is free. I use JGSoft editpad pro sometimes.

Lots of useful programs use regexes, like regex renamer, or powergrep.. regex coach helps to put together a regex. Even grep of course uses regexes.. It's good to know regexes!

If you want to figure out a regex from the command line then using echo and grep is a good idea. e.g. echo abc | grep ab

C:\blahblah>dir /b >a.a

C:\blahblah>type a.a
gfgdfgdf.doc
sdfs.ewerw.txt

C:\blahblah>grep -P "[^.]+\.[^.]+\...." a.a
sdfs.ewerw.txt

C:\blahblah>

That lists all filenames that match the pattern of some sequence of one or more characters that aren't a dot, followed by a dot followed by some sequence of one or more characters that aren't a dot. Followed by a dot, followed by three characters.

You would have to get grep, mine seems to be a fairly recent version I got from cygwin

C:\blahblah>grep --version
grep (GNU grep) 3.7
Packaged by Cygwin (3.7-2)
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others; see
<https://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.

C:\blahblah>

Another answer mentions findstr, that might better for you as it's native. Though note that findstr doesn't support PCRE(perl compatible regular expressions), so some of its regex options are a bit limited. e.g. PCRE includes support of looking for \d{3} which would match 3 consecutive digits. Or lookahead/lookbehind. (?=1). So if you are interested in regexes, findstr is limiting.

You must log in to answer this question.

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