2

I need to list all text files on the C drive whose names are seven characters long. I tried the following command

DIR ???????.txt

However, this command displayed files with seven characters and less. Which command could I use to only display files with seven characters?

3 Answers 3

4

The wildcard ? matches any character at most once, so dir ???????.txt will match any .txt-file with an extension preceded by at most seven characters. There is no wildcard that matches any character exactly once that dir directly supports, but the command's output can be piped into findstr, which supports regular expressions.

In this case, dir /B | findstr /R "^.......\.txt" will do the trick.

6
  • this is exactly what i'm looking for thankyou you have been a huge help
    – batsta13
    Mar 22, 2012 at 12:16
  • So then this is wrong? ss64.com/nt/dir.html "? Match any ONE character", or , microsoft.com/resources/documentation/windows/xp/all/proddocs/… , it say ? substitute one character, not "at most one".
    – Morpfh
    Mar 22, 2012 at 13:15
  • 1
    As far as I know, the ?-wildcard has matched zero or one character ever since MS-DOS. Likewise, the character ? is used in regular expressions to match the preceding element zero or one times, but after some digging, it seems there is more to it. Apperently, ? will only match zero characters when it preceeds either another wildcard, a dot or the end of a string, in Windows 7, that is. How convenient! Mar 22, 2012 at 13:39
  • I love this solution. It took me a while though to figure out how I can use it with subdirectories (dir /b /s) because it also returned files like c:\test\aa.txt (yes, test\aa has 7 characters too). I found the solution in the commandline: dir /B /s| findstr /R "\[^\][^\][^\][^\][^\][^\][^\]\.txt$"
    – ZEDA-NL
    Mar 22, 2012 at 14:06
  • @MarcksThomas Ok, thanks. Thought so, but the "documentation" I found was a bit vague/misleading.
    – Morpfh
    Mar 22, 2012 at 14:58
1

I wrote a batch file that examines the output of dir ???????.txt and echoes only the files that have 7 characters (11, including .txt).

@ECHO OFF

REM --- Call Subroutine for all files of Len 1 - 7 characters ---
For /f "delims=*" %%a in ('dir "c:\???????.txt" /b') do Call :CheckLen %%~nxa

REM --- End Batchfile ---
Goto :eof

:CheckLen
  REM --- Place Filename in Variable FNAME ---
  Set FName=%*

  REM --- IF Valiable FName <> First 10 characters of FName, FName has  ---
  REM --- 11 Characters (7 + .txt). Echo Output ---
  if NOT "%FName%"=="%FName:~0,10%" Echo %FName%

Note: In line 'dir "c:\???????.txt" /b' ,add /s if you want also to search the subdirectories of C:. This will probably take a long time without output.

1
  • thanks this seems to work as i'm new to scripting is their a simpler way of doing it.
    – batsta13
    Mar 22, 2012 at 11:51
1
DIR /b /s | findstr /r /i "\\.......\.txt$"
1
  • 1
    Welcome to Super User! Instead of just posting a line of code, please explain what it does as well. Edit your post to add more info. Thanks.
    – slhck
    Apr 23, 2013 at 3:43

You must log in to answer this question.

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