0

I have full path ( using "%%~fl" or %%~l )

E:\Documents\Windows-Bash\CheckModels\SB3\models\TEST3\Cerus\Modbridge\Core\g-221.mdl

My base path ( according to %~dp0 ) is:

E:\Documents\Windows-Bash\CheckModels\SB3\

How can I extract:

models/TEST3/Cerus/Modbridge/Core/g-221.mdl

To search for it inside a text file.

My batch script is like follows:

echo off

set DataBase=E:\Documents\Windows-Bash\CheckModels\SB3\DB\trackassembly_init.lua
set AddModel=models_present.txt
set MisModel=models_missing.txt
set BasePath=%~dp0
set Model

dir /a-d /b /s *.mdl > %AddModel%

for /F "tokens=*" %%l in ('type "%AddModel%"') do (
  set Model=%%~l 
  :: ?[1] Process the %Model% so it becomes "models/TEST3/Cerus/Modbridge/Core/g-221.mdl"
  :: ?[2] IF (not found %Model% in the contents of %DataBase% ) THEN
  :: ?[2]    echo Model > MisModel ( Append missing file )
  :: ?[2] END
)

del %AddModel%

echo End
timeout 25

How can I replace the pseudo-code in ?[1] and ?[2] with the real thing ? I can easily convert the string using C/C++, but I do not need additional files. Is there a way the whole thing to be converted to C/C++ ?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • You seem to be confused about the terminology here. Bash is a Unix shell; it certainly exists in a Windows port as well, but the code you are showing is definitely not bash code. I edited your question to fix this apparent confusion. – tripleee Feb 10 '16 at 15:12
  • Yes, maybe. It's a "BAT" file as of Windows BATCH. A file containing dos commands telling the windows what to do... I will may be try to do the whole thin in C++ instead, but I thought that this way is more clean as is ... – Деян Добромиров Feb 10 '16 at 15:19
  • You can use the set command to do string replacement to remove the partial path of where the batch file is to your file name. – Squashman Feb 10 '16 at 15:23
  • I guess what Magoo have posted below, but somehow I cannot get it to work .. Sorry Squashman for the stupid question, but how should I do that ? – Деян Добромиров Feb 10 '16 at 15:52
  • Already made is with C++ and a batch. Yhe batch call the C++ app and done ... – Деян Добромиров Feb 13 '16 at 18:41

1 Answers1

0
@ECHO Off
SETLOCAL
SET "basepath=u:\sourcedir\t w o"
PUSHD "%basepath%"
for /F "tokens=*" %%l in ('dir /a-d /b /s *.mdl ') do (
 set "Model=%%~l"
 call set "relpath=%%model:*%basepath%\=%%"
 call set "relpath=%%relpath:\=/%%"
 CALL echo %%relpath%% FOR %%l
)
popd
GOTO :EOF

Note positioning of quotes to ensure value assigned does not include stray trailing spaces in the sourcecode (difficult to find if they exist)

basepath is the relative root for the directory scan. Change to suit yourself - I used a directory that suits my system.

The call executes a set command with the single-% variable substituted.

set "var1=%var2:string1*=string2%"

sets var1 to the value of var2, with the characters up to and including string1 replaced by string2

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • It does not work on Windows 7. "for /F "tokens=*" %%l in ('dir /a-d /b /s *.mdl ') do" outputs the same file every iteration _echo %%l _set "Model=%%l" _call echo %Model% >> %LogModel% _call set "Relpath=%%Model:%BasePath%*=%%" _call echo . >> %LogModel% _call echo %Relpath% >> %LogModel% _call echo . >> %LogModel% _call echo . >> %LogModel% When I print the model in every iteration is giving me the same file and in %LogModel% i get the exact same file. On the other hand "echo %%l" prints what is it supposed to .. – Деян Добромиров Feb 10 '16 at 15:50