#206995 - 2013-03-28 07:09 PM
IF statement
|
joshuaer
Fresh Scripter
Registered: 2006-02-09
Posts: 15
|
Hello
I want to create a statement that says
IF %USERNAME% matches the person who is logged in then use this info
username = tuser
info to be used = ABC30
So my thought is in my script have a list of the usernames and their corresponding info for the script to match.
|
|
Top
|
|
|
|
#206996 - 2013-03-28 08:23 PM
Re: IF statement
[Re: joshuaer]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
If @USERID = "tuser"
;do stuff
EndIf
|
|
Top
|
|
|
|
#206997 - 2013-03-28 08:44 PM
Re: IF statement
[Re: Arend_]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
To match a user to corresponding data, you can use two arrays if the data is simple. A larger data set should use an external data file.
Let's start simply:; Define an array of user names
$aUsers = 'fred','wilma','barney','betty'
; an array of data that matches each user - in this case, the color of their clothes
$aUData = 'orange','white','brown','green'
; search the user array for the current user id
$Index = AScan($aUsers, @USERID)
If $Index < 0 ; not found
$Color = 'unknown'
Else
; define color from the data array based on the userID found in the array
$Color = $aUData[$Index]
EndIf
@USERID ' wears ' $Color ' clothes.' ? This illustrates a simple two-array method. One array is the userID and the other is the associated data. A two-dimensional array might be better but is a little harder to initialize. Much better, and more direct, is an external data file. Kix can natively manipulate INI files, so here's an example:$Color = ReadProfileString('UserClothes.ini', 'USERS', @USERID)
If $Color = ''
$Color = 'unknown' ; not found in data file, show unknown
EndIf
@USERID ' wears ' $Color ' clothes.' ? Notice that this code is very simple and requires no initialization. Another advantage is that anyone can edit the data file - no programming skill is required. Here's the data file format:[USERS]
fred=orange
Wilma=white
barney=brown
betty=green Just add the userIDs to the file and the associated data value as needed.
Keeping your data separate from your program is generally a very good practice.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#206999 - 2013-03-28 10:09 PM
Re: IF statement
[Re: Glenn Barnas]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
Stop it Glenn, you're scaring newcomers off!
|
|
Top
|
|
|
|
#207001 - 2013-03-29 03:42 PM
Re: IF statement
[Re: Arend_]
|
joshuaer
Fresh Scripter
Registered: 2006-02-09
Posts: 15
|
Hello Glenn
Thank you for the script, the top one works perfect but the second script I cannot seem to get it working correctly. It is not reading the information from the .ini files for the USERID, is there something I am missing in this?
Thanks again
$Color = ReadProfileString('UserClothes.ini', 'USERS', @USERID) If $Color = '' $Color = 'unknown' ; not found in data file, show unknown EndIf @USERID ' wears ' $Color ' clothes.' ?
|
|
Top
|
|
|
|
#207002 - 2013-03-29 04:25 PM
Re: IF statement
[Re: joshuaer]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
Use the full path to the ini file
|
|
Top
|
|
|
|
#207004 - 2013-03-29 05:11 PM
Re: IF statement
[Re: Glenn Barnas]
|
joshuaer
Fresh Scripter
Registered: 2006-02-09
Posts: 15
|
Sorry, I pasted the wrong version, I am using the full path with no luck..
I also am using my username in the file. If I put in the first script you gave me it works great. But I was wanting to use the separate .ini to make managing the users easier.
Break on $Color = ReadProfileString("C:\script\UserClothes.ini", "USERS", "@USERID")
$short = left(@userid,8) $inifile = "C:\Windows\Fsp.ini" $value = ReadProfileString($inifile, "UBS", "LastUser=")
$rc = WriteProfileString($inifile, "UBS", "LastUser", "$short")
$value = ReadProfileString($inifile, "UBS", "PCAlias=")
$rc = WriteProfileString($inifile, "UBS", "PCAlias", "$Color")
|
|
Top
|
|
|
|
#207005 - 2013-03-29 05:44 PM
Re: IF statement
[Re: joshuaer]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
The example I posted (with the ".\" prefix) works as shown.
Your code has some issues:
1. the $Color = ReadProfileString line should NOT place @USERID in quotes. You should also adjust this so it properly represents what you want, not a direct copy from my example. The other lines also place variables inside of quotes, which is STRONGLY discouraged.
2. The $value = ReadProfileString lines are not correct. You should NOT have an equal sign in the last value! The way you have specified this will return nulls all the time.
3. The second $value = ReadProfileString line doesn't do anything, unless you are using $Value later in the script (and not shown in your example).
You say you want to write a parameter to a file based on the UserID. Try this:Break on
; Get the attribute associated with the current user
$Attrib = ReadProfileString("C:\script\UserAttributes.ini", "USERS", @USERID)
; get the short name.
$short = left(@USERID,8)
; define the local INI file
$inifile = "C:\Windows\Fsp.ini"
; Not sure why you are reading this, as the next line overwrites it - Not needed?
; $value = ReadProfileString($inifile, "UBS", "LastUser")
; Update the local INI file with the user's short name and attribute
$rc = WriteProfileString($inifile, "UBS", "LastUser", $short)
$rc = WriteProfileString($inifile, "UBS", "PCAlias", $Attrib) If this is running during logon, then the UserAttributes.ini should be placed in NetLogon so it is available to all users (use ".\UserAttributes.ini").
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 756 anonymous users online.
|
|
|