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?

one
  • 511
  • 5
  • 17
  • possible duplicate of [How to split the filename from a full path in batch?](http://stackoverflow.com/questions/9252980/how-to-split-the-filename-from-a-full-path-in-batch) – Ondrej Tucny May 10 '15 at 09:55

2 Answers2

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
)
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • thanks for that!, but my issue now is at the next step. i've edited the question. – one May 10 '15 at 10:35
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

npocmaka
  • 55,367
  • 18
  • 148
  • 187