Displaying file names with a certain number of characters in command line

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?

batsta13

Posted 2012-03-22T10:50:18.423

Reputation: 123

Answers

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.

Marcks Thomas

Posted 2012-03-22T10:50:18.423

Reputation: 5 749

this is exactly what i'm looking for thankyou you have been a huge help – batsta13 – 2012-03-22T12:16:50.047

So then this is wrong? http://ss64.com/nt/dir.html "? Match any ONE character", or , http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/dir.mspx?mfr=true , it say ? substitute one character, not "at most one".

– Morpfh – 2012-03-22T13:15:33.280

1As 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 – 2012-03-22T13:39:15.170

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 – 2012-03-22T14:06:47.317

@MarcksThomas Ok, thanks. Thought so, but the "documentation" I found was a bit vague/misleading. – Morpfh – 2012-03-22T14:58:24.447

maybe empty string is a character for them. but they should write it better and give 2-3 examples along with find str – tgkprog – 2013-05-04T08:33:33.253

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.

ZEDA-NL

Posted 2012-03-22T10:50:18.423

Reputation: 418

thanks this seems to work as i'm new to scripting is their a simpler way of doing it. – batsta13 – 2012-03-22T11:51:58.673

1

DIR /b /s | findstr /r /i "\\.......\.txt$"

Lester

Posted 2012-03-22T10:50:18.423

Reputation: 13

1Welcome 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 – 2013-04-23T03:43:17.657