Page 1 of 1

assign result of findstr to variable

Posted: 19 Feb 2009 20:11
by petronni
Hi.
First, sorry for my bad English...:)
I spent 4 hours searching soultion to this problem, on internet, cause i'm begginer at this.
Let's say i have c:\test.txt file. That file has 4 lines of text:
line1
line2
line2
qwertABC
I want to extract ABC from line 4, something like this:

set str=%findstr "ABC" c:\test.txt%
set str=%str:~-3%
but it won't work...

Sorry, if there is already another similiar post, but i didn't find one..

tnks.

Posted: 20 Feb 2009 08:09
by RElliott63
Try something like:

Code: Select all

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

Find "ABC" Test.txt > %Temp%\ABCstrings
For /F %%s in (%Temp%\ABCstrings) Do (
   Set "Str=%%s:~-3%
   Call :DoSomethingWithString !Str!
)
Del %Temp%\ABCstrings > Nul


**Just a suggestions, hasn't been tested**

-R

Posted: 20 Feb 2009 22:39
by DosItHelp
Good start RElliott63,

The line with the set command needs some fixing.
How about:

Code: Select all

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

For /F "delims=" %%s in ('Find "ABC" Test.txt') Do (
   Set "Str=%%s"
   Set "Str=!s:~-3!"
   Call :DoSomethingWithString !Str!
)

Hope this helps.