Page 1 of 1

Finding Words in a File and Copying Strings

Posted: 13 Apr 2013 16:17
by Ranguna173
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

Re: Finding Words in a File and Copying Strings

Posted: 13 Apr 2013 21:21
by foxidrive
Untested:

Code: Select all

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

Re: Finding Words in a File and Copying Strings

Posted: 14 Apr 2013 01:06
by abc0502
Copy where ?!
BTW, add a
pause
after Foxidrive code to see the result.

Re: Finding Words in a File and Copying Strings

Posted: 14 Apr 2013 06:35
by Ranguna173
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 :)