Wildcard for receiving files with exact extension

When diagnosing a larger batch of script that should process files with the extension *.log, I found a funny behavior. In the sample directory with these files:

bar.log
foo.log
foo.log.ignore
foo.log.log-1676521099
not-related

... my little test script:

@echo off
setlocal enabledelayedexpansion
set DEF_LOG="C:\test\*.log"
for %%i in (%DEF_LOG%) do (
    echo %%i
)

... prints this:

C:\test\bar.log
C:\test\foo.log
C:\test\foo.log.log-1676521099

Digging deeper, I discovered how Windows templates were created:

C:\>dir "C:\test\*.log" /b
bar.log
foo.log
foo.log.log-1676521099

My question is: how can I list all the files that end with exactly .log?

+4
source share
3 answers

. , , 8.3, , 8.3. 3 , . dir /x "C:\test\*.log", .

, .

- DIR/B, FINDSTR. , FOR, , !. ! .

@echo off
pushd "C:\test"
for /f "eol=: delims=" %%F in ('dir *.log^|findstr /lie ".log"') do echo %%~fF
popd
+3

- %% ~ x .

@echo off&cls
for /f "delims=" %%a in ('dir /b') do if /i "%%~xa"==".log" echo %%a
pause
+5

Use the internal scan loop to get only files with the desired extension

@echo off
setlocal enabledelayedexpansion

rem set desired extension for additional filter
set extn=.log
set DEF_LOG="C:\temp\*.log"

for %%i in (%DEF_LOG%) do (
    rem if file extension is equal to our ext
    if "%%~xi"=="%extn%" echo %%i
)
+2
source

All Articles