Solved Finding .txt files with 7 characters in C: drive

April 14, 2012 at 02:40:20
Specs: Windows 7
I'm trying to display all text files in the C: drive that have exactly 7 characters in the file name.

So far I'm using DIR C:\ /W /S | findstr /R "^.......\.txt" which sort of works.

It gives me all the files with 7 characters to the left, but shows up with some random files to the right of some of them, e.g:

eulaENU.txt InstallManager.cfg Language.Dat
License.txt License_Italian.txt
schemes.txt simplified.clr
Medical.txt Philosophy.txt
LICENSE.txt PATCH.ERR
AUTHORS.txt COPYING_GPL.txt
License.txt locdata.ini

Please Help!

NOTE: I don't want to use any loops, I'm very close with my current line of code, so I assume it needs tweaking in some small way


See More: Finding .txt files with 7 characters in C: drive

Report •

✔ Best Answer
April 16, 2012 at 02:08:58
Erm...
dir c:\ /B /S | findstr "^.*\\.......\.txt$" ?

AFAIK there are only two wild cards in DOS : ? and *

hth

Elinor Hurst
http://elinorhurst.blogspot.com



#1
April 14, 2012 at 04:43:41
That's because the /W option makes the file names appear on several columns.
Have you tried using /B instead?

And what your regular expression says is "starts with any seven characters, followed by .txt" - this does not ensure that's how the string ends. You should be able to fix this using the $ sign.

hth

Elinor Hurst
http://elinorhurst.blogspot.com


Report •

#2
April 14, 2012 at 04:57:36
Something like:

dir c:\ /B /S | findstr "^.*\\.......\.txt$"

hth

Elinor Hurst
http://elinorhurst.blogspot.com


Report •

#3
April 14, 2012 at 11:11:58
Why is everyone using FINDSTR?
dir c:\???????.txt /s /b

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
April 14, 2012 at 12:24:27
"Didn't know you could that" is probably why...
I guess there is a performance gain doing it the way you suggest.
Thanks for the tip.

Elinor Hurst
http://elinorhurst.blogspot.com


Report •

#5
April 15, 2012 at 12:50:47
That is also giving me txt files that are less than 7 characters in length. Any way you can negate those?

Report •

#6
April 16, 2012 at 02:08:58
✔ Best Answer
Erm...
dir c:\ /B /S | findstr "^.*\\.......\.txt$" ?

AFAIK there are only two wild cards in DOS : ? and *

hth

Elinor Hurst
http://elinorhurst.blogspot.com


Report •

#7
April 16, 2012 at 14:22:54
Yeah, my bad. I forgot WinNT's handling of the "?" wildcard is broken. Also note the above findstr will find txt files if the parent directory + file name is 6 characters (+ 1 for path separator) long.
E:\7-Zip\License.txt
E:\7-Zip\Lang\af.txt

It seems like only solution is something like this:
@echo off
for /R "c:\" %%a in (???????.txt) do call :lengthCheck "%%~Na" "%%~Fa"
goto :EoF

:lengthCheck
set _tmp=%~1
if not "%_tmp:~6,1%"=="" @echo %~2

How To Ask Questions The Smart Way


Report •

#8
April 17, 2012 at 01:48:14
Hi again,

? isn't broken: what it means is 0 or 1 characters which is why we get any number of characters up to 7. So, it does make sense, my bad for not working that one out ;-).

The OP stated (s)he wanted to avoid loops. I suggest this workaround for the problem you pointed out:
dir c:\ /b /s | findstr "^.*\\[^\\][^\\][^\\][^\\][^\\][^\\][^\\]\.txt$"

What this means is take all filenames (including paths), then select in particular those strings that start with anything and end with \xxxxxxx.txt where x can be anything but \.

Works a treat on my box.

hth

Elinor Hurst
http://elinorhurst.blogspot.com


Report •

#9
April 17, 2012 at 06:19:09
? isn't broken: what it means is 0 or 1 characters
Close, but not really. It means exactly one character, unless it precedes a period. Then it means 1 or less. For instance, ??????e.txt will return license.txt, but not readme.txt. A single wild card character matching a single character is sane, how every other OS handles it, and how Microsoft handles it most of the time. Except for when it doesn't. Asking why Windows behaves as it does is always a history lesson. In this case, the behavior mimics CP/M. You see, it stored file names in 8.3, positionally. File names would be padded with spaces to get them up to 8 characters. Raymond Chen even has a blog post on the subject.

As for why the OP doesn't want to use a FOR, I do not know. Maybe he views it as too powerful? That would explain why he would use a language like Batch.

How To Ask Questions The Smart Way


Report •

#10
April 17, 2012 at 12:06:21
Fair enough, despite this not being the habitual usage of "?" in anything resembling regexp. But I guess it is simplistic approach having only two wildcards to start with. Again, thanks for the tip - you live, you learn, right?

Re the rest, I don't think it really matters why the OP wants to use batch, but the solution offered does work satisfactorily in a single command line which is often considered more elegant even if more difficult to read/understand.

Just my two cents.

Elinor Hurst
http://elinorhurst.blogspot.com


Report •

Ask Question