Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm trying to write a simple script that makes a csv file query. The csv file structure is:
1234,55.66.77.88
Using inputbox, i'll ask for the first number. Then, based on that number, i'd like to search the csv file and return the number that follows the comma.
example:
Enter database number: 1234
The IP address is: 55.66.77.88There will actually be quite a bit more to this, but this is the part that i'm stuck on.
Thanks in advance!

I dunno VB but this is how I'd do it in a BAT:
::== findREC.bat
@echo off
set /p rec=Enter database number:
find "%rec%" < my.csv > therec
if errorlevel 1 echo %rec% not found && goto :eof
for /f "tokens=2 delims=," %%R in (therec) do (
echo The IP address is: %%R
)
:: DONE
If at first you don't succeed, you're about average.M2

Can you be more specific where you are having a problem? Are you having a problem reading the csv file, comparing the values, what?
Michael J

The part I'm having trouble with is grabbing the IP address that follows the comma. I need to wind up with a strIPaddress variable that's set based on the first number in the txt file.
This is a really crude example of what I've got so far:
dim strClubNumber
dim strIPaddressstrClubNumber = inputbox ("Please enter the 4 digit club number: ")
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\scripts\IPaddresses.txt", 1)Do Until objFile.AtEndOfStream
strLine = objFile.Read(4)
Wscript.echo strline
if strLine = strClubnumber then wscript.echo ("found it!")
objFile.SkipLine
loop
I was using the echo to see if it was actually reading the correct info in the file. It appears to be because when it reaches the number entered it echos...Now, If I could get strIPaddress set with the IP address that follows the clubnumber in the txt file, I'll be good to go.
All of my scripting knowledge has been primarily for administrative purposes... wmi stuff, etc. so I'm pretty lost when it comes to FSO.
Thanks!

Here's what I would do:
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
//Create a 2 dimensional array from the
//line, using the comma as a delimiter
lineAry = Split(strLine, ",", -1, 1)Wscript.echo strline
If lineAry[0] = strClubnumber Then wscript.echo ("The IP is " & lineAry[1])
objFile.SkipLine
End If
LoopMichael J

First get rid of the File Scripting Object. It's slow, cumbersome and a resource hog. It is designed to be used with VBScript and is not needed in VB. Use the facilities built into VB.
Dim Number as String
Dim IPAddress as String
Dim StrClubNumber as StringstrClubNumber = inputbox ("Please enter the 4 digit club number: ")
Open Filename For Input As #1
Do
input #1, Number, IPaddress
Loop While Number <> strClubNumber
Close #1IP Address will be in the variable IPAddress
A lot simpler than all that FSO crap and a hell of a lot faster.Stuart

Stuart:
I should have been a little clearer in the title. VB script or WSH is what I'm working on, so I think the FSO is my only option.
I'm not a programmer, just a netadmin that has a hard time telling people no! :)
MichaelJ:
Thanks for your help. Here's what I have, but I'm getting an 'expected then' in line 14.
dim strClubNumber
dim strIPaddressstrClubNumber = inputbox ("Please enter the 4 digit club number: ")
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\dtscript\dtIPaddresses.txt", 1)Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
lineAry = Split(strLine, ",", -1, 1)
If lineAry[0] = strClubnumber Then wscript.echo ("The IP is " & lineAry[1])
objFile.SkipLine
End If
Loop
Any suggestions? Thanks again!

You just had some formatting errors. I have corrected it and condensed it for you:
dim strClubNumber, strIPaddress, foundIPstrClubNumber = inputbox ("Please enter the 4 digit club number: ")
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("test.txt", 1)Do Until objFile.AtEndOfStream
lineAry = Split(objFile.ReadLine, ",", -1, 1)
If (lineAry(0) = strClubNumber) Then
foundIP = lineAry(1)
Exit Do
End If
Loopwscript.echo ("The IP is " & foundIP)
Michael J

Thank you! That works just like I want. With a few tweaks, i'll have this working in no time.
Thanks again!

Hi Michael J,
How do you keep you indents from getting lost when you post code?
TIA
=====================================
If at first you don't succeed, you're about average.M2

Hi Michael J,
Obviously I'm not smart enough for this.
Can you give us a little "indenting for dummies"?
=====================================
If at first you don't succeed, you're about average.M2

Sure. You just need to use the HTML code for a non-breaking space which is "& n b s p ;" (minus the spaces and the spaces and the quotes).
Here is an example. This first sample is using the non-breaking spaces - except I have replaced the ampersand (&) with a percent sign (%) so you can see what it looks like when you post:
Title A
%nbsp;%nbsp;Sub Title 1
%nbsp;%nbsp;Sub Title 2
%nbsp;%nbsp;%nbsp;%nbsp;Line i
%nbsp;%nbsp;%nbsp;%nbsp;Line ii
Title BNow here is the same code replacing the percent symbol with an ampersand. This should show up with the sub elements indented:
Title A
Sub Title 1
Sub Title 2
Line i
Line ii
Title B
Michael J

@echo off
:main
echo this is main
goto :eof:eof
=====================================
If at first you don't succeed, you're about average.M2

hmmm
It looked OK in preview.
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |