Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer
    
this is exactly what i'm looking for thankyou you have been a huge help –  batsta13 Mar 22 '12 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 '12 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! –  Marcks Thomas Mar 22 '12 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 '12 at 14:06
    
@MarcksThomas Ok, thanks. Thought so, but the "documentation" I found was a bit vague/misleading. –  Morpfh Mar 22 '12 at 14:58

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.

share|improve this answer
    
thanks this seems to work as i'm new to scripting is their a simpler way of doing it. –  batsta13 Mar 22 '12 at 11:51
DIR /b /s | findstr /r /i "\\.......\.txt$"
share|improve this answer
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 '13 at 3:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.