AutoHotkey Community

It is currently May 26th, 2012, 4:35 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: March 27th, 2009, 7:16 pm 
I need to add an item to a text file if it does not already exist in the text file.

For example:

I have the file userlist.txt on a server.
When someone loads the AHK program it checks to see if their computer name is on userlist.txt. If it is not on userlist.txt, the AHK program adds it to the list.

I can't figure out how to do this. Thanks!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 27th, 2009, 8:00 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Look at:
- Fileread (to read userlist.txt in var)
- IfNotInString (to check if user isn't already in userlist.txt)
- Fileappend (to add new user to userlist.txt)
Be careful of partial matches, e.g. John will also match BigJohn, if
that happens may want to look at a parsing loop to check
each user in userlist.txt explicitly.

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 27th, 2009, 8:22 pm 
Offline

Joined: October 15th, 2007, 7:23 pm
Posts: 252
OR

An alternative would be to read the file line by line (1 user name per line) with a Loop, Read:
Code:
;Get the user name into a variable
;For Example UserName
;Read thru UserList.txt
NewUser:=1
Loop, Read, UserList.txt
{
   if(UserName = A_LoopReadLine){
      ;Have the UserName already
      NewUser:=0
      break
   }
}
if(NewUser){
   ;Don't have the UserName so add it
   FileAppend, %UserName%`n, UserList.txt
}
This will look for a non case sensitive match. To use HugoV's example, if user is BigJohn then John will not match it.

Hope this helps.

dmatch


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 27th, 2009, 8:33 pm 
Offline

Joined: July 21st, 2006, 6:13 am
Posts: 558
another example that might help you

Code:
IfNotExist, UserList.txt
   FileAppend, Computer1`,Computer2`,Computer3`,, UserList.txt
FileRead, List, UserList.txt
If A_ComputerName not in %List%
   FileAppend, %A_ComputerName%`,, UserList.txt
ExitApp

_________________

HTH...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 27th, 2009, 11:46 pm 
I personally would use python for something like this 8)


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, notsoobvious, SKAN, tterB, Yahoo [Bot] and 13 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group