Page 1 of 1

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

Posted: 17 May 2024 04:22
by miskox
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

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

Posted: 17 May 2024 07:19
by IcarusLives
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
	)
)

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

Posted: 17 May 2024 10:51
by miskox
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:\

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

Posted: 17 May 2024 11:46
by IcarusLives
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

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

Posted: 17 May 2024 16:17
by penpen
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

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

Posted: 19 May 2024 03:24
by miskox
Thank you both.

Saso

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

Posted: 20 May 2024 05:05
by miskox
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

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

Posted: 20 May 2024 06:35
by npocmaka_
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...