devxlogo

Extracting Parameters from a File Using Regular Expressions

Extracting Parameters from a File Using Regular Expressions

Extracting parameters from a file using regular expressions can be a very effective and practical way of doing things. Suppose, for instance, that you need a logon script that maps drives depending on the computer’s sites. A custom parameter file could look like this:

Site1.MapDrive_X=\server1share1Site1.MapDrive_Y=\server1share2Site2.MapDrive_X=\server3share1Site3.MapDrive_Z=\server4share4

Extracting those parameters would be as easy as getting the actual site name, building a regular expression, and looping on found matches. Assuming that you’ve got a ParameterFileContent variable already available, you’d have something like this:

set oRegExp = new RegExpoRegExp.Global = True 'To get all matchesoRegExp.IgnoreCase = True 'We don't want to bother with case in our parameter file'Mapping drives for the current ActiveDirectory siteSet oAdSysInfo = CreateObject("ADSystemInfo")oRegExp.Pattern = oAdSysInfo.SiteName & ".MapDrive_(w)=(.*)" Set oMatchCol = oRegExp.Execute(ParameterFileContent)For Each oMatch In oMatchCol '  DriveName = oMatch.SubMatches(0) 'The w from the first set of parenthesis  SharePath = oMatch.SubMatches(1) 'The .* from the second set of parenthesis  WScript.Echo("DriveName=" & DriveName & " SharePath=" & SharePath)  WshNetwork.MapNetworkDrive DriveName & ":",SharePath,FalseNextReuse the oRegExp object to process any other needed parameter...GroupName2.RunApplication=\server1share1Application1Application1.exeGroupName2.MapNetworkDrive_X=\server1share1  Site1.SQLServerName=SQL01

devx-admin

Share the Post: