Computing.Net > Forums > Programming > Searching for Strings

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.

Searching for Strings

Reply to Message Icon

Name: deepak.s
Date: July 18, 2009 at 10:11:17 Pacific
OS: Windows XP
CPU/Ram: Core Duo/1GB
Subcategory: Batch
Comment:

Hi Friends,

I am kinda new to Batch file programming.
I have few text files in a folder. (eg: mp3_1.txt mp3_2.txt mp3_3.txt etc etc). All these text files contains some mp3 songs file names in each line.

I want to search a specific string in all these files and I want to display the matching lines as

mp3_1.txt
<tab> line1
<tab> line32
<tab> line45

mp3_2.txt
<tab> line2
<tab> line6

I dont want other files in the folder to be displayed. (only the file contaning the string should be displayed)
Can anyone guide me in this regard?



Sponsored Link
Ads by Google

Response Number 1
Name: wizard-fred
Date: July 18, 2009 at 13:42:11 Pacific
Reply:

I think you are doing it backwards. The mp3 text files entries should be all added to a database. Then indexed by title, artist, location, etc. acccording by how you want it accessed.


0

Response Number 2
Name: deepak.s
Date: July 18, 2009 at 19:07:14 Pacific
Reply:

thanks for the quick reply.
i think that will solve a big problem. bt if i want to have these files seperate, is there any solution?


0

Response Number 3
Name: wizard-fred
Date: July 18, 2009 at 20:53:59 Pacific
Reply:

You still can have the text files separate. The database will contain the information that you consider important or a link back to the text file.


0

Response Number 4
Name: wizard-fred
Date: July 18, 2009 at 21:49:50 Pacific
Reply:

Another method is to use a utility like GREP. It will do the seach almost exactly as you require except for the tab.
grep options string *.txt


0

Response Number 5
Name: ghostdog
Date: July 18, 2009 at 23:29:45 Pacific
Reply:

vbscript

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "c:\test"
strSearch = "test"
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
	strFileName =strFile.Name
	strFilePath = strFile.Path
	strFileExt = objFS.GetExtensionName(strFile)
	If strFileExt = "txt" Then
		Set objFile = objFS.OpenTextFile(strFile)	
		s=""	
		f=0
		Do Until objFile.AtEndOfLine
			linenum=objFile.Line
			strLine = objFile.ReadLine
			If InStr(strLine,strSearch) > 0 Then
				s = s & strLine & "-> line: " & linenum
				f=1	
			End If 
		Loop
		If f=1 Then
			WScript.Echo strFileName 
			WScript.Echo vbTab & s
		End If 
		objFile.Close
	End If 
Next

save as myscript.vbs and on command line

c:\test> cscript /nologo myscript.vbs

GNU win32 packages | Gawk


0

Related Posts

See More



Response Number 6
Name: deepak.s
Date: July 19, 2009 at 19:10:35 Pacific
Reply:

wow.. thank you for the code. it worked with me. I have also tried to do it using bat file. Well, i came up with a solution today.
I am putting the code here.

echo.
set/p cho=Please Enter Keyword:
cd "CD Content"
echo.
echo Search Results for - %cho%
echo.
for %%f in (*.*) do (
findstr /i "%cho%" "%%f" > found.cdtxt
for %%t in (found.cdtxt) do (
if %%~zt GTR 0 (
echo.
echo %%f
for /F "tokens=* delims=" %%t in (found.cdtxt) do (
echo %%t
)
)
)
)
echo.
pause
del found.cdtxt
cd ..

thank you guys for helping me with the code.


0

Sponsored Link
Ads by Google
Reply to Message Icon

i want create msi file fo... batch to rename txt file ...



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: Searching for Strings

Search for string? (C++) www.computing.net/answers/programming/search-for-string-c/13287.html

Search for string on multi comp www.computing.net/answers/programming/search-for-string-on-multi-comp/15032.html

search for string in batch www.computing.net/answers/programming/search-for-string-in-batch/16124.html