Find string in a big txt file and rturn the number line where the string is

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sal21
Posts: 22
Joined: 12 Jul 2016 12:58
Location: Italy

Find string in a big txt file and rturn the number line where the string is

#1 Post by sal21 » 03 May 2019 03:37

i'm working with vb6.

possible to:
Find a string in a big txt file and return the number of line where the string is found?
Naturally store the line number in a variable

note:
i need to integrate the batch in my vb6 application

tks and sorry for my bad english.

aGerman
Expert
Posts: 3706
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find string in a big txt file and rturn the number line where the string is

#2 Post by aGerman » 03 May 2019 03:47

Code: Select all

for /f "delims=:" %%i in ('findstr /nc:"your string" "your file.txt"') do set "n=%%i"
... untested though.
i need to integrate the batch in my vb6 application
Any good reason why you don't do this task in VB directly?

Steffen

sal21
Posts: 22
Joined: 12 Jul 2016 12:58
Location: Italy

Re: Find string in a big txt file and rturn the number line where the string is

#3 Post by sal21 » 05 May 2019 10:40

tks!
Sorry for delay.

but i need to use vb6.

possible to use the VB6 Shell?

If yes, have an idea for vb6 code?
Tks

sal21
Posts: 22
Joined: 12 Jul 2016 12:58
Location: Italy

Re: Find string in a big txt file and rturn the number line where the string is

#4 Post by sal21 » 05 May 2019 10:43

aGerman wrote:
03 May 2019 03:47

Code: Select all

for /f "delims=:" %%i in ('findstr /nc:"your string" "your file.txt"') do set "n=%%i"
... untested though.
i need to integrate the batch in my vb6 application
Any good reason why you don't do this task in VB directly?

Steffen
tks!
Sorry for delay.

but i need to use vb6.

possible to use the VB6 Shell?

If yes, have an idea for vb6 code?
Tks

aGerman
Expert
Posts: 3706
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find string in a big txt file and rturn the number line where the string is

#5 Post by aGerman » 05 May 2019 10:49

but i need to use vb6.
I'm not experienced with VB6. This is a Batch forum and I offered a Batch solution...

Steffen

sal21
Posts: 22
Joined: 12 Jul 2016 12:58
Location: Italy

Re: Find string in a big txt file and rturn the number line where the string is

#6 Post by sal21 » 05 May 2019 11:20

aGerman wrote:
05 May 2019 10:49
but i need to use vb6.
I'm not experienced with VB6. This is a Batch forum and I offered a Batch solution...

Steffen
ok
i can trap your code in a file .bat?
In this case i know the code in vb6 to lunch the bat.
if is possible, can you integrate your code to return the number of line finded in c:\test\myresult.txt

tks

aGerman
Expert
Posts: 3706
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find string in a big txt file and rturn the number line where the string is

#7 Post by aGerman » 06 May 2019 11:07

Just a guess ...

Code: Select all

@for /f "delims=:" %%i in ('findstr /nc:"your string" "your file.txt"') do exit /b %%i
No idea if that is what "return the number" means in your case.

Steffen

sal21
Posts: 22
Joined: 12 Jul 2016 12:58
Location: Italy

Re: Find string in a big txt file and rturn the number line where the string is

#8 Post by sal21 » 07 May 2019 00:54

aGerman wrote:
06 May 2019 11:07
Just a guess ...

Code: Select all

@for /f "delims=:" %%i in ('findstr /nc:"your string" "your file.txt"') do exit /b %%i
No idea if that is what "return the number" means in your case.

Steffen
HI Steffen, TKS FOR CODE.!

To save the result of bat in c:\testdir\result.txt, in this case the number of line found?

is this correct:
@for /f "delims=:" %%i in ('findstr /nc:"your string" "your file.txt"') do exit /b %%i > c:\testdir\result.txt

aGerman
Expert
Posts: 3706
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find string in a big txt file and rturn the number line where the string is

#9 Post by aGerman » 07 May 2019 06:16

No, you have to redirect the ECHO output to the file.

Code: Select all

@for /f "delims=:" %%i in ('findstr /nc:"your string" "your file.txt"') do >"c:\testdir\result.txt" echo %%i
Steffen

sal21
Posts: 22
Joined: 12 Jul 2016 12:58
Location: Italy

Re: Find string in a big txt file and rturn the number line where the string is

#10 Post by sal21 » 07 May 2019 06:35

aGerman wrote:
07 May 2019 06:16
No, you have to redirect the ECHO output to the file.

Code: Select all

@for /f "delims=:" %%i in ('findstr /nc:"your string" "your file.txt"') do >"c:\testdir\result.txt" echo %%i
Steffen
Is this correct?
Last edited by sal21 on 07 May 2019 06:48, edited 1 time in total.

aGerman
Expert
Posts: 3706
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find string in a big txt file and rturn the number line where the string is

#11 Post by aGerman » 07 May 2019 06:41

Does it work?
How should I know the content of your 985.txt and if all paths are correct and existing?

Steffen

sal21
Posts: 22
Joined: 12 Jul 2016 12:58
Location: Italy

Re: Find string in a big txt file and rturn the number line where the string is

#12 Post by sal21 » 07 May 2019 06:50

aGerman wrote:
07 May 2019 06:41
Does it work?
How should I know the content of your 985.txt and if all paths are correct and existing?

Steffen
resolved!
wRONG PATH!
tHE CODE NOW WORK PERFECT!

Bro, love Pizza?
When you come in Napoli....

Tks also for your time.
Sal

sal21
Posts: 22
Joined: 12 Jul 2016 12:58
Location: Italy

Re: Find string in a big txt file and rturn the number line where the string is

#13 Post by sal21 » 09 May 2019 06:52

aGerman wrote:
07 May 2019 06:41
Does it work?
How should I know the content of your 985.txt and if all paths are correct and existing?

Steffen
OPS.....
in txt file i can have:
AAAA
AAAA
AAAA

The bat capture all AAAA...

in my work i need to capture only the first occurrence of AAAA, and exit from the code, possible?

aGerman
Expert
Posts: 3706
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find string in a big txt file and rturn the number line where the string is

#14 Post by aGerman » 09 May 2019 08:13

Try it this way

Code: Select all

@for /f "delims=:" %%i in ('findstr /nc:"your string" "your file.txt"') do >"c:\testdir\result.txt" echo %%i&exit /b
Steffen

Post Reply