VBScript Noob - Find Variable 1, Return Variable 2
December 4, 2008 11:30 AM   Subscribe

I have a text file with two columns. The 1st column has the asset number of a PC and the 2nd column has the PC name. I need a VB Script to look for an asset number (variable 1) in column 1 and return the answer in column 2 (variable 2). Thanks!
posted by bleucube to Computers & Internet (6 answers total) 1 user marked this as a favorite
 
Do you have Excel? This is a single function in Excel: =vlookup(). Read about it here:

Use HLookup and VLookup
posted by cameradv at 12:25 PM on December 4, 2008


Best answer: I apoligize if the formatting gets messed up.

Set fs = CreateObject("Scripting.FileSystemObject")Set haystack = fs.OpenTextFile(WScript.Arguments(0))needle = WScript.Arguments(1)Do Until haystack.AtEndOfStream    fields = Split(haystack.ReadLine, vbTab)    If UBound(fields) >= 1 Then        If fields(0) = needle Then            WScript.Echo fields(1)            Exit Do        End If    End IfLoophaystack.Close


On the command line, the first argument to the script is the name of the input file and the second argument is what you're searching for. Replace vbTab with "," if your file is comma delimited instead of tab delimited.
posted by zixyer at 12:32 PM on December 4, 2008


Response by poster: Thank you zixyer. Giving it a bash now!
Will provide results shortly.

Appreciate the quick reply

@cameradv need it in command line format to run as a post script for imagining process.
posted by bleucube at 12:52 PM on December 4, 2008


Response by poster: Zixyer,
I must confess I have no idea where to put my information.

So If my text file is c:\test.txt
and I'm looking for 789

where would this go in your script?
posted by bleucube at 1:26 PM on December 4, 2008


Best answer: You provide that information as arguments to the script when you run it from the command line.

E.g., if you saved the script as C:\scripts\find.vbs

C:\scripts>cscript //nologo find.vbs C:\test.txt 789

(//nologo suppresses the display of the Windows Script Host copyright information.)
posted by zixyer at 1:29 PM on December 4, 2008


Response by poster: Perfect - works great. Thank you so much!
posted by bleucube at 1:38 PM on December 4, 2008


« Older Handheld Backgammon Game Search   |   Can my composter be turned away from the dark (and... Newer »
This thread is closed to new comments.