Batch - find line that starts with string

June 24, 2009 at 04:50:25
Specs: Windows XP
In a dos batch I am searching one txt file line by line. How can I find line that starts with some string?

So I need something like:
if line starts with "string" do (...) in dos batch!

Thx for help!


See More: Batch - find line that starts with string

Report •


#1
June 24, 2009 at 06:43:16
First you need something like a regex tool.

Search for xgrep.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
June 24, 2009 at 06:57:29
  findstr /B /C:"string" File.txt > nul
  if not ErrorLevel 1 (
    Your Commands
  )

This is NOT DOS but NT batch; see the above thread for some hints.

Report •

#3
June 24, 2009 at 11:27:50
I need to find string inside other string(not inside file)!

I am reading file with:

for /f "eol= tokens=* delims= usebackq" %%a in (D:\test_promweb.log) do (

i need something like:
findstr /B /C:"VMtriggers" "%%a" > nul ....

Is it possible to find string inside other string or to compare 2 strings?


Report •

Related Solutions

#4
June 24, 2009 at 13:23:29

"In a dos batch I am searching one txt file line by line. How can I find line that starts with some string?"


English is not my mother tongue as I am Italian, but I'm sure to have correctly understood your question.

Anyway from your post #3 I suggest you try

findstr /B /C:"C:\VMtriggers" "D:\test_promweb.log" > nul 
 if not ErrorLevel 1 (
    Your Commands
  )

as your For /F loop does nothing else than to read the file and makes cumbersome an easy operation.

Take a look at the thread above this one as coding inside an IF may be tricky for not trained people.


Report •

#5
June 24, 2009 at 14:24:34
I am reading from file test_promweb.log with:
for /f "eol= tokens=* delims= usebackq" %%a in (D:\test_promweb.log)

Lets say my test_promweb.log looks like:
line 1 - C:\VMtriggers\Preb_script>pcli RenameLabel
line 2 - C:\VMtriggers\Preb_script>goto end
line 3 - C:\VMtriggers\Preb_script>del
line 4 - fri 19.06.2009
line 5 - C:\VMtriggers\Preb_script>if
line 6 - 0 File(s) copied

As I said I am reading line by line.
I am trying to find out if line 1, line 2, line 3.... line 100 are starting with string "C:\VMtriggers"!

example - I have line "C:\VMtriggers\Preb_script>del", so I am trying to find out if I can find "C:\VMtriggers" in "C:\VMtriggers\Preb_script>del"

I hope u will understand my problem now :)


Report •

#6
June 24, 2009 at 17:57:24
you are the one not understanding what IVO posted. the findstr command uses an internal "for loop" (if you want to call it that way) to loop through files , so in fact, you don't need to use your own for loop (just for this case). as for searching within a string, that's what findstr does. It goes over the lines in the file and grab your string according to parameters you give it.

GNU win32 packages | Gawk


Report •


Ask Question