Page 1 of 1 1
Topic Options
#202998 - 2011-08-31 09:38 AM Adding an incremental line to a text file.
GeorgeLittle Offline
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.

 Code:
;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
#202999 - 2011-08-31 11:21 AM Re: Adding an incremental line to a text file. [Re: GeorgeLittle]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
This shouldn't be so difficult. Can you post an example of the file that holds the IDīs?
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#203000 - 2011-08-31 11:31 AM Re: Adding an incremental line to a text file. [Re: Mart]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
You were missing an "EndIf" as well.
 Code:
;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 Offline
Fresh Scripter

Registered: 2011-02-08
Posts: 47
Loc: UK
Example text file;

jane.doe,12345
joe.blogs,12346
jon.doe,12347

Thanks \:D

Top
#203002 - 2011-08-31 04:46 PM Re: Adding an incremental line to a text file. [Re: GeorgeLittle]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4557
Loc: USA
How often does this happen? How many people are in this file now? The reason I ask is, you could have situation where multiple people are accessing the file at the same time, and while this would work fine when READING the file, only one person can WRITE to it at a time.
Top
#203003 - 2011-08-31 05:06 PM Re: Adding an incremental line to a text file. [Re: Allen]
GeorgeLittle Offline
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_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Untested, give it a go.
 Code:
;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 Offline
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_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
 Code:
$strLastLine = $objFile.ReadLine

Top
#203011 - 2011-09-01 12:04 PM Re: Adding an incremental line to a text file. [Re: Arend_]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
This could happen, if the last line in the CSV is not blank.
FSO likes the last line to be blank.

Top
#203012 - 2011-09-01 01:38 PM Re: Adding an incremental line to a text file. [Re: Arend_]
GeorgeLittle Offline
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_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
ok, tested code this time:
 Code:
;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 Offline
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
#203016 - 2011-09-01 03:52 PM Re: Adding an incremental line to a text file. [Re: GeorgeLittle]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Works perfectly here....
Version 4.61, make sure you copy the entire code i just posted.
Not just the changes you may see, cos I changed a lot.

Top
Page 1 of 1 1


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

Who's Online
0 registered and 323 anonymous users online.
Newest Members
Audio, Hoschi, Comet, rrosell, PatrickPinto
17880 Registered Users

Generated in 0.064 seconds in which 0.026 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