-1

I have string photo="999" price="10" category="1" . I want to get only 10. This means I need to the string which start price=" and ends with "

@For /F "Tokens=1*Delims==" %%A In ('FindStr /I "^price=" "C:\price.txt" 2^>NUL')Do @Set "Ver=%%~B"
@Echo(%%Ver%% = %Ver% & Pause
Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
  • 2
    Your `FindStr` match is not correct, because it is trying to match any line in `C:\price.txt` which begins with the string `price=`, and clearly your line begins with `photo=`. Even if you were to change it to `"^photo="`, your first token `%%A`, would resolve to `photo` and your second token, `%%B`, would resolve to `"999" price="10" category="1"`. In that case, you would therefore need to perform further actions on `%%B` to isolate your required value. – Compo Oct 30 '20 at 15:04
  • What about `for /F "tokens=4 delims== " %%A in ("C:\price.txt") do echo/%%~I`? – aschipfl Oct 30 '20 at 15:24
  • Do you need to find ANY price? Then use this: `@For /F "Tokens=4 Delims=^= " %%A In ('TYPE "C:\price.txt" ^| FIND /I "price="""') Do @( Set "Ver=%%~A"& Echo Price found was "%%~A"` OR are you saying you only want price when it equals 10? (seems less likely imo) If so use this: `@For /F "Tokens=4 Delims=^= " %%A In ('TYPE "C:\price.txt" ^| FIND /I "price=""10"""') Do @( Set "Ver=%%~A")` The way your question title and content is worded it's a little unclear what you really need to accomplish. Hope that helps! :) – Ben Personick Oct 30 '20 at 16:23

4 Answers4

0

findstr always returns the complete line, if successful. So it's not the right tool for this task (actually, there is no tool in cmd at all that could do that this way).

But with a bit of logic, you can work around it: remove the part from the start until (including) the triggerword price (a task, the set command is happy to do), then process the rest with a for /f loop to get the desired substring:

set "string=photo="999" price="10" category="1""
echo check: %string%
echo debug: %string:*price=%
for /f tokens^=2^ delims^=^" %%a in ("%string:*price=%") do set "ver=%%~a"
echo ver=%ver%

If you are sure of the exact format of your string (in your example the searched substring is the second quoted argument, so the fourth token when splitted by ") it gets as easy as:

for /f tokens^=4^ delims^=^" %%a in ("%string%") do echo ver=%%~a

or

for /f tokens^=4^ delims^=^" %%a in (file.txt) do echo ver=%%~a
Stephan
  • 53,940
  • 10
  • 58
  • 91
0

Here's an example which tries to follow a similar methodology as your example code.

It uses FindStr to isolate any line in C:\price.txt, which includes the word price="<OneOrMoreDigits>". That line is saved as a variable named price, which is split under delayed expansion in a nested For loop, to remove everything up to, and including the first instance of the string price, leaving, in this case, ="10" category="1". The nested loop further splits that, to take the second token, using a doublequote character as the delimiter, (which should be your required value).

@For /F Delims^=^ EOL^= %%G In ('%__AppDir__%findstr.exe /IR "\<price=\"[0123456789]*\"\>" "C:\price.txt"') Do @(Set "price=%%G" & SetLocal EnableDelayedExpansion
    For /F Tokens^=2^ Delims^=^" %%H In ("!price:* price=!") Do @EndLocal & Set "price=%%H")
@Echo %%price%% = %price% & Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
0
@ECHO OFF
SETLOCAL

set "string=photo="999" price="10" category="1""

:: remove quotes
set "string=%string:"=%"
for /f %%a in ("%string:* price=%") do set /a pricefound%%a
set pri
goto :eof

Since we don't have a representative sample of the file in question, we're forced to the conclusion that the requirement is to find the one and only appearance of price="anumber" in the file.

So, since findstr output, properly framed, would select this line, all we need do is process the string.

This is kind of a quick-and-dirty method; it may be adequate for OP's purpose.

First, remove the quotes from the string as they have a habit of interfering.

Next, use for /f in string-processing mode where it does its magic on the quoted string in parentheses. The string is the original string, minus quotes, so replace all characters up to "Spaceprice" with nothing and take the first token of the result, resulting in =10 assigned to %%a in the example case.

Then execute "set /a somevariablename=10" by simply concatenating the two strings.

Note that if the file contains a line like ... pricelastweek="9" ... then other measures may need to be taken.

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

Well clearly you need to match lines that contain price=" as there may be other lines.

What's unclear is if you need match 10 exactly, or just want that to be any number.

It seems likely you just want to match any number and grab it.

This is done easily with:

@For /F "Tokens=4 Delims=^= " %%A In ('
  TYPE "C:\price.txt" ^| FIND /I "price="""') Do @(
    Set "Ver=%%~A" & CALL SET Ver &Pause )

While is you need to match Price="10", which seems less useful, but at least one person took that meaning and your wording is a little unclear so I will add that was well:

@For /F "Tokens=4 Delims=^= " %%A In ('
  TYPE "C:\price.txt" ^| FIND /I "price=""10"""') Do @(
    Set "Ver=%%~A" & CALL SET Ver &Pause )

Note in all examples I left in the @ symbols since I assume this is you being clever, and leaving ECHO ON and only removing the @ symbols when you want to debug some specific thing you are doing.

However, in case not, it's worth pointing out that in a script it's usually easiest to place ECHO OFF at the start of the script instead of putting an @ at the beginning of each statement to stop it from echoing.

Cheers! :)

Ben Personick
  • 3,074
  • 1
  • 22
  • 29