#126450 - 2004-09-10 11:22 PM
Re: Importing Environmental Variables
|
xpanmanx
Starting to like KiXtart
Registered: 2002-07-08
Posts: 108
Loc: St. Louis MO USA
|
That snippet was a cut-and-paste from the manual. My testing code used apostophes.
|
|
Top
|
|
|
|
#126451 - 2004-09-10 11:23 PM
Re: Importing Environmental Variables
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Code:
Break On
$rc=SetOption( "NoVarsInStrings", "ON" )
If Open(1,'c:\data\scripts\junk.txt') = 0
$raw_line = ReadLine(1)
While @ERROR = 0
$raw_line = Trim(SubStr(Lcase($raw_line),5))
$var_name = left($raw_line, instr($raw_line,"=")-1)
$var_value = substr($raw_line, instr($raw_line,"=")+1)
;? "$"+$var_name+"="+$var_value
$rc=execute ("$" + $var_name + "='" + $var_value + "'")
$raw_line = ReadLine(1)
Loop
$rc=Close(1)
EndIf
? '___________________________________________________________________'
?
? "Output:"
? "$autodeploy_process_type=" + $autodeploy_process_type
? "$timezone=" + $timezone
? "$days_to_retain_full_workstation_backup=" + $days_to_retain_full_workstation_backup
? "$distribution_medium=" + $distribution_medium
? "$serialnum=" + $serialnum
? "$machinetype=" + $machinetype
? "$comppath=" + $comppath
or
Code:
Break On
$rc=SetOption( "NoVarsInStrings", "ON" )
If Open(1,'c:\data\scripts\junk.txt') = 0
$raw_line = ReadLine(1)
While @ERROR = 0
$mod_line = Trim(Lcase(SubStr(left($raw_line, instr($raw_line,"=")),5) +
"'" + substr($raw_line, instr($raw_line,"=")+1) + "'"))
$rc=execute ("$" + $mod_line)
$raw_line = ReadLine(1)
Loop
$rc=Close(1)
EndIf
? '___________________________________________________________________'
?
? "Output:"
? "$autodeploy_process_type=" + $autodeploy_process_type
? "$timezone=" + $timezone
? "$days_to_retain_full_workstation_backup=" + $days_to_retain_full_workstation_backup
? "$distribution_medium=" + $distribution_medium
? "$serialnum=" + $serialnum
? "$machinetype=" + $machinetype
? "$comppath=" + $comppath
Edited by Howard Bullock (2004-09-10 11:34 PM)
|
|
Top
|
|
|
|
#126452 - 2004-09-10 11:23 PM
Re: Importing Environmental Variables
|
Les
KiX Master
   
Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
|
|
|
Top
|
|
|
|
#126453 - 2004-09-10 11:29 PM
Re: Importing Environmental Variables
|
maciep
Korg Regular
   
Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
|
Assuming that each line starts with 'set ' then this script should work just fine for you. Just try running this and see what you get (obviously change the file)
Code:
$ = open(1,"c:\blah.txt")
$curLine = substr(readline(1),5)
while @error = 0
$firstEqualPos = instr($curLine, '=')
$varName = substr($curLine,1,$firstEqualPos-1)
$val = '"' + substr($curLine,$firstEqualPos+1) + '"'
$ = execute("$$" + $varName + "=" + $val)
$curLine = substr(readline(1),5)
loop
$ = close(1)
? $AutoDeploy_Process_Type
? $TIMEZONE
? $DAYS_TO_RETAIN_FULL_WORKSTATION_BACKUP
? $DISTRIBUTION_MEDIUM
? $SERIALNUM
? $MACHINETYPE
? $CompPath
my blah.txt file looks like this
Code:
set AutoDeploy_Process_Type=Attended Network-based Migration
SET TIMEZONE=003
SET DAYS_TO_RETAIN_FULL_WORKSTATION_BACKUP=30
Set DISTRIBUTION_MEDIUM=Network
SET SERIALNUM=7GCXZ01
SET MACHINETYPE=COMPUTERS-XP
set CompPath=CN=7GCXZ01,OU=Outlook Mail Client,OU=Employees,OU=CORP,DC=CORP1,DC=CORP,DC=COM
And here's the output i get
Quote:
Attended Network-based Migration
003
30
Network
7GCXZ01
COMPUTERS-XP
CN=7GCXZ01,OU=Outlook Mail Client,OU=Employees,OU=CORP,DC=CORP1,DC=CORP,DC=COM
or howard's should work just fine too
Edited by maciep (2004-09-10 11:35 PM)
_________________________
Eric
|
|
Top
|
|
|
|
#126456 - 2004-09-13 10:03 PM
Re: Importing Environmental Variables
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
If you have SetOption('NoVarsInStrings','On') then you only need a single "$" in the line:
Execute("$$" + "$var_name" + "='" + $var_value + "'")
and no quotes around "$var_name" as this will be literal and no variable substitution performed.
Please see my posted code several replies above.
Edited by Howard Bullock (2004-09-13 10:04 PM)
|
|
Top
|
|
|
|
#126458 - 2004-09-13 10:26 PM
Re: Importing Environmental Variables
|
xpanmanx
Starting to like KiXtart
Registered: 2002-07-08
Posts: 108
Loc: St. Louis MO USA
|
maciep,
(Please forgive the "template" look of this reply )
I've done a cut-and-paste on your code in post #125507, changing only the path and filename of the data file.
The code example beeps out in error without producing any visible output.
Thanks,
Tim ==
|
|
Top
|
|
|
|
#126459 - 2004-09-13 10:32 PM
Re: Importing Environmental Variables
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Well from the manual:
A variable name: • Can't contain operator characters (+,-,*,/,&,<,>,=) • Can’t contain a period • Must be unique within the same scope, which is the range from which the variable can be referenced in a script, or script segment.
|
|
Top
|
|
|
|
#126461 - 2004-09-13 11:01 PM
Re: Importing Environmental Variables
|
xpanmanx
Starting to like KiXtart
Registered: 2002-07-08
Posts: 108
Loc: St. Louis MO USA
|
I guess I should cut my losses and move on. At this time I can't redesign the uplevel product to remove the operands from the variable names. Fortunately those names are acceptable in DOS batch. The next time I crack open that code I'll clean the variable names.
You'll see that I've added a trap to catch and ignore variable names which contain operands...my final code looks like this:
Code:
Break on
Color c+/n
$dummy = SetOption('novarsinstrings','on') ;$dummy = SetOption('explicit','on') -- can't use explicit without dimming each $var_name $dummy = SetOption('wrapateol','on')
If Open(1,'c:\temp\envars.bat') = 0
$raw_line = Trim(SubStr(Lcase(ReadLine(1)),5))
While @error = 0
$var_name = '' $var_value = '' $dummy = ''
;provide special handling for ldap path If InStr($raw_line,'comppath') For Each $element in Split($raw_line,'comppath' + Chr(61)) $var_name = 'comppath' $var_value = $element Next GoTo set_var EndIf
For Each $element in Split($raw_line,Chr(61)) Select Case $var_name = '' $var_name = $element Case $var_value = '' $var_value = $element EndSelect Next
If InStr($var_name,'-') ? $var_name Color r+/n ' $var_name contains operand - skipped' Color c+/n GoTo next_line EndIf
:set_var ? '$' + $var_name + '=' + $var_value If $var_value = '' Color r+/n "null - skipped" Color c+/n GoTo next_line EndIf
$dummy = Execute ("$" + $var_name + "='" + $var_value + "'")
:next_line $raw_line = Trim(SubStr(Lcase(ReadLine(1)),5))
Loop
Close(1)
EndIf
Thank you, everyone, for your help.
Best regards,
Tim ==
|
|
Top
|
|
|
|
#126462 - 2004-09-14 02:14 AM
Re: Importing Environmental Variables
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
If it would be acceptable, we can substitute a different "valid" character for all the illegal characters that are encountered.
|
|
Top
|
|
|
|
#126465 - 2004-09-14 10:16 PM
Re: Importing Environmental Variables
|
xpanmanx
Starting to like KiXtart
Registered: 2002-07-08
Posts: 108
Loc: St. Louis MO USA
|
Okay, now I'm in waay over my head.
One of the several things I'm trying to do with this code, is to grab a value from a text file, set it as a Kix variable, then mod @date against the variable using Jochen's DateCalc UDF.
Net/net is that I'm trying to obtain a calendar date X days in the future, where "X" is the numerical value taken from the 'set days_to_retain_full_workstation_backup=x' line in the text file, as $days_to_retain_full_workstation_backup in the KIX script.
Code:
;DateCalc=======================================================================================
Function DateCalc($date1, $DateOrMod, optional $SingleDigit)
Dim $_intDate1, $_intYear1, $_intMonth1, $_intDay1
Dim $_intDate2, $_intYear2, $_intMonth2, $_intDay2
$date1 = Split($date1,'/')
If Ubound($date1) <> 2
Exit 1
EndIf
$_intYear1 = Val($date1[0])
$_intMonth1 = Val($date1[1])
$_intDay1 = Val($date1[2])
If $_intMonth1 < 3
$_intMonth1 = $_intMonth1 + 12
$_intYear1 = $_intYear1 - 1
EndIf
$_intDate1 = $_intDay1 + ( 153 * $_intMonth1 - 457 ) / 5 + 365 * $_intYear1 +
$_intYear1 / 4 - $_intYear1 / 100 + $_intYear1 / 400 - 306
Select
Case VarType($DateOrMod) = 3
$_intDate2 = $_intDate1 + $DateOrMod
If InStr($_intDate2,'-') $_intDate2 = Val(SubStr($_intDate2,2,Len($_intDate2)-1)) EndIf
$_intYear2 = ( 100 * ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
) + (100*($_intDate2+306)-25)
) / 36525
$_intMonth2 = ( 5 * ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
) + 456
) / 153
$_intDay2 = ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
) - ( 153 * $_intMonth2 - 457
) / 5
If $_intMonth2 > 12 $_intYear2 = $_intYear2 + 1 $_intMonth2 = $_intMonth2 - 12 EndIf
If NOT $SingleDigit
If Len($_intYear2 ) < 4
$_ = Execute("for $i=1 to 4-len($$_intYear2) $$_intYear2 = '0' + $$_intYear2 next")
EndIf
$_intMonth2 = Right("0" + $_intMonth2,2)
$_intDay2 = Right("0" + $_intDay2,2)
EndIf
$DateCalc = '$_intYear2/$_intMonth2/$_intDay2'
Case VarType($DateOrMod) = 8
$DateOrMod = Split($DateOrMod,'/')
If Ubound($DateOrMod) <> 2
Exit 1
EndIf
$_intYear2 = Val($DateOrMod[0])
$_intMonth2 = Val($DateOrMod[1])
$_intDay2 = Val($DateOrMod[2])
If $_intMonth2 < 3
$_intMonth2 = $_intMonth2 + 12
$_intYear2 = $_intYear2 - 1
EndIf
$_intDate2 = $_intDay2 + ( 153 * $_intMonth2 - 457 ) / 5 + 365 * $_intYear2 +
$_intYear2 / 4 - $_intYear2 / 100 + $_intYear2 / 400 - 306
$DateCalc = $_intDate1 - $_intDate2
;comment the next line if you wish to return negative results also !!!
If InStr($DateCalc,'-') $DateCalc = Val(SubStr($DateCalc,2,Len($DateCalc)-1)) EndIf
Case 1
Exit 1
EndSelect
EndFunction
;===============================================================================================
Break on
Color c+/n
$dummy = SetOption('novarsinstrings','on')
;$dummy = SetOption('explicit','on') -- can't use explicit without dimming each $var_name
$dummy = SetOption('wrapateol','on')
If Open(1,'c:\temp\envars.bat') = 0
$raw_line = Trim(SubStr(Lcase(ReadLine(1)),5))
While @error = 0
$var_name = ''
$var_value = ''
$dummy = ''
;provide special handling for ldap path
If InStr($raw_line,'comppath')
For Each $element in Split($raw_line,'comppath' + Chr(61))
$var_name = 'comppath'
$var_value = $element
Next
GoTo set_var
EndIf
For Each $element in Split($raw_line,Chr(61))
Select
Case $var_name = '' $var_name = $element
Case $var_value = '' $var_value = $element
EndSelect
Next
If InStr($var_name,'-')
? $var_name Color r+/n ' $var_name contains operand - skipped' Color c+/n
GoTo next_line
EndIf
:set_var
? '$' + $var_name + '=' + $var_value
If $var_value = ''
Color r+/n "null - skipped" Color c+/n
GoTo next_line
EndIf
$dummy = Execute ("$" + $var_name + "='" + $var_value + "'")
:next_line
$raw_line = Trim(SubStr(Lcase(ReadLine(1)),5))
Loop
Close(1)
EndIf
;===============================================================================================
@CRLF + @CRLF
? '$days_to_retain_full_workstation_backup=' + $days_to_retain_full_workstation_backup
$begin_prompting_calc = DateCalc(@date, $days_to_retain_full_workstation_backup)
? '$begin_prompting_calc=' + $begin_prompting_calc
@CRLF + @CRLF
Get $
The problem is that $begin_prompting_calc is returning empty.
If I set NOVARSINSTRINGS to 'ON', $begin_prompting_calc appears as $_intYear2/$_intMonth2/$_intDay2 .
Any ideas?
Edited by xpanmanx (2004-09-14 11:11 PM)
|
|
Top
|
|
|
|
#126466 - 2004-09-15 10:52 AM
Re: Importing Environmental Variables
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
Any ideas?
Yes, you need to cast the adjustment day value to an integer (it will be a string).
Here is a complete solution.
Note the following enhancements:
- Imported variable names are prefixed with an underscore to avoid clashes with your own variables - this is documented in the script and you can change it if you wish.
- Illegal characters in variable names are replaced with underscores.
- The variable parse routine will handle values with embedded quotes (' and " characters)
- Works with SetOption("Explicit","ON") - variables are declared with global scope.
Give it a go and let us know how you get on.
Code:
Break ON
$=SetOption("Explicit","ON")
Dim $sVarsPath,$fdVars
Dim $sLine,$s
Dim $sVarName,$sVarValue
Dim $sAllowed,$iIndex
$sAllowed="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_"
$fdVars=FreeFileHandle()
If Not $fdVars funFatal("Cannot get free file handle") EndIf
$sVarsPath="c:\temp\envvars.bat"
If Open($fdVars,$sVarsPath) funFatal("Cannot open file '"+$sVarsPath+"' for reading.") EndIf
; Load environment variables.
$sLine=ReadLine($fdVars)
While @ERROR=0
If Left($sLine,4)="SET "
$iIndex=InStr($sLine,"=")
If Not $iIndex $iIndex=Len($sLine)+1 EndIf
$s=SubStr($sLine,5,$iIndex-5)
$sVarName="_" ; Start variable name with "_" to avoid namespace clashes
; Remove any illegal characters from variable name
While $s <> ""
If InStr($sAllowed,Left($s,1))
$sVarName=CStr($sVarName)+Left($s,1)
Else
$sVarName=CStr($sVarName)+"_"
EndIf
$s=SubStr($s,2)
Loop
; Create global variable
$=Execute("Global $$"+$sVarName)
$sVarValue=SubStr($sLine,$iIndex+1)
; Remove any tricky quote marks from value and assign to KiXtart variable
$=Execute("$$"+$sVarName+"='"+Join(Split($sVarValue,"'"),"'+Chr("+Asc("'")+")+'")+"'")
EndIf
$sLine=ReadLine($fdVars)
Loop
$=Close($fdVars)
"Begin prompting at "+DateCalc(@DATE,Cint($_days_to_retain_full_workstation_backup)) ?
Function funFatal($s)
CStr($s)+@CRLF
If @ERROR "Reason: ["+@ERROR+"] "+@SERROR+@CRLF EndIf
Quit @ERROR
EndFunction
;DateCalc=======================================================================================
Function DateCalc($date1, $DateOrMod, optional $SingleDigit)
Dim $_intDate1, $_intYear1, $_intMonth1, $_intDay1
Dim $_intDate2, $_intYear2, $_intMonth2, $_intDay2
$date1 = Split($date1,'/')
If Ubound($date1) <> 2
Exit 1
EndIf
$_intYear1 = Val($date1[0])
$_intMonth1 = Val($date1[1])
$_intDay1 = Val($date1[2])
If $_intMonth1 < 3
$_intMonth1 = $_intMonth1 + 12
$_intYear1 = $_intYear1 - 1
EndIf
$_intDate1 = $_intDay1 + ( 153 * $_intMonth1 - 457 ) / 5 + 365 * $_intYear1 +
$_intYear1 / 4 - $_intYear1 / 100 + $_intYear1 / 400 - 306
Select
Case VarType($DateOrMod) = 3
$_intDate2 = $_intDate1 + $DateOrMod
If InStr($_intDate2,'-') $_intDate2 = Val(SubStr($_intDate2,2,Len($_intDate2)-1)) EndIf
$_intYear2 = ( 100 * ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
) + (100*($_intDate2+306)-25)
) / 36525
$_intMonth2 = ( 5 * ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
) + 456
) / 153
$_intDay2 = ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
) - ( 153 * $_intMonth2 - 457
) / 5
If $_intMonth2 > 12 $_intYear2 = $_intYear2 + 1 $_intMonth2 = $_intMonth2 - 12 EndIf
If NOT $SingleDigit
If Len($_intYear2 ) < 4
$_ = Execute("for $i=1 to 4-len($$_intYear2) $$_intYear2 = '0' + $$_intYear2 next")
EndIf
$_intMonth2 = Right("0" + $_intMonth2,2)
$_intDay2 = Right("0" + $_intDay2,2)
EndIf
$DateCalc = '$_intYear2/$_intMonth2/$_intDay2'
Case VarType($DateOrMod) = 8
$DateOrMod = Split($DateOrMod,'/')
If Ubound($DateOrMod) <> 2
Exit 1
EndIf
$_intYear2 = Val($DateOrMod[0])
$_intMonth2 = Val($DateOrMod[1])
$_intDay2 = Val($DateOrMod[2])
If $_intMonth2 < 3
$_intMonth2 = $_intMonth2 + 12
$_intYear2 = $_intYear2 - 1
EndIf
$_intDate2 = $_intDay2 + ( 153 * $_intMonth2 - 457 ) / 5 + 365 * $_intYear2 +
$_intYear2 / 4 - $_intYear2 / 100 + $_intYear2 / 400 - 306
$DateCalc = $_intDate1 - $_intDate2
;comment the next line if you wish to return negative results also !!!
If InStr($DateCalc,'-') $DateCalc = Val(SubStr($DateCalc,2,Len($DateCalc)-1)) EndIf
Case 1
Exit 1
EndSelect
EndFunction
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 811 anonymous users online.
|
|
|