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

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Park.BJ
  • 163
  • 1
  • 10
  • You could check [this post here](http://stackoverflow.com/q/17279114/6707985) and use the information of [this one](http://stackoverflow.com/a/15568171/6707985) and combine them to get the result I think you needed. Also I recommend you to read [the tour page](http://stackoverflow.com/tour) to learn how this site works. – geisterfurz007 Nov 23 '16 at 07: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. – soja Nov 23 '16 at 07:12

1 Answers1

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.

Melebius
  • 6,183
  • 4
  • 39
  • 52