I'm trying to recuperate a particular data from a file (we will call it MyFile.dart) using a batch file.
To be more precise I'm trying to recuperate the 2.4 from this line :
static var version = 2.4;
First, I use a for loop to iterate through each line of my file :
FOR /F "tokens=*" %%i IN (MyFile.dart)
Then I want to check if the line contains a particulare string (here "var version")
set str = %%i
if not %str:"var version"=% == %str% DO
I got this from this topic but here I get the error :
=str is unexpected
Since the check doesn't work, I comment it and I try my next for loop on each line of MyFile.dart (if the check worked it would have been only on the line containing "var version") :
set str = %%i
FOR /F "tokens=2 delims==" %%a IN (%str%) DO (
@echo %%a
)
Here I'm supposed to split the line using "=" as a separator and display the second element of the split array, but I get nothing printed in the console, and when I comment @echo off, I see that %str% is null. I tried using directly %%i but I also get an error.
So I hardcoded the line I'm interested in the loop :
set str = %%i
FOR /F "tokens=2 delims==" %%a IN ("static var version = 2.4;") DO (
@echo %%a
)
And got the expected result of 2.4; in the console (but obviously it's not how I want to get it).
So to summarize :
First problem : the "if not" to check if the line contains a particular substring doesn't work.
Second problem : I can't pass the variable from the first loop (a line of the file) to the second loop to then parse it.
Here is my whole code :
FOR /F "tokens=*" %%i IN (MyFile.dart) DO (
set str = %%i
if not %str:"var version"=% == %str% DO (
FOR /F "tokens=2 delims==" %%a IN (%str%) DO (
@echo %%a
)
)
)
NB : If you have a totally different algorithm to get to the same result I will take it !