Author Topic: How do I add an item, delete an item, and get an item from a text file?  (Read 829 times)

How? I've seen mutemod's list manager but it doesn't save to a text file.

you must be talking about FileObjects.
It allows you to write, read, and create/delete files.

to add a item, heres the syntax, but there are 2 different modes for writing; Write and Append.
Write clears the file and then writes lines.
Append opens the file and writes to the end of the file.
Code: [Select]
%blah = new FileObject();
%blah.openForWrite("filepath");
%blah.writeLine("enter text here");
%blah.close();
%blah.delete();
Thats to overwrite.
And to append:
Code: [Select]
%blah = new FileObject();
%blah.openForAppend("filepath");
%blah.writeLine("enter text here");
%blah.close();
%blah.delete();
To read, its the same thing, just change a couple things:
Code: [Select]
%blah = new FileObject();
%blah.openForRead("filepath");
%blah.readLine();
%blah.close();
%blah.delete();

you must be talking about FileObjects.
It allows you to write, read, and create/delete files.

-code-
Thanks, I'll edit when it doesn't work.

Wait, what if there is no text file and I need the add-on to create it? In that case, I also need a script to check if the file exists.
« Last Edit: December 19, 2011, 06:44:54 PM by Axolotl »

Wait, what if there is no text file and I need the add-on to create it? In that case, I also need to check if the file exists.
When using the FileObject functions, the openForWrite will create the file, so will the openToAppend (I think).
To check if there is a file, do
if(isFile(%path))
{
//yes
}
else
{
//no
}

Enjoi.

When using the FileObject functions, the openForWrite will create the file, so will the openToAppend (I think).
To check if there is a file, do
if(isFile(%path))
{
//yes
}
else
{
//no
}

Enjoi.
Thanks

To read, its the same thing, just change a couple things:
Code: [Select]
%blah = new FileObject();
%blah.openForRead("filepath");
%blah.readLine();
%blah.close();
%blah.delete();

Oops, I meant to read a specific item. What I am saying is to see if a certain item is in the text file.

Oops, I meant to read a specific item. What I am saying is to see if a certain item is in the text file.
Code: [Select]
function FUNCTION_NAME(%string)
{
%file = new FileObject();
%file.openForRead("File/path/here");
while(!%file.isEOF())
{
%line = %file.readLine();
if(%line $= %string)
{
%file.close();
%file.delete();

return 1;
}
}
%file.close();
%file.delete();

return 0;
}
Now you can just call FUNCTION_NAME(%item);

Code: [Select]
function FUNCTION_NAME(%string)
{
%file = new FileObject();
%file.openForRead("File/path/here");
while(!%file.isEOF())
{
%line = %file.readLine();
if(%line $= %string)
{
%file.close();
%file.delete();

return 1;
}
}
%file.close();
%file.delete();

return 0;
}
Now you can just call FUNCTION_NAME(%item);
thanks jes

Code: [Select]
function FUNCTION_NAME(%string)
{
%file = new FileObject();
%file.openForRead("File/path/here");
while(!%file.isEOF())
{
%line = %file.readLine();
if(%line $= %string)
{
%file.close();
%file.delete();

return 1;
}
}
%file.close();
%file.delete();

return 0;
}
wait, to look specifically in a line for that item (example: I am searching for 1 but I don't want 12, I specifically want 1) do I just replace if(%line $= %string) with if(%line == %string)?

EDIT: nvm
« Last Edit: December 19, 2011, 08:04:13 PM by Axolotl »

You locked this and then unlocked this : /