Search code examples
windowsvariablesbatch-filedos

Save variable while in a "for"


Concerning the Windows batch files: Is there a way to have a variable with all pathes leading to my folders "src" followed by *.cc, it should look like this when I print it :

path1\src\*.cc path2\src\*.cc etc.

I've found this :

for /d /r . %%g in (src) do echo %%g

but it print

path1\src
path2\src
etc.

on the standard output, because of the echo %%g.

EDIT

I need to have the result in a variable, not print on the screen (std out). It must be path_to_folder1\*.cc path_to_folder2\*.cc [...].

Is it possible ?

EDIT 2

I'm actually with the following code :

setlocal enabledelayedexpansion
for /d /r c:\ %%g in (Desktop*) do (
    set APLLIS_SRC= !APPLIS_SRC! %%g\*.cc
)
endlocal

which give me this out put if I do in the for echo path: !APPLIS_SRC!:

path:
path:
[...]
Press any key to continue...

So my variable is empty... Why ?

OS: Windows Server 2008 SP2


Solution

  • setlocal enabledelayedexpansion
    set "myvar="
    for /d /r . %%g in (src) do set "myvar=!myvar! %%g"
    
    echo %myvar%
    echo %myvar:~1%
    

    (untested)

    Note that 'myvar' will contain a leading space.