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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read text file

Status
Not open for further replies.

ptuck

MIS
Aug 8, 2003
130
0
0
US
I have a text file (computername.txt) that has 50 computer names. I want to read each computer name from the text file one at a time and perform an action on each machine. The computer names were originally in an excel spreadsheet and I just copies and pasted them to notepad. Not sure if I could open an excel file. What is the best way to do this?

Data in text file is formated like below:
computer01
computer02
computer03

They are all 10 characters. Thanks for the help.
 
Here is the way to read them from a text file:

Private Sub Command1_Click()
Dim InputData As String
Open "C:\Test.txt" For Input As #1

Do Until EOF(1)
Line Input #1, InputData
MsgBox InputData
Loop

MsgBox "Done!", vbInformation
End Sub

I will post an Excel solution shortly.

Swi
 
Hello,

you may try this MSDN link to get a few examples of the File System Object :

MSDN - FSO Object Model

The "Working with Files" section may be interesting.

Or use "File System Object" as keyword for a search in this same forum.

Hope this helps.

Droops
 
Here is an example using ADO to read an Excel sheet:

Private Sub Command1_Click()
'==============================================================
' Add a reference to Microsoft ActiveX Data Objects X.X Library
'==============================================================
' Declare variables
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset

' Initialize variables
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset

' Establish connection
With conn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Properties("Extended Properties").Value = "Excel 8.0;HDR=NO"
.Open "C:\Testit1.xls"
End With

' Populate recordset
rs.Open "SELECT * FROM [Sheet1$]", conn, adOpenStatic, adLockOptimistic

' Loop through data from spreadsheet
Do Until rs.EOF
MsgBox rs.Fields(0).Value
rs.MoveNext
Loop

' Close and destroy objects
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing

MsgBox "Done!", vbInformation
End Sub


Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top