Solved

Read External File in VBScript

Posted on 2006-12-01
16
590 Views
Last Modified: 2010-08-05

Ok, this question it very simple just any one example.

I whant read one external file (Data.log). This file have four column and many lines like this.:

    First Name        Last Name     Movielphone            HomePhone
         Peter             Gomez     (305)555-5555        (305) 444-4444
         Anni              Gomez     (305)555-5555        (305) 444-4444

In this script have four variable   First Name, Last Name, Movielphone and  HomePhone. I need to read all value in my external file for create other process.
Example.:

WshS.run ".\delprofi.bat " & First Name & Last Name & Movielphone & HomePhone

Thanks

0
Comment
Question by:HelpDeskCentennialrd
  • 9
  • 5
16 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 18054040
You can use the Windows Shell Runtime, using the FileSSystemObject:

Dim fso

Set fso = CreateObject("Scripting.FileSystemObject")

you can learn about how to use the FileSystemObject at this Microsoft link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/a3f17c7a-69a2-47d8-8b40-6a74646f4e1d.asp

and also, for the complete overview:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/0fa93e5b-b657-408d-9dd3-a43846037a0e.asp

AW
0
 

Author Comment

by:HelpDeskCentennialrd
ID: 18054367
Thanks for your comments but my questions it more How I can Read one external file and get the value for my variables if the data to be in columns like my example.
0
 
LVL 3

Expert Comment

by:JRockSolid
ID: 18054474
Are those tab delimited?  Ex   FirstName <TAB> LastName
0
 
LVL 3

Expert Comment

by:JRockSolid
ID: 18054728
Does This Work
------------------------------Untested ...Very Busy today

Const objDBlist = "C:\text.txt"
Const ForReading = 1
Const BatchFile = "C:\Batch.bat"

Public LastName(),FirstName(),Movielphone(),HomePhone()
x=0
Set Wshell = CreateObject("Wscript.Shell")
Set objTextFile = objFSO.OpenTextFile(objPClist, ForReading) '1 ip or hostname per line
      Do Until objTextFile.AtEndOfStream
            strLine = Split(objTextFile.readline,VBtab)
            
            ReDim Preserve FirstName(x)
            ReDim Preserve LastName(x)
            ReDim Preserve Movielphone(x)
            ReDim Preserve HomePhone(x)
            
            LastName(x)= strLine(0)
            FirstName(x) = strLine(1)
            Movielphone(x) = strLine(2)
            HomePhone(x) = strLine(3)
            
            x = x + 1
      Loop
      
For z = 0 To (x -  1)
      Space = " "
      Arguments = Space & Firstname(z) & Space & LastName(z) & Space &  Movielphone(z) & Space & HomePhone(z)
      Wshell.Run (BatchFile & Arguments)
Next
------------------------------------------------------------
0
 

Author Comment

by:HelpDeskCentennialrd
ID: 18061122

Could you check this line ?

Set objTextFile = objFSO.OpenTextFile(objPClist, ForReading) '1 ip or hostname per line

0
 
LVL 3

Expert Comment

by:JRockSolid
ID: 18061233
What did it say when it Sorry, Made a few changes...That what happens when you rush :[
---------------------------
Const objDBlist = "C:\text.txt"
Const ForReading = 1
Const BatchFile = "C:\Batch.bat"

Public LastName(),FirstName(),Movielphone(),HomePhone()
x=0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Wshell = CreateObject("Wscript.Shell")
Set objTextFile = objFSO.OpenTextFile(objDBlist, ForReading) '1 ip or hostname per line
     Do Until objTextFile.AtEndOfStream
          strLine = Split(objTextFile.readline,VBtab)
         
          ReDim Preserve FirstName(x)
          ReDim Preserve LastName(x)
          ReDim Preserve Movielphone(x)
          ReDim Preserve HomePhone(x)
         
          FirstName(x)= strLine(0)
          LastName(x) = strLine(1)
          Movielphone(x) = strLine(2)
          HomePhone(x) = strLine(3)
         
          x = x + 1
     Loop
     
For z = 0 To (x -  1)
     strSpace = " "
     Arguments = strSpace & Firstname(z) & strSpace & LastName(z) & strSpace &  Movielphone(z) & strSpace & HomePhone(z)
     Wshell.Run (BatchFile & Arguments)
Next
-------------------------------------------

Did you get watchdog?
0
 
LVL 3

Expert Comment

by:JRockSolid
ID: 18061251
A couple things about this script....
1) It assumes that these fields are seperated by TABS
2) If there is an empty line in your file ... It will blow up
3) If the files dont exist, it will blow up
4) If the user doesnt have permission to the file it will blow up
5) If there is a line with a missing entry it will blow up
6) If you strap a bomb to it, IT WILL BLOW UP!!!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:HelpDeskCentennialrd
ID: 18073973
I supose when you say IT WILL BLOW UP you takin about this error?

LecturaFile.vbs(21, 11) Microsoft VBScript runtime error: Subscript out of range: '[number: 3]'
0
 

Author Comment

by:HelpDeskCentennialrd
ID: 18073993
yes, i have my copy of watdog
0
 
LVL 3

Expert Comment

by:JRockSolid
ID: 18074035
No, if none of those situations were met then it is something unaccounted for.
Tell me your line 21 so I can sync up with yours.
0
 
LVL 3

Expert Comment

by:JRockSolid
ID: 18074051
If it is where i think it is then double check these:

1) It assumes that these fields are seperated by TABS
2) If there is an empty line in your file ... It will blow up
0
 
LVL 3

Expert Comment

by:JRockSolid
ID: 18074057
AND

All 4 fields must be present :

First Name<TAB>Last Name<TAB>Movielphone<TAB>HomePhone
0
 
LVL 3

Accepted Solution

by:
JRockSolid earned 25 total points
ID: 18217200
Yes, I have interest in this and no points was not what i had in mind.  I thought more like.... A response and another solution if he didnt have tab delimeted files, but definately NOT no points.
0
 

Author Comment

by:HelpDeskCentennialrd
ID: 18219122

If i want to ski the errors, what can I do.?
0
 
LVL 3

Expert Comment

by:JRockSolid
ID: 18220111
Get some skis and jump dude?!?!  I am unsure of what you want here. If there were errors or problems you could give the experts chances to fix them and if you dont its on you.  On the other hand if i have completely misunderstood your comment I am sorry.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Run Powershell Script When New File Is In A Folder 3 63
Using Powershell to monitor file changes then execute a command 5 172
MaxSpan challenge 9 80
groovy example issue 10 90
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now