AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Add item to .txt file if it does not exist in the file

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
thoerner
Guest





PostPosted: Fri Mar 27, 2009 6:16 pm    Post subject: Add item to .txt file if it does not exist in the file Reply with quote

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!
Back to top
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Fri Mar 27, 2009 7:00 pm    Post subject: Reply with quote

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 Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
dmatch



Joined: 15 Oct 2007
Posts: 252

PostPosted: Fri Mar 27, 2009 7:22 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
aCkRiTe



Joined: 21 Jul 2006
Posts: 555

PostPosted: Fri Mar 27, 2009 7:33 pm    Post subject: Reply with quote

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...
Back to top
View user's profile Send private message
Guest






PostPosted: Fri Mar 27, 2009 10:46 pm    Post subject: Reply with quote

I personally would use python for something like this Cool
Back to top
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group