Is absent "!" string when EnableDelayedExpansion

When I included DelayedExpansion in the script, it does not output "!" string in the file name. For example:

Original
File01-TEXT! .Txt

Echo Outside
File01-text.txt

I think this is because setlocal EnableDelayedExpansion, but I canโ€™t delete because I need it.

@echo off
setlocal EnableDelayedExpansion

cd "C:\Files"
for %%a in (*.txt) do (

    REM Here the problem...
    echo %%a

    set "str=%%a"
    set new_str=!str:0,3!
)

echo %new_string%

pause >nul
+1
source share
1 answer

Depending on the actual code, you can disable the pending extension, enable it when access to the modified content is required, and then disable it again

@echo off
    setlocal enableextensions disabledelayedexpansion

    cd "C:\Files"
    for %%a in (*.txt) do (
        set "str=%%a"

        rem Option 1 
        echo file: %%a
        setlocal enabledelayedexpansion
        echo substring: !str:~0,3!
        endlocal 

        rem Option 2 - capture changed value to use inside non delayed expansion context
        setlocal enabledelayedexpansion
        for %%b in ("!str:~0,3!") do (
            enlocal
            echo %%a -- %%~b
        )
    )
+3
source

Source: https://habr.com/ru/post/1612777/


All Articles