How can I list file which's name contains exclamation marks in dos batch with the delay expansion enabled?

batch-file

Solution

@ECHO OFF
    setlocal enableextensions disabledelayedexpansion

    FOR /F "tokens=* delims=" %%a IN ('dir /b *.txt') DO (
        set /a "count+=1"
        set "file=%%a"
        setlocal enabledelayedexpansion
        echo !count!:!file!
        endlocal
    )

    endlocal

Problem

I got two txt files "test-exclamations!!!.txt", "test-normal.txt" and a bat file contains the script below in the same folder. ``` ECHO OFF & CLS SETLOCAL ENABLEDELAYEDEXPANSION FOR /F "tokens=* delims=" %%a IN ('dir /b *.txt') DO ( set /a count=count+1 echo !count!:%%a ) ENDLOCAL EXIT/B ``` and I get the result: 1:test-exclamations.txt 2:test-normal.txt We can see the exclamation marks disappeared. I've got some method to keep the exclamation marks from the Internet, but I cannot keep both the exclamation marks and the value of variable "count". Please tell me how to keep the both.

Original source

Related problems