Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching files for a string with VBscript

Status
Not open for further replies.

tekdatt

MIS
Apr 27, 2012
2
US
I need to make a script that searches all the text files in a folder for a specific string and if the file has that string, I need to save it do a different folder. I realize this may be a simple task for a real programmer, but I'm just a novice programmer. Please help.
 
Hello,

Please note that this forum is for VB 5 & 6 code. You may receive some replies here, although you should direct your code to the vb script forum: forum329

If at first you don't succeed, then sky diving wasn't meant for you!
 
Also a suggestion, you may want to indicate what you have already tried and possibly some sample code. This may help you get more responses.

If at first you don't succeed, then sky diving wasn't meant for you!
 
Thank you, I'll repost there as well. It honestly doesn't matter if it's done in VB or VB script. I've been able to figure out how to search a file for the string.

Dim objFSO, strLine, objReadFile
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objReadFile = objFSO.OpenTextFile("C:\temp\Test.txt", 1, False)

Do Until objReadFile.AtEndOfStream

strLine = objReadFile.ReadLine
strLine = trim(strLine)

if instr(strLine, "123456") Then
'save this file to folder C:\temp\saveEnd If

Loop

set objFSO = nothing


I'm just not understanding how to do it for every file in the folder, and once it finds the string, how to save it to the other folder.

 
There is some example code for a recursive search in the VBScript FAQ section: faq329-5515
 
>I've been able to figure out how to search a file for the string.

The line-by-line slow way ...
 
It was worth a try! Biting on comments like that helped me learn a fair bit over the years! [smile]

Weirdly, I've just had to do this very task (and display the found files). Only really to see if I could write a faster solution than my boss did using LINQ.

My Python solution served me pretty well once the file count got large...[wink]

Andy
---------------------------------
Zebracorn: 50% Zebra, 50% Unicorn = 100% Real.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top