#202998 - 2011-08-31 09:38 AM
Adding an incremental line to a text file.
|
GeorgeLittle
Fresh Scripter
Registered: 2011-02-08
Posts: 47
Loc: UK
|
So I have this script that opens up a csv file with a users sid and then a 5 digit id that is then set as a environmental variable for a legacy app.
;Set the string for the filename
$UIDINFO = "c:\Codes2b.csv"
;Check it exists
If Exist ($UIDINFO) = 0
MessageBox ("Error1:Consultant ID File Does Not Exist."+ Chr(13)+
"Please contact IT Support on 1234, as you will not be able to log in,"+Chr(13)+
"You will now be logged off this system!.",
"WARNING MESSAGE", 48, 300)
Else
;Check if the file is of a size greater than 0KB
$UIDSIZE = GetFileSize($UIDINFO)
If $UIDSIZE = 0
MessageBox ("Error2:Consultant ID File Is To Small."+ Chr(13)+
"Please contact IT Support on 1234, as you will not be able to log in,"+Chr(13)+
"You will now be logged off this system!.",
"WARNING MESSAGE", 48, 300)
Sleep 10
EndIf
;Open Up The UID File
$rc = Open(1, $UIDINFO, 2)
$line = ReadLine(1)
;Search for a match for the user
While @ERROR = 0
$line = Split($line, ",")
If $line[0] = @USERID
$uid = $line[1]
EndIF
$line = ReadLine(1)
Loop
;Count the UID string and ensure its populated
$uid_length = Len($UID)
;If statmenet that logs the user off if the UID is not of a suitable length
If $uid_length = "0"
MessageBox ("Error3:No matching Consultant Code Found."+ Chr(13)+
"Please contact IT Support on 1234, as you will not be able to log in,"+Chr(13)+
"You will now be logged off this system!.",
"WARNING MESSAGE", 48, 300)
Sleep 10
Else
Set "CONS_CODE=$UID"
? "Found name with UID: " + $uid
EndIF
What I would like to do is rather than Warn the users that not entry for them can be found is to create a new entry for them in a file increasing the incremental number.
Is this possilbe
Thanks
Edited by Mart (2011-08-31 11:14 AM) Edit Reason: Please use code tags when posting code.
|
Top
|
|
|
|
#203000 - 2011-08-31 11:31 AM
Re: Adding an incremental line to a text file.
[Re: Mart]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
You were missing an "EndIf" as well.
;Set the string for the filename
$UIDINFO = "c:\Codes2b.csv"
;Check it exists
If Exist ($UIDINFO) = 0
MessageBox ("Error1:Consultant ID File Does Not Exist."+ Chr(13)+
"Please contact IT Support on 1234, as you will not be able to log in,"+Chr(13)+
"You will now be logged off this system!.",
"WARNING MESSAGE", 48, 300)
Else
;Check if the file is of a size greater than 0KB
$UIDSIZE = GetFileSize($UIDINFO)
If $UIDSIZE = 0
MessageBox ("Error2:Consultant ID File Is To Small."+ Chr(13)+
"Please contact IT Support on 1234, as you will not be able to log in,"+Chr(13)+
"You will now be logged off this system!.",
"WARNING MESSAGE", 48, 300)
Sleep 10
EndIf
;Open Up The UID File
$rc = Open(1, $UIDINFO, 2)
$line = ReadLine(1)
;Search for a match for the user
While @ERROR = 0
$line = Split($line, ",")
If $line[0] = @USERID
$uid = $line[1]
EndIF
$line = ReadLine(1)
Loop
;Count the UID string and ensure its populated
$uid_length = Len($UID)
;If statement that logs the user off if the UID is not of a suitable length
If $uid_length = "0"
MessageBox ("Error3:No matching Consultant Code Found."+ Chr(13)+
"Please contact IT Support on 1234, as you will not be able to log in,"+Chr(13)+
"You will now be logged off this system!.",
"WARNING MESSAGE", 48, 300)
Sleep 10
Else
Set "CONS_CODE=$UID"
? "Found name with UID: " + $uid
EndIf
EndIf
Please post a line or two of the contents of the CSV file.
|
Top
|
|
|
|
#203001 - 2011-08-31 02:11 PM
Re: Adding an incremental line to a text file.
[Re: Arend_]
|
GeorgeLittle
Fresh Scripter
Registered: 2011-02-08
Posts: 47
Loc: UK
|
Example text file;
jane.doe,12345 joe.blogs,12346 jon.doe,12347
Thanks
|
Top
|
|
|
|
#203003 - 2011-08-31 05:06 PM
Re: Adding an incremental line to a text file.
[Re: Allen]
|
GeorgeLittle
Fresh Scripter
Registered: 2011-02-08
Posts: 47
Loc: UK
|
You would probably only have single users connecting without an ID allready in the file. So it should work on the basis that two new users are not likely to log on at the same time.
|
Top
|
|
|
|
#203005 - 2011-08-31 07:42 PM
Re: Adding an incremental line to a text file.
[Re: GeorgeLittle]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
Untested, give it a go.
;Set the string for the filename
$UIDINFO = "c:\Codes2b.csv"
;Check it exists
If Exist ($UIDINFO) = 0
MessageBox ("Error1:Consultant ID File Does Not Exist."+ Chr(13)+
"Please contact IT Support on 1234, as you will not be able to log in,"+Chr(13)+
"You will now be logged off this system!.",
"WARNING MESSAGE", 48, 300)
Else
;Check if the file is of a size greater than 0KB
$UIDSIZE = GetFileSize($UIDINFO)
If $UIDSIZE = 0
MessageBox ("Error2:Consultant ID File Is To Small."+ Chr(13)+
"Please contact IT Support on 1234, as you will not be able to log in,"+Chr(13)+
"You will now be logged off this system!.",
"WARNING MESSAGE", 48, 300)
Sleep 10
EndIf
;Open Up The UID File
Dim $objFSO, $objFile, $strLastLine, $strNewLine
$objFSO = CreateObject("Scripting.FileSystemObject")
$objFile = $objFSO.OpenTextFile($UIDINFO, 1, 1)
While Not $objFile.AtEndOfLine
If InStr($objFile.ReadLine,@USERID)
$uid = Split($objFile.ReadLine,",")[0]
EndIf
Loop
$strLastLine = $objFile.ReadLine
$objFile.Close
;Count the UID string and ensure its populated
$uid_length = Len($UID)
;If statement that creates a new UID and number
If $uid_length = "0"
$strNewLine = Split($strLastLine,",")[1]
$strNewLine = CInt($strNewLine)+1
$objFSO = CreateObject("Scripting.FileSystemObject")
$objFile = $objFSO.OpenTextFile($UIDINFO, 8, 1)
$objFile.WriteLine(@USERID+","+CStr($strNewLine))
$objFile.Close
Else
Set "CONS_CODE=$UID"
? "Found name with UID: " + $uid
EndIf
EndIf
|
Top
|
|
|
|
#203009 - 2011-09-01 11:49 AM
Re: Adding an incremental line to a text file.
[Re: Arend_]
|
GeorgeLittle
Fresh Scripter
Registered: 2011-02-08
Posts: 47
Loc: UK
|
It does not appear to add a incremental number. I am probably stupid but where is the lastline variable defined ?
|
Top
|
|
|
|
#203010 - 2011-09-01 12:02 PM
Re: Adding an incremental line to a text file.
[Re: GeorgeLittle]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
$strLastLine = $objFile.ReadLine
|
Top
|
|
|
|
#203012 - 2011-09-01 01:38 PM
Re: Adding an incremental line to a text file.
[Re: Arend_]
|
GeorgeLittle
Fresh Scripter
Registered: 2011-02-08
Posts: 47
Loc: UK
|
Thanks apronk it now works for the first entry but then the counter does not seem to go up. I have checked and the file ends with a CRLF
|
Top
|
|
|
|
#203013 - 2011-09-01 03:04 PM
Re: Adding an incremental line to a text file.
[Re: GeorgeLittle]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
ok, tested code this time:
;Set the string for the filename
$UIDINFO = "c:\Codes2b.csv"
;Check it exists
If Exist ($UIDINFO) = 0
MessageBox ("Error1:Consultant ID File Does Not Exist."+ Chr(13)+
"Please contact IT Support on 1234, as you will not be able to log in,"+Chr(13)+
"You will now be logged off this system!.",
"WARNING MESSAGE", 48, 300)
Else
;Check if the file is of a size greater than 0KB
$UIDSIZE = GetFileSize($UIDINFO)
If $UIDSIZE = 0
MessageBox ("Error2:Consultant ID File Is To Small."+ Chr(13)+
"Please contact IT Support on 1234, as you will not be able to log in,"+Chr(13)+
"You will now be logged off this system!.",
"WARNING MESSAGE", 48, 300)
Sleep 10
EndIf
;Open Up The UID File
Dim $objFSO, $objFile, $strLine, $strNewLine
$objFSO = CreateObject("Scripting.FileSystemObject")
$objFile = $objFSO.OpenTextFile($UIDINFO, 1, 1)
While Not $objFile.AtEndOfStream
$strLine = $objFile.ReadLine
$strUser = Split($strLine,",")[0]
If $strUser = @USERID
$uid = $strUser
EndIf
Loop
$objFile.Close
;Count the UID string and ensure its populated
$uid_length = Len($UID)
;If statement that creates a new UID and number
If $uid_length = "0"
$strNewLine = Split($strLine,",")[1]
$strNewLine = CInt($strNewLine)+1
$objFSO = CreateObject("Scripting.FileSystemObject")
$objFile = $objFSO.OpenTextFile($UIDINFO, 8, 1)
$objFile.WriteLine(@USERID+","+CStr($strNewLine))
$objFile.Close
Else
Set "CONS_CODE=$UID"
? "Found name with UID: " + $uid
EndIf
EndIf
|
Top
|
|
|
|
#203014 - 2011-09-01 03:42 PM
Re: Adding an incremental line to a text file.
[Re: Arend_]
|
GeorgeLittle
Fresh Scripter
Registered: 2011-02-08
Posts: 47
Loc: UK
|
Hmm now it's not working at all . What version of kix are you using ?
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 323 anonymous users online.
|
|
|