NETKILLER Posted August 27, 2011 Posted August 27, 2011 Hi Need some help? Need a vbscript to read a txt file and use the info in it as variables in the script. Example of txt file is below Package_location=\\server\packages SYSusername=sysbob SYSpassword=654321 DBlocation=DBserver DBUsername=dbbob SYSpassword=123456 I have got this script but not sure how to get it to do what I want to Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("c:\scripts\name and value.txt", ForReading) Do Until objTextFile.AtEndOfStream strNextLine = objTextFile.Readline arrServiceList = Split(strNextLine , "=") Wscript.Echo "name: " & arrServiceList(0) For i = 1 to Ubound(arrServiceList) Wscript.Echo "value: " & arrServiceList(i) Next Loop What this does is loop through the txt file and displays it in an echo box So reading the first line it would show name: Package_location and then value:\\server\packages what I need it to do is take the value from let’s say Package_location and set that a variable, Hope this is clear thank you for any help you can provide
Steve21 Posted August 27, 2011 Posted August 27, 2011 Honestly not sure why you wouldn't just put it inside script (unless there.'s lots?) But it depends if it's always same amount of lines? If yes you could remove the loop and just cycle through file linking to cars, but that wouldn't work if the files change. If files change you could dynamically create cars on-the-fly but it'd either be random name, or as "left of =" If youcould answer that will try to knock up script later, or tomorrow if you want Steve
NETKILLER Posted August 27, 2011 Author Posted August 27, 2011 there are a lots of scripts so any change would mean lots of changes to the scripts the lines in the txt files would change / get added to over time and development so i would need the script to read / test the value to the right of the = and use the value to the left of the = as the variable each time to get the new value or current values. thank you
NETKILLER Posted August 27, 2011 Author Posted August 27, 2011 thinking about it the values to the right of the = will not change but the values to the leeft of the = will not sure if i have made that clear very late and very tired
ChrisH Posted August 27, 2011 Posted August 27, 2011 Assuming the order of the config is the same in every file I would read the each line into an array and reference it that way Package_location=\\server\packages SYSusername=sysbob SYSpassword=654321 DBlocation=DBserver DBUsername=dbbob SYSpassword=123456 Dim myArray(6) Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("c:\scripts\name and value.txt", ForReading) j = 0 Do Until objTextFile.AtEndOfStream strNextLine = objTextFile.Readline j = j + 1 arrServiceList = Split(strNextLine , "=") Wscript.Echo "name: " & arrServiceList(0) For i = 1 to Ubound(arrServiceList) Wscript.Echo "value: " & arrServiceList(i) myarray(j) = arrServiceList(1) Next Loop For i = 1 to Ubound(myarray) do something with myarray(i) next
NETKILLER Posted August 29, 2011 Author Posted August 29, 2011 Unfortunately ChrisH the txt file may change in the future and if it does then I would be back to square one of changing all the scripts, but thank you
dwhyte85 Posted August 29, 2011 Posted August 29, 2011 Unfortunately ChrisH the txt file may change in the future and if it does then I would be back to square one of changing all the scripts, but thank you With ChrisH's order doesn't matter... as long as they have x=y in format. You could create a multidimensional array to hold x and y, enum and create an array of your defined bits... so could then say after declaration whatever.leftpart and whatever.rightpart and have both bits of information.
NETKILLER Posted August 30, 2011 Author Posted August 30, 2011 Hey all I have been able to write a script that will read the file and write variables; however I have come to the sad conclusion that I would still have to make changes to all the scripts if I was to add to the txt file, is there a “include” command / function in vbscript that works like in PHP? I will be doing a bit of searching to try and find out but if you can help Than kyou
NETKILLER Posted September 1, 2011 Author Posted September 1, 2011 hey thank you all for your help i have come across this ExecuteGlobal, this looks to do what i want so will work on this
robochop Posted January 22, 2014 Posted January 22, 2014 (edited) Hi Chris, not sure if you will get this but taking your below script how would you use it or recode it for a defined array in the vbscript and not from a text file ? Regards Greg Dim myArray(6) Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile("c:\scripts\name and value.txt", ForReading) j = 0 Do Until objTextFile.AtEndOfStream strNextLine = objTextFile.Readline j = j + 1 arrServiceList = Split(strNextLine , "=") Wscript.Echo "name: " & arrServiceList(0) For i = 1 to Ubound(arrServiceList) Wscript.Echo "value: " & arrServiceList(i) myarray(j) = arrServiceList(1) Next Loop For i = 1 to Ubound(myarray) do something with myarray(i) next Edited January 22, 2014 by robochop
ChrisH Posted January 22, 2014 Posted January 22, 2014 To define a static array just use: Dim myArray(6) myArray(0) = "Value1" myArray(1) = "Value2" myArray(2) = "Value3" myArray(3) = "Value4" myArray(4) = "Value5"
robochop Posted January 22, 2014 Posted January 22, 2014 Thanks Chris. It's your actual do and for loops in your script below that i am interested in. So if my array - Dim myArray(6) myArray(0) = "Value1=emailaddress1" myArray(1) = "Value2=emailaddress2" myArray(2) = "Value3=emailaddress3" myArray(3) = "Value4=emailaddress4" myArray(4) = "Value5=emailaddress5" How can i use your below script to read each value and then split by finding the "=" sign and then get the emailaddress value Regards j = 0 Do Until objTextFile.AtEndOfStream strNextLine = objTextFile.Readline j = j + 1 arrServiceList = Split(strNextLine , "=") Wscript.Echo "name: " & arrServiceList(0) For i = 1 to Ubound(arrServiceList) Wscript.Echo "value: " & arrServiceList(i) myarray(j) = arrServiceList(1) Next Loop For i = 1 to Ubound(myarray) do something with myarray(i) next
navyjax2 Posted January 23, 2019 Posted January 23, 2019 (edited) Good post, except you can't use "Loop" before "For" - it isn't supposed to be there. And "Split" doesn't seem to exist for VBScript: https://www.w3schools.com/asp/asp_ref_vbscript_functions.asp#string Except I was able to go back and use it in other areas, so I'm not sure why w3schools left it out. I had an unrelated error, and I thought it was because of that, so I went a different way. I thought this post could benefit from my experience, in case someone else does the same thing I did. So another alternative is that you can use "InStr" to get the index of the equals sign, and then trim using "Right", which gets all the characters after that point in the string for the number of characters you specify, which should be the total characters minus the number of characters that were before and including the equals: https://www.w3schools.com/asp/func_instr.asp https://www.w3schools.com/asp/func_right.asp Dim valuez Dim idx, valueLength idx = InStr(strNextLine, "=") valueLength = Len(strNextLine) - idx valuez = Right(strNextLine, valueLength) Should work for someone, in case they want to go this way for some reason for parsing out values. Edited January 23, 2019 by navyjax2 1
Knil92 Posted January 24, 2019 Posted January 24, 2019 (edited) So i've had a play around with some vbscript and this was what I came up with. Dim variable Dim splitvarvariable=CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\file\location\file.ini",1).readall splitvar=split(variable,"=") msgbox(splitvar(1)) msgbox(splitvar(3)) All you would have to do is change the file.ini to look something like variable1=value1= variable2=value2= so just put an extra = at the end of each line in the file you already have. Then you can use splitvar(1), splitvar(3), splitvar(5) to call the values in the file, and the even numbers will be the tags. Edited January 24, 2019 by Knil92
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now