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.

TechHorse
  • 357
  • 1
  • 6
  • 17

2 Answers2

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.

harrymc
  • 498,455
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.

barlop
  • 25,198