assign result of findstr to variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
petronni
Posts: 1
Joined: 19 Feb 2009 19:59

assign result of findstr to variable

#1 Post by petronni » 19 Feb 2009 20:11

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.

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#2 Post by RElliott63 » 20 Feb 2009 08:09

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

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#3 Post by DosItHelp » 20 Feb 2009 22:39

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.

Post Reply