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 !
CodePudding user response:
set str = %%i
This sets the variable "strSpace" to the value "Space(the value of %%i
)"
Use the syntax
set "str=%%i"
Including the quotes. Use set "var1=value"
for setting STRING values - this avoids problems caused by trailing spaces. Quotes are not needed for setting arithmetic values (
set /a`)
if not %str:"var version"=% == %str% DO (
The correct syntax is
if not "%str:var version=%" == "%str%" (
or, better
if "%str:var version=%" neq "%str%" (
The comparison is literal - both sides of the comparison operator must be quoted since the value may contain separators like spaces.
The correct syntax for string substitution is %varname:string=substitutestring%
Why set str
again?
To parse a string using =
as a delimiter, use
FOR /F "tokens=2 delims==" %%a IN ("string") DO (
Note however that %str%
will be the value str
had at the time the outer loop (%%i
) was encountered. For an explanation of how this works, see Stephan's DELAYEDEXPANSION link
You should consider using
FOR /F "tokens=2 delims==" %%a IN ("%%i") DO (
This worked for me - I changed the name of the file
@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=*" %%i IN (q71930885.txt) DO (
set "str=%%i"
if "!str:var version=!" neq "!str!" (
FOR /F "tokens=2 delims==" %%a IN ("%%i") DO ( @echo %%a )
)
)
GOTO :EOF
Response:
Space2.4;