How can i get this text from multiple line in batch file

Viewed 40

I have a text file (Myfile.txt), line by line, which is very long and centered as shown below

...","ItemPrice":17000.0,"MustPay":17000.0,"PaywithCash":17000.0,"etc...
...","ItemPrice":900.0,"MustPay":900.0,"PaywithCash":900.0,"etc...
...","ItemPrice":1400.0,"MustPay":1400.0,"PaywithCash":1400.0,"etc...

so I want to get the number after the word "PayWithCash":, for example the first line is the number 17000 and so on to the next line, and save it to a new text file "result.txt"

1700
900
1400

I have tried several codes, including as below

echo off 
SETLOCAL EnableDelayedExpansion
for /f "delims=" %%a in ('type Myfile.txt^|find "PayWithCash:"') do (
  set "line=%%a"
  set "line=!line:*PayWithCash =!
  set /a "last=!line:~1!" 2>nul
)
echo %last% >> result.txt

And yes, I still can't get the desired result, can you help me?

I use google translate, I hope you understand

1 Answers

Correct PayWithCash to the correct case in the data, PaywithCash or add the /i switch to find to make the match case-insensitive.

Remove the colon as suggested by @Compo.

Remove the Space after PayWithCash in the set statement as there is no such Space in the data.

Since the result in line will now begin ": you need to use line:~2

Test code I used:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q64838297.txt"
SET "outfile=%destdir%\outfile.txt"

(
for /f "delims=" %%a in ('type "%filename1%"^|find /i "PayWithCash"') do (
  set "line=%%a"
  set "line=!line:*PayWithCash=!"
  set /a "last=!line:~2!" 2>nul

  echo !last!
)
)>"%outfile%"

TYPE "%outfile%"

GOTO :EOF

You would need to change the settings of sourcedir and destdir to suit your circumstances. The listing uses a setting that suits my system.

I used a file named q64838297.txt containing your data for my testing.

Produces the file defined as %outfile%

Result:

17000
900
1400
Related