2

I have a loop that loops over files, gets the actual file name (not including the whole path) and tries to check if that file name exists in some list. the code I have is:

setlocal enabledelayedexpansion enableextensions
for /R %%j in (*.c) do (
set MYVAR=%%j
set actualFileName=%%~nj
if NOT "%MY_FILE_LIST%"=="!MY_FILE_LIST:%actualFileName%=!" set "TOCOMPILE=!TOCOMPILE! %MYVAR%"

this code does not work since the actualFileName is accessed with % instead of !. but !MYVAR:~!actualFileName!! doesn't work either. what can i do?

1

2 Answers 2

1

You can directly use the for replaceable parameter modifiers to get the needed information

for /r %%j in (*.c) do (
    echo actualFileName is %%~nj
)

Where %%~nj is the name, without extension, of the file being referenced in %%j. See for /? for the full list

edit to adapt to comments

setlocal enableextensions enabledelayedexpansion

for /r %%j in (*.c) do (
    if not "!MY_FILE_LIST:%%~nj!"=="%MY_FILE_LIST%" (
        set "TOCOMPILE=!TOCOMPILE! "%%~fj""
    )
)

This should do the job IF no file contains a exclamation in its name. If this can not be ensured

setlocal enableextensions disabledelayedexpansion
for /r %%j in (*.c) do (
    setlocal enabledelayedexpansion
    if not "!MY_FILE_LIST:%%~nj!"=="%MY_FILE_LIST%" (
        for /f "delims=" %%a in ("!TOCOMPILE! ") do (
            endlocal
            set "TOCOMPILE=%%a"%%~fj""
        )
    ) else endlocal
)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for that!, but my issue now is at the next step. i've edited the question.
1
setlocal enabledelayedexpansion enableextensions
for /R %%j in (*.c) do (
 set MYVAR=%%j
 set lastIndex= some calculation to find last index of /


 for /f "tokens=* delims=" %%# in ("!lastIndex!") do (
    set actualFileName=!MYVAR:~%%#!
 )

)

use another nested FOR

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.