| View previous topic :: View next topic |
| Author |
Message |
thoerner Guest
|
Posted: Fri Mar 27, 2009 6:16 pm Post subject: Add item to .txt file if it does not exist in the file |
|
|
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
|
Posted: Fri Mar 27, 2009 7:00 pm Post subject: |
|
|
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 |
|
 |
dmatch
Joined: 15 Oct 2007 Posts: 252
|
Posted: Fri Mar 27, 2009 7:22 pm Post subject: |
|
|
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 |
|
 |
aCkRiTe
Joined: 21 Jul 2006 Posts: 555
|
Posted: Fri Mar 27, 2009 7:33 pm Post subject: |
|
|
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 |
|
 |
Guest
|
Posted: Fri Mar 27, 2009 10:46 pm Post subject: |
|
|
I personally would use python for something like this  |
|
| Back to top |
|
 |
|