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
  • 269
  • 2
  • 13
  • "Is this possible using just the `dir` command?" No – DavidPostill Mar 12 '22 at 15:42
  • 1
    It’s so much easier to do things like this with powershell. – Appleoddity Mar 12 '22 at 15:45
  • @Appleoddity Much easier to use standard linux commands that have been around for decades, incorporated into the windows command line. e.g. there is a grep implementation for windows . The good small linux commands used appropriately, are a lot like the old dos or cmd prompt commands anyway.. Powershell is very different. – barlop Mar 12 '22 at 16:11
  • 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. – Appleoddity Mar 12 '22 at 16:27
  • use `dir` in powershell instead (which is an alias to Get-ChildItem) and throw the legacy cmd away. It'll match the dot as expected – phuclv Mar 12 '22 at 16:33
  • @Appleoddity Look, things have their purposes/uses , for some things you can use C# and Visual Studio. That will do many things that would be very difficult in python. And there are a bunch of scripting languages you can use if you like scripting languages.. Nobody is suggesting to always use command line and no scripting and no programming ever, and to limit yourself to just that!! – barlop Mar 12 '22 at 16:34
  • 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 Mar 12 '22 at 16:36
  • 2
    Does this answer your question? [Why does \`dir \*.\*\` give me all files and folders?](https://superuser.com/questions/1193102/why-does-dir-give-me-all-files-and-folders) – phuclv Mar 12 '22 at 16:37
  • @phuclv powershell is obviously an improvement on batch files but nobody is suggesting to use a batch file for this. – barlop Mar 12 '22 at 16:44

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
  • 455,459
  • 31
  • 526
  • 924
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
  • 23,380
  • 43
  • 145
  • 225