Park.BJ
Park.BJ

Reputation: 163

Replace sub-string in a FOR loop

I want specific String later Path,

set PROJECT_FOLDER_PATH=E:\aaa\bbb\cccc
FOR /f %%i IN ('dir /b /s %PROJECT_FOLDER_PATH%\src\*.c') DO echo %%i:bbb\=% 

Expected Result: cccc\src\test.c

but current Result: E:\aaa\bbb\cccc\src\test.c:bbb\=

Please, help me

Upvotes: 2

Views: 2586

Answers (1)

Melebius
Melebius

Reputation: 6695

You cannot use the replace syntax on a parameter-style variable like %i. First store the value to a normal variable using set.

setlocal EnableDelayedExpansion

set PROJECT_FOLDER_PATH=E:\aaa\bbb\cccc
FOR /f %%i IN ('dir /b /s %PROJECT_FOLDER_PATH%\src\*.c') DO (
    set TMP_PATH=%%i
    echo !TMP_PATH:bbb\=!
)

We need Delayed expansion of variables to set and read the variable in the same loop. Otherwise, the variable in echo command would be expanded before entering the loop.

Upvotes: 5

Related Questions