Fahim Arisol
Fahim Arisol

Reputation: 1

Batch-file find a specific word in a file and pass the word from the line

I have this config file that I want to print out the word that I want. For now i'm just using simple batch-file like this:

File Name : MYIMMSTOCK.EXE.CONFIG

<add key="ApplicationName" value="MyImms-VP"/>
<add key="CurrentVersion" value="v6.1.0.8"/>

Batch-File

findstr "CurrentVersion" C:\JIM\MYIMM\BIN\STOCK\MYIMMSTOCK.EXE.CONFIG

Output

add key="CurrentVersion" value="v6.1.0.8"/

So how can I get the word from that value v6.1.0.8 only to print out? Please help me.

Upvotes: 0

Views: 194

Answers (1)

dbenham
dbenham

Reputation: 130929

Assuming the format of the line never changes from what you have shown, then you can use FOR /F to capture the 4th token using " as a token delimiter.

@echo off
for /f tokens^=4^ delims^=^" %%A in (
  'findstr "CurrentVersion" "C:\JIM\MYIMM\BIN\STOCK\MYIMMSTOCK.EXE.CONFIG"'
) do echo %%A

Or you could use my JREPL.BAT regular expressionn text processing utility to achieve a bit more robust solution. JREPL is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward.

jrepl "key=\qCurrentVersion\q +value=\q(.*?)\q" $1 /x /jmatch /f "C:\JIM\MYIMM\BIN\STOCK\MYIMMSTOCK.EXE.CONFIG"

Use CALL JREPL if you put the command within a batch script.

Upvotes: 1

Related Questions