Page 1 of 1 1
Topic Options
#206995 - 2013-03-28 07:09 PM IF statement
joshuaer Offline
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.
_________________________
www.omfgservers.com

Top
#206996 - 2013-03-28 08:23 PM Re: IF statement [Re: joshuaer]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
 Code:
If @USERID = "tuser"
  ;do stuff
EndIf

Top
#206997 - 2013-03-28 08:44 PM Re: IF statement [Re: Arend_]
Glenn Barnas Administrator Offline
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:
 Code:
; 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:
 Code:
$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:
 Code:
[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! \:D

Top
#206999 - 2013-03-28 10:09 PM Re: IF statement [Re: Glenn Barnas]
Arend_ Moderator Offline
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 Offline
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.' ?
_________________________
www.omfgservers.com

Top
#207002 - 2013-03-29 04:25 PM Re: IF statement [Re: joshuaer]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Use the full path to the ini file \:\)
Top
#207003 - 2013-03-29 04:31 PM Re: IF statement [Re: Arend_]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
As Arend says - full path, or at least ".\UserClothes.ini" to define it to be in the current directory.

Also, for the lookup to work, you'll need valid user names unless you're in Mr. Slate's Quarry. ;\)

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#207004 - 2013-03-29 05:11 PM Re: IF statement [Re: Glenn Barnas]
joshuaer Offline
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")
_________________________
www.omfgservers.com

Top
#207005 - 2013-03-29 05:44 PM Re: IF statement [Re: joshuaer]
Glenn Barnas Administrator Offline
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:
 Code:
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! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 756 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.043 seconds in which 0.017 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org