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

Parse/Loop string 3

Status
Not open for further replies.

JadeKnight

IS-IT--Management
Feb 23, 2004
94
NO
I'm creating a script wich is going to read some variables from a txt file. To make the script even more usefull, I want to include computer variables in the text file. Making vbscript expand these.

However, I'm a bit stuck on how I should parse the txt string. I think this should be done in a loop, because it's impossible to know how many computer variables wich will be used in value of variable. Any help would be appreciated.

Here's what I've played with :
Code:
sLocalStore = ""
sLine = "sLocalStore=%ProgramFiles%\somepath\%computername%"


If Left(sLine, InStr(sLine, "=") -1) = sLocalStore Then
	sLocalStore = Mid(sLine, InStr(sLine, "=") +1) '%ProgramFiles%\somepath\%computername% assigned to sLocalStore
End If

'What now?

Code above is a tmp sample, but it illustrates my point... After assigning the value to sLocalStore, how can I parse the string, and successfully manage to use : ExpandEnvironmentStrings with all variables in string.
 
Set dicData = CreateObject("Scripting.Dictionary")
Do While Not TxtFile.AtEndOfStream
sLine = TxtFile.ReadLine
If sLine <> "" Then
intEquals = InStr(1, sLine, "=")
If (intEquals <= 1) Then
Else
sKey = Left(sLine, intEquals - 1)
sVal =Right(sLine, Len(sLine) - intEquals)
dicData.Add sKey, WshShell.ExpandEnvironmentStrings(sVal)
End If
End If
Loop

'you can then loop through the dic object later???
'if youre thing before the = sign is not unique then try this

Set dicData = CreateObject("Scripting.Dictionary")
Do While Not TxtFile.AtEndOfStream
sLine = TxtFile.ReadLine
If sLine <> "" Then
dicdData.Add dicData.Count + 1, WshShell.ExpandEnvironmentStrings(sLine)
End If
Loop
 
First you've to enclose string by quotes else it is taken as a variable.
>[tt]If Left(sLine, InStr(sLine, "=") -1) = sLocalStore Then[/tt]
[tt]If Left(sLine, InStr(sLine, "=") -1) = [red]"[/red]sLocalStore[red]"[/red] Then[/tt]

A more concise approach is this if the regularity of those lines warrant.
[tt]
sLocalStore = ""
sLine = "sLocalStore=%ProgramFiles%\somepath\%computername%"

dim a, b, Line_ex, i
dim wshshell

a=split(sLine,"=")
b=split(trim(a(1)),"\")

set wshshell=createobject("wscript.shell")
for i=0 to ubound(b)
b(i)=wshshell.expandenvironmentstrings(trim(b(i)))
next
set wshshell=nothing

a(1)=join(b,"\")
sLine_ex=join(a,"=")
wscript.echo sLine_ex 'just to show
[/tt]
 
Did not see you posted during writing up, mrmovie. Sorry!
 
Thx to both of you...

The solution provided by tsuji was the best one in my case. I had allready created the routines for read/loop file, strings etc. I used an array, but the dictionary was also a nice approach. However, I was only stuck on how I should actually treat the string. Example by tsjui was exactly what I was looking for.

Cheers :)
 
tsuji, why the need to split the b string by "\" then use WshShell.Expand on all elements?
 
You're right, mrmovie. It does not need that splitting. Just did not test the script and wrote it mentally. The expansion is spreading out automatically. So JadeKnight should take into account of this note as well. You have my vote for the observation and thanks.
 
As an after-thought, to push the observation a step more, why split the "=" as well? Just expand the whole stuff!
[tt] sLine_ex=wshshell.expandenvironmentstrings(sLine)[/tt]
(Maybe I should hand back the vote of mine, or am too at ease at split and join!)
 
Nice, mrmovie :)... I wasn't aware of that. "Keep it simple" is a nice motto :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top