Using DOS to search for string

July 3, 2008 at 16:35:08
Specs: Win2K, Intel Centrino
Hi experts,

How can I search for multiple occurence of a particular string in a series of textfile using DOS? Once it has found each occurence, it will copy the whole line of that occurence to a seperate file, for example "C:\temp.txt". Once this is done, it will continue with the next text file in the folder.

Thanks everyone.



See More: Using DOS to search for string

Report •


#1
July 3, 2008 at 18:27:09
Seems kind of odd that the last several threads have been about manipulating text files with search routines. Are you all in the same programming class?

Report •

#2
July 4, 2008 at 01:32:47
Hi Dan

Could be a bit tricky in DOS.

LOL


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

M2


Report •

#3
July 4, 2008 at 09:34:53
Hey all,

Lol..no man. Its actually for work. I tried manipulating them using VBA, but I'm limited to the number of lines in Excel to do it. So i figured DOS would be able to do something similar.


Report •

Related Solutions

#4
July 4, 2008 at 10:52:25
for %%a in (*.txt) do find "string" < %%a >> newfile


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

M2


Report •

#5
July 4, 2008 at 14:36:40
Thanks! That worked just fine. Is it possible to attach the filename of where the string was found into the 'newfile'?

Report •

#6
July 4, 2008 at 14:57:49
Is it DOS or W2K?


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

M2


Report •

#7
July 4, 2008 at 15:20:54
Its in DOS. Thanks again mate

Report •

#8
July 4, 2008 at 20:30:41
@echo off > newfile
for %%a in (*.txt) do @echo %%a =========== >> newfile & find "string" < %%a >> newfile

[that's 2 lines]


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

M2


Report •

#9
July 5, 2008 at 22:24:59
Hi M2,

Thanks again. But it doesnt work. It doesnt copy the file name of the file that contains the string into the newfile.


Report •

#10
July 6, 2008 at 05:14:00
Post a sample of the directory you're running it in.


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

M2


Report •

#11
July 7, 2008 at 10:59:17
for example:

Folder: C:\temp. In it contains 1.txt and 2.txt. The string "happy" is contained only in 1.txt. The batch file does a search on both the txt files and when it finds the string "happy", it will copy the entire line into "newfile". If the string is not found, move to next file.


Report •

#12
July 7, 2008 at 11:30:29
I'm stumped.

Do a VER and report exact OS.


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

M2


Report •

#13
July 7, 2008 at 12:28:06
Microsoft Windows 2000 [Version 5.00.2195]

Based on the latest code that was given, it still doesn't copy the filename along with the line from which the string was found.

I tried manipulating your code a bit with whatever basic understanding I have of DOS and came up with this:

@echo off > Block.txt
for %%a in (*.fmt) do @echo %%a Block.txt & find "Blocked" < %%a >> Block.txt & @echo %%a >> Block.txt

(2 lines)

Thing is, this code copies the filename, regardless if the string was found or not.


Report •

#14
July 7, 2008 at 22:56:28
w2000 is not DOS.

The code in #8 works in w2000.


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

M2


Report •

#15
July 8, 2008 at 17:15:44
hey mate. my bad. it works. forgot to put the >> in the last line. Thanks!

Report •


Ask Question