dir ????????.txt does not show only files with length of 8 characters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 630
Joined: 28 Jun 2010 03:46

dir ????????.txt does not show only files with length of 8 characters

#1 Post by miskox » 17 May 2024 04:22

How to list only files with filenames of 8-characters in length?

Code: Select all

@echo off
break>12345678.txt
break>87654321.txt
break>test.txt
break>123456789.txt
echo dir ????????.txt
dir ????????.txt
echo --------------
echo dir "????????.>txt"
dir "????????.>txt"
echo --------------
Output:

Code: Select all

dir ????????.txt
17.05.2024  12:19                 0 12345678.txt
17.05.2024  12:19                 0 123456789.txt
17.05.2024  12:19                 0 87654321.txt
17.05.2024  12:19                 0 test.txt
--------------
dir "????????.>txt"
 Directory of c:\temp

File Not Found
--------------

c:\temp>
See also viewtopic.php?f=3&t=6207 and viewtopic.php?f=3&t=5057

Tried different options with < and >. No luck.

My filenames are actually numbers only so maybe regex could help (dir /b *.txt|findstr ...).

Thanks.
Saso

IcarusLives
Posts: 175
Joined: 17 Jan 2016 23:55

Re: dir ????????.txt does not show only files with length of 8 characters

#2 Post by IcarusLives » 17 May 2024 07:19

I'd just check the name length directly.

Code: Select all

for /f "delims=" %%i in ('dir /b "*.txt"') do (
	set "current=%%i"
	if /i "!current:~9!" neq "" (
		echo %%~i
	)
)

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: dir ????????.txt does not show only files with length of 8 characters

#3 Post by miskox » 17 May 2024 10:51

Thanks. But it doesn't work for me. This is what I get. But your idea is good - to check length of the filename.

Code: Select all

c:\
12345678.txt
123456789.txt
87654321.txt
test.txt

c:\

IcarusLives
Posts: 175
Joined: 17 Jan 2016 23:55

Re: dir ????????.txt does not show only files with length of 8 characters

#4 Post by IcarusLives » 17 May 2024 11:46

Apologies for the oversight.

Code: Select all

@echo off & setlocal enableDelayedExpansion

for /f "delims=" %%i in ('dir /b "*.txt"') do (

	set "str=A%%~ni"
    set "len=0"
    for /L %%A in (12,-1,0) do (
        set /a "len|=1<<%%A"
        for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
	
	if !len! equ 8 (
		echo %%~i
	)
)
pause
exit

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: dir ????????.txt does not show only files with length of 8 characters

#5 Post by penpen » 17 May 2024 16:17

I would guess that you might find the short names of some files (in case that feature is activated on your system).
You might test that by forcing the dir command to also display short filenames:

Code: Select all

dir /x "????????.txt"

penpen

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: dir ????????.txt does not show only files with length of 8 characters

#6 Post by miskox » 19 May 2024 03:24

Thank you both.

Saso

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: dir ????????.txt does not show only files with length of 8 characters

#7 Post by miskox » 20 May 2024 05:05

I decided to use this solution:

Code: Select all

dir /b *.txt|(findstr /i /b /r "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt")
Thanks again for the ideas.

Saso

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: dir ????????.txt does not show only files with length of 8 characters

#8 Post by npocmaka_ » 20 May 2024 06:35

By default 8.3 notation is turned on Windows and all files have two names the short one and the real one. -> https://learn.microsoft.com/en-us/opens ... e871b71f14

with dir /x and dir /n you can list short names or long names respectively. Eventually you can turn off 8.3 notation with fsutil 8dot3name [set] { <defaultvalue> | <volumepath> {1|0}} or through the registry in the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem

Though turning of the sort names could lead to some problems...

Post Reply