Programming, Solution

VBScript: Content Search

Hi,

I used to write codes in Visual Basics, it was long time ago. Never thought that I will use it again until recently with my current job which using VBScript. For my understanding is that VB is more to application while VBS is more to script language that runs command, more like a batch file. In another word, VBS is the light version of VB. (in a sense haha)

For the first time, I was given a sample before I write it then after writing it I quite surprise VBS can do a lot with the files which I have been doing it manually years ago when I was in my uni. Some of the things that I wish I knew it back then so I could just write it and let the task scheduler do the works haha.

OK, so one of the tasks that have given to me was to search for numbers among 100-500++ files. So what I did was I wrote the code below which I called it “smart lookup” hehe… Below are just the small chunk of it but definitely a keeper:

num= InputBox("Please enter the number","Look for")
Set result Set fldr = fso.GetFolder(folderpath)
Set fls = fldr.Files
Set d=CreateObject("Scripting.Dictionary")
n=1

For Each fl In fls
Set r= fl.OpenAsTextStream(1)
content = r.ReadAll
if instr(1,content,num) then
d.Add "a" & n, fl.name
n=n+1
end if
r.close


Next
allRslt=d.Items
counter=d.Count

set w= result.CreateTextFile("result.txt") 'put all the result into this file, just in case it appears in multiple files
if counter>0 then
For i=0 to counter-1
w.WriteLine allRslt(i)
Next
end if
w.close

So this is just one of it that I’ve done and it’s quite handy.. Next post I’ll share you another task using VBScript

-Farrah K.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s