2

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

2
  • You could check this post here and use the information of this one and combine them to get the result I think you needed. Also I recommend you to read the tour page to learn how this site works. Commented Nov 23, 2016 at 7:10
  • Is test.c a folder? Is it supposed to be in PROJECT_FOLDER_PATH? What is the purpose of the =? One way to do this would be to get the name of the parent folder before the FOR /f, like this: FOR %%a in (%PROJECT_FOLDER_PATH%) do SET parent=%%~na. Then you can use parent and a similar method to get the file name from the next FOR, and put them together. Commented Nov 23, 2016 at 7:12

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.