0

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 !

JS1
  • 631
  • 2
  • 7
  • 23
  • 1
    a) spaces matter in batch: `set str = %%i` should be `set str=%%i` or even better `set "str=%%i"`. b) `if not %str:"var version"=% == %str%` should be `if not "%str:var version=%" == "%str%"` and c) you need [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) to use a variable that you defined or changed within the same code block. – Stephan Apr 19 '22 at 20:37
  • Thanks for answering, the command if "%str:var version=%" neq "%str%" DO ( isn't working properly the DO operator isn't colored as other operators are in my editor and when I execute the code I get the error message "DO was unexpected" also why when I the str variable with set "str=%%i" and try to echo it with @ echo %str% nothing is returned while @ echo %%i return the correct line ? – JS1 Apr 19 '22 at 23:18
  • 1
    `do` isn't part of the `if` syntax. You might remember a `for` loop, which actually requires a `do`. Have you read the linked article about delayed expansion? It explains why `%str%` is empty and how to use `!str!` instead. – Stephan Apr 20 '22 at 06:34

1 Answers1

1
 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;

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thanks for answering, the command if "%str:var version=%" neq "%str%" DO ( isn't working properly the DO operator isn't colored as other operators are in my editor and when I execute the code I get the error message "DO was unexpected" also why when I the str variable with set "str=%%i" and try to echo it with @ echo %str% nothing is returned while @ echo %%i return the correct line ? – JS1 Apr 19 '22 at 23:17
  • Yes - I missed that. I just copied the line and corrected the comparison. The `do` is illegal here. The syntax is `if astring op anotherstring (dosomething) else (dosomethingelse)` where `op` is an operator (==, equ, neq, gtr, lss, leq, geq). The parentheses are required only if there is an `else` clause and are optional on that `else` clause. – Magoo Apr 19 '22 at 23:52
  • Tried it, now I got "( was unexpected" – JS1 Apr 20 '22 at 00:11
  • Did you read the link to `delayedexpansion`? The point here is `str` will be unassigned before the outer `for` is encountered, so it will be replaced by *nothing* when the evaluation takes place. As `str` varies within the `code block` (parenthesised sequence of lines) then you need to invoke `delayedexpansion` by executing a `setlocal enabledelayedexpansion` statement on the line before the first `for` **AND** replace those `%` with `!`. My response applies to the *general* case. – Magoo Apr 20 '22 at 00:31
  • Yeah I read it, I was too quick to ask this question, wanted to edit it, but couldn't. As for my main problem after suppressing the "do" I got this error : "( was unexpected", here is my code : if "%str:var version=%" neq "%str%" ( FOR /F "tokens=2 delims==" %%a IN ("%%i") DO ( @echo %%a ) ) – JS1 Apr 20 '22 at 00:50
  • Your solution works well, thanks ! I would like to know a last detail, I replaced @echo %%b by set "vers=%%b" set vers=%vers: =% echo %vers% so that the spacing before the 3.14 is removed, but nothing appears in the console, if you have a way around it I will take it – JS1 Apr 20 '22 at 09:51
  • 1
    `set "vers=%%b"&set "vers=!vers: =!"&echo !vers!` The `&` separates commands. You could also use `"...tokens=4 delims==; " %%a...` as `delims` is a *set* of characters and `%%a` would then contain the string `2.4` provided the line found starts `static var version = 2.4;` – Magoo Apr 20 '22 at 11:26