Finding Words in a File and Copying Strings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Finding Words in a File and Copying Strings

#1 Post by Ranguna173 » 13 Apr 2013 16:17

Hi everyone :)


I have a file db.txt and it's something like this:
---------start of db.txt
'a'=b
'b'=c
'c'=d
---------end of db.txt

What I want the batch to do is to find a user provided letter (a, b or c) and copy the letter that's next to the "=".

I already know how to find words or letters in a file:

Code: Select all

FINDSTR /C:"('a', 'b' or 'c')" db.txt > nul

But I don't know how to make the batch copy the letter that's next to the = symbol.

Thanks for reading and please help :D

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Finding Words in a File and Copying Strings

#2 Post by foxidrive » 13 Apr 2013 21:21

Untested:

Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('FINDSTR /C:"'a'" db.txt') do echo %%a

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Finding Words in a File and Copying Strings

#3 Post by abc0502 » 14 Apr 2013 01:06

Copy where ?!
BTW, add a
pause
after Foxidrive code to see the result.

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Finding Words in a File and Copying Strings

#4 Post by Ranguna173 » 14 Apr 2013 06:35

abc0502 wrote:Copy where ?!
BTW, add a
pause
after Foxidrive code to see the result.


Forgot to mention that, copying into a variable like foxdrive did.
There's realy no need for a pause, you can run it the console.

foxidrive wrote:Untested:

Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('FINDSTR /C:"'a'" db.txt') do echo %%a


Untested ??
You managed to code a working codeblock without testing it, dude that's some awesome skills :shock:

Anyways I tested it and it works perfecly, thanks :D


Thanks again to everyone for replaying :)

Post Reply