By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

read file in VB

P: 1
hi,
could any one let me know how to read file thru VB script.

Scenerio is like this:
i am keeping a personal details such as name, age, sex, company, location in a text file called user details for some users and when I input the users name thru input box, a message box shd be appeared with the dtails from file.

i would appreciate if i can get some code.
Dec 15 '06 #1
Share this Question
Share on Google+
1 Reply


axtens
P: 32
I use this function, or one like it.

Expand|Select|Wrap|Line Numbers
  1. Const AS_SYSTEMDEFAULT = -2
  2.  
  3. Function ReadFile( FileName)
  4.     Dim oStream
  5.     Dim sData
  6.  
  7.     sData = vbNullString
  8.  
  9.     Set oStream = oFSO.OpenTextFile(FileName, CONST_READ, False, AS_SYSTEMDEFAULT )
  10.     sData = oStream.ReadAll
  11.     oStream.Close
  12.  
  13.     ReadFile = sData
  14. End Function
  15.  
This reads the entire file into memory. Then I split it into an array and work from there. For example:

Expand|Select|Wrap|Line Numbers
  1. Dim aFile
  2. aFile = Split( ReadFile( "somefile.txt" ), vbNewLine )
  3. WScript.Echo "First line", aFile( 0 )
  4. WScript.Echo "Last line", aFile( UBound( 0 ) )
  5.  
There are many more examples of file reading and writing routines on my weblog, Code-a-holic
Dec 15 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.