Computing.Net > Forums > Programming > Batch - find line that starts with string

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch - find line that starts with string

Reply to Message Icon

Name: musa33
Date: June 24, 2009 at 04:50:25 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: June 24, 2009 at 06:43:16 Pacific
Reply:

First you need something like a regex tool.

Search for xgrep.


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

M2


0

Response Number 2
Name: IVO
Date: June 24, 2009 at 06:57:29 Pacific
Reply:

  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.

0

Response Number 3
Name: musa33
Date: June 24, 2009 at 11:27:50 Pacific
Reply:

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?


0

Response Number 4
Name: IVO
Date: June 24, 2009 at 13:23:29 Pacific
Reply:


"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.


0

Response Number 5
Name: musa33
Date: June 24, 2009 at 14:24:34 Pacific
Reply:

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 :)


0

Related Posts

See More



Response Number 6
Name: ghostdog
Date: June 24, 2009 at 17:57:24 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch - find line that starts with string

batch processing text files. www.computing.net/answers/programming/batch-processing-text-files/15280.html

Batch file RegEx query www.computing.net/answers/programming/batch-file-regex-query/16943.html

Batch file to extract certain lines www.computing.net/answers/programming/batch-file-to-extract-certain-lines/17465.html