I am trying to create a small script to do light template replacement duties, but I'm getting stuck with dereferencing a variable the way I want.
Here's my template replacement batch file:
@echo off
echo ------------
echo %~nx0%
SETLOCAL ENABLEDELAYEDEXPANSION
SET "src=%~1"
SET "dst=%~2"
ECHO %src%
FOR /F "tokens=1,2* delims=%%" %%i IN (%src%) DO (
IF "%%k"=="" (
ECHO %%i >> %dst%
) ELSE (
SET "first=%%i"
SET "middle=%%j"
SET "last=%%k"
SET "replace=!middle:~2,-1!"
IF NOT "!first:~-1!"=="<" (
ECHO %%i >> %dst%
) ELSE IF NOT "!middle:~0,1!"=="=" (
ECHO %%i >> %dst%
) ELSE IF NOT "!last:~0,1!"==">" (
ECHO %%i >> %dst%
) ELSE (
ECHO !first:~0,-1! ^<^< !replace! ^>^> !last:~1!
)
)
)
ENDLOCAL
GOTO :EOF
:Error
EXIT /B 1
an example input file might look like:
{
"name": "<%= comp.name %>",
"version": "<%= comp.version %>",
"description": "<%= comp.description %>",
"author": "me",
"url": "https://localhost/<%= comp.name %>"
}
and an example call might look like:
SET comp.name=TestApp
SET comp.version=1.0
SET comp.description=The most awesome thing you will ever see
CALL TemplateReplacement.bat %1 %2
example output I want to see would be:
{
"name": "TestApp",
"version": "1.0",
"description": "The most awesome thing you will ever see",
"author": "me",
"url": "https://localhost/TestApp"
}
when I get into the ELSE, I get output like:
"name": " << comp.name >> ",
"version": " << comp.version >> ",
"description": " << comp.description >> ",
(note: I'm purposefully echoing the replacement situation to the console vs. the file since I'm debugging; that is reflected in the output above).
!replace! is correct; it's the name of a variable I want to expand, so I tried !!replace!!, since I expected !replace! to dump something like comp.name and then !comp.name! would resolve to TestApp (and since I want it resolving at execute time, this feels like the proper syntax to be sniffing at). that is not what happens, however -- instead I just get comp.name, etc. I have now gone through every iteration of replace I can think to try (e.g. %!replace!%, !!replace!! and nonsensical ones like %%replace%%, !%replace%! and !!!replace!!!) but nothing jives. ECHOing !comp.name! from within the FOR shows that the variable can be resolved (as does %comp.name%) so I'm extremely confused why !!replace!! wouldn't be the proper thing to do here.
is there a trick that I'm missing?
p.s. while alternate scripts that will solve the problem I'm trying to solve are certainly welcome, I am interested in knowing why I cannot solve it this way.
!!this!!because the parsing of such feature is done from left to right. I suggest you to read this post, where this management is explained.call %!this!%that would let you get an additional layer of delayed expansion without using afor /floop, but I can't remember exactly what it is.call %%!this!%%?comp.variables, the purpose of your script is also to replace<%=by space_+<<and%>by>>+_space, is that correct?forcommand. You may review some examples of extreme use of the multiple expansion phases in split string into substrings based on delimiter thread. For example, look for the "Replace each substring by a series of different strings" one.