Computing.Net > Forums > Programming > Simple VB text file query? Help!

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.

Simple VB text file query? Help!

Reply to Message Icon

Name: glenmoore670
Date: June 1, 2006 at 07:53:54 Pacific
OS: Windows XP Pro
CPU/Ram: XEON 4 gigs
Product: Dell
Comment:

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.88

There will actually be quite a bit more to this, but this is the part that i'm stuck on.

Thanks in advance!




Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: June 1, 2006 at 08:59:51 Pacific
Reply:

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


0

Response Number 2
Name: Michael J (by mjdamato)
Date: June 1, 2006 at 11:35:21 Pacific
Reply:

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


0

Response Number 3
Name: glenmoore670
Date: June 1, 2006 at 14:38:51 Pacific
Reply:

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 strIPaddress

strClubNumber = 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!



0

Response Number 4
Name: Michael J (by mjdamato)
Date: June 1, 2006 at 15:45:26 Pacific
Reply:

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
Loop

Michael J


0

Response Number 5
Name: Michael J (by mjdamato)
Date: June 1, 2006 at 15:46:32 Pacific
Reply:

Whoops! You can remove the "Wscript.echo strline" at the end of the 5th line above.

Michael J


0

Related Posts

See More



Response Number 6
Name: StuartS
Date: June 1, 2006 at 16:22:11 Pacific
Reply:

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 String

strClubNumber = inputbox ("Please enter the 4 digit club number: ")

Open Filename For Input As #1
Do
input #1, Number, IPaddress
Loop While Number <> strClubNumber
Close #1

IP Address will be in the variable IPAddress

A lot simpler than all that FSO crap and a hell of a lot faster.

Stuart


0

Response Number 7
Name: glenmoore670
Date: June 8, 2006 at 08:21:03 Pacific
Reply:

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 strIPaddress

strClubNumber = 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!



0

Response Number 8
Name: Michael J (by mjdamato)
Date: June 8, 2006 at 11:36:12 Pacific
Reply:

You just had some formatting errors. I have corrected it and condensed it for you:

dim strClubNumber, strIPaddress, foundIP

strClubNumber = 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
Loop

wscript.echo ("The IP is " & foundIP)

Michael J


0

Response Number 9
Name: glenmoore670
Date: June 8, 2006 at 12:20:39 Pacific
Reply:

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

Thanks again!


0

Response Number 10
Name: Mechanix2Go
Date: June 8, 2006 at 22:43:08 Pacific
Reply:

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



0

Response Number 11
Name: Michael J (by mjdamato)
Date: June 8, 2006 at 22:58:56 Pacific
Reply:

& n b s p ;

(without the spaces, of course)

Michael J


0

Response Number 12
Name: Mechanix2Go
Date: June 8, 2006 at 23:19:17 Pacific
Reply:

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



0

Response Number 13
Name: Michael J (by mjdamato)
Date: June 9, 2006 at 14:32:57 Pacific
Reply:

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 B

Now 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


0

Response Number 14
Name: Mechanix2Go
Date: June 9, 2006 at 18:55:18 Pacific
Reply:

@echo off

:main
echo this is main
goto :eof

:eof



=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 15
Name: Mechanix2Go
Date: June 9, 2006 at 18:59:36 Pacific
Reply:

hmmm

It looked OK in preview.



=====================================
If at first you don't succeed, you're about average.

M2



0

Sponsored Link
Ads by Google
Reply to Message Icon






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: Simple VB text file query? Help!

VB text files www.computing.net/answers/programming/vb-text-files/8304.html

vb text file question www.computing.net/answers/programming/vb-text-file-question/8376.html

Help browsing a text file www.computing.net/answers/programming/help-browsing-a-text-file/9929.html