select and get only 1 string

May 18, 2010 at 03:48:15
Specs: Windows 7
Hello,

I would like to extract only "C:\gamedir\mygame\mygame.exe"
from a TOTO.txt which contains:

[A]
gamepath=C:\gamedir\A\A.exe
[B]
gamepath=C:\gamedir\B\B.exe
[Mygame]
gamepath=C:\gamedir\mygame\mygame.exe
[C]
gamepath=C:\gamedir\C\C.exe

Any help please ?


See More: select and get only 1 string

Report •


#1
May 18, 2010 at 06:24:00
this will work if the number of line is fixed

@echo off
setlocal EnableDelayedExpansion

for /f "skip=5 delims== tokens=2" %%i in (c:\toto.txt) do (
echo %%i
goto end
)
:end


Report •

#2
May 18, 2010 at 07:03:12
Thank you for your proposal, but this toto.txt was just an example.
The number of line is not fixed and it could be like this:

....
....
[A]
gamepath=C:\gamedir\A\A.exe
..........
[B]
......
gamepath=C:\gamedir\B\B.exe
[Mygame]
gamepath=C:\gamedir\mygame\mygame.exe
[C]
........
gamepath=C:\gamedir\C\C.exe
........
.......


I have to extract the first Gamepath value after corresponding to [Mygame] paragraph

Thank you in advance for your help


Report •

#3
May 18, 2010 at 09:09:42
maybe like this:
for /f "tokens=2* delims==" %%a in ('find /i "mygame.exe"<toto.txt') do echo %%a

or, if you have to use [Mygame], refer to this thread, and change 4 to 1:
http://www.computing.net/answers/pr...


Report •

Related Solutions

#4
May 18, 2010 at 22:55:59
OK.... my pb has moved a bit.
Now, I have to replace the line in toto.txt corresponding to the content of Myoldgame.txt that contains only 1 line like this:
gamepath=C:\mygame\mygame.exe

by a new string in a variable %New_Path% that I get by a SET /P command which value is for instance : C:\newgames\mynewgame.exe

Result expected is a Toto.txt like this:

....
....
[A]
gamepath=C:\gamedir\A\A.exe
..........
[B]
......
gamepath=C:\gamedir\B\B.exe
[Mygame]
gamepath= C:\newgames\mynewgame.exe
[C]
........
gamepath=C:\gamedir\C\C.exe
........
.......



Report •

#5
May 18, 2010 at 23:08:21
try this

@echo off
setLocal EnableDelayedExpansion

set /a count=1

for /f %%i in (c:\toto.txt) do (
if not %%i equ [Mygame] set /a count+=1
if %%i equ [Mygame] goto next
)

:next
for /f "skip=%count% delims== tokens=2" %%j in (c:\toto.txt) do (
echo %%j
goto end
)
:end

NOTE:: this code will give wrong output if the word "[Mygame]" is missing from the txt file.


Report •

#6
May 19, 2010 at 06:39:07

just modify the above code to direct the output to file like this
echo first few lines b4[mygame] >> new.txt
stop here
then echo newpath >> new.txt
then continue with the rest of the lines

delet the original toto.txt
rename new.txt to toto.txt


Report •

#7
May 20, 2010 at 03:34:51
Thank you Addy Roy for your 2 proposals.
They seem correct.
Although, I am not enough confident with Batch programs for merging them in only one fonctionning batch.

Many thanks for your help


Report •

#8
May 20, 2010 at 08:59:16
I tried to merge the 2 proposals in one with no positiv result.
Could you post the merged batch please ?

Report •


Ask Question