0

I'm trying to make a bat file that localizes labels in a custom extension file I made and gets an output, the problem is that since there is apparently no way to increase a variable in a FOR loop without starting a local environment, I cannot do that.

@echo off
SET _TSL=%3
SET _forcount=1
SET _COLONLINE=0
SET /a _LINESKIP=%_TSL%-1
if "%5"=="1" (
echo [Mesa Markdown Debug] File Directory     : %1
echo [Mesa Markdown Debug] Target Colon       : %2
echo [Mesa Markdown Debug] Line               : %_LINESKIP%
echo [Mesa Markdown Debug] Item               : %4
echo [Mesa Markdown Debug] DebugMode          : %5
echo.
)
setlocal ENABLEDELAYEDEXPANSION
for /F "eol=$ usebackq" %%i in (%1) do (
SET /A _forcount=_forcount+1
if "%%i"=="%2" SET _COLONLINE=!_forcount!
)
if "%5"=="1" (echo [Mesa Markdown Debug] Target Colon is: %_COLONLINE%)
SET /a _LINESKIP=%_LINESKIP%+%_COLONLINE%
if "%5"=="1" (echo [Mesa Markdown Debug] Target line acquired: %_LINESKIP%)
for /F "eol=$ skip=%_LINESKIP% delims=" %%i in (%1) do if not defined _mmkstring (SET _mmkstring=%%i)
if "%5"=="1" (echo [Mesa Markd

own Debug] Conent of line %_LINESKIP%: %_mmkstring%)
if "%_mmkstring%"=="" (echo [Mesa Markdown] Error @ line %_LINESKIP% of %1 - The line appears empty.)
if "%4"=="1" (for /F "tokens=1-9* delims={}," %%a in ("%_mmkstring%") do SET _mmk=%%a)
if "%4"=="2" (for /F "tokens=1-9* delims={}," %%a in ("%_mmkstring%") do SET _mmk=%%c)
if "%4"=="3" (for /F "tokens=1-9* delims={}," %%a in ("%_mmkstring%") do SET _mmk=%%e)
endlocal

In a separate batch file which called this one, "echo %_mmk% returns nothing.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
DarkMesa
  • 15
  • 1
  • 6
  • Can you please give an example of how you execute it? (we need to see the parameters you use) – Stephan Jan 31 '19 at 14:40
  • My bad: @echo off `:: Syntax: CALL mmkparse.bat (file directory, colon, line, line item, debug mode (0,1)) CALL mmkparse.bat %cd%\demo.mmk, [Section_2], 2, 2, 1 echo Item = %_mmk% pause>nul` – DarkMesa Jan 31 '19 at 14:44
  • Let me know if there's a way to increment a variable within a FOR loop without using setlocal or any way to make it so the variables don't destroy once I do `endlocal`. I tried exporting the variable to a text file and then load it but to no avail. – DarkMesa Jan 31 '19 at 14:46
  • 2
    If you are trying to pass back the value of `_mmk` to the calling batch file you need to do this at the end of the batch file that was called. `endlocal &set _mmk=%_mmk%` – Squashman Jan 31 '19 at 14:48
  • 1
    without testing... Do you `echo %_mmk%` *after* the `endlocal`? If yes: it's not existent any more. You need to `endlocal & set _mmk=%_mmk%`. But to your question: yes it is possible to increment a variable without delayed expansion: `set /a _forcount+=1` (Increment yes, echoing no) – Stephan Jan 31 '19 at 14:48
  • @squashman Thank you that fixed it. I had never used setlocal before and this was freaking me out, thank you now it works perfectly. I wish there was some other way to increment variables within a `FOR` loop though. – DarkMesa Jan 31 '19 at 14:52
  • 2
    You *can* increment/decrement a variable in a loop without delayed expansion, but that doesn't help you, because you can't *use* it. What exactly do you want to do? Finding the line number of a line with a certain string? – Stephan Jan 31 '19 at 14:57
  • 1
    The problem is not the increment. The problem is assigning the value of the increment variable to another variable. You could do without delayed expansion by using the `CALL` command. `if "%%i"=="%2" CALL SET _COLONLINE=%%_forcount%%`. But why bother doing the `SET`. Just break out of the FOR command and you could then also set the variable. – Squashman Jan 31 '19 at 14:57
  • 2
    Please include the [above](https://stackoverflow.com/questions/54462879/need-assistance-with-delayed-expansion#comment95734100_54462879) into your question! – aschipfl Jan 31 '19 at 15:00

0 Answers0