Blockland Forums > Modification Help
Deleting With File Objects
<< < (2/5) > >>
Headcrab Zombie:
You'll need to read everything in the file, store it somewhere (a string only if you know the file will never go over 4096 bytes, otherwise you'll need to use another file), delete the line you're looking for, than rewrite it.

For example, something like this

Using a string

--- Code: ---%file = new FileObject();
%file.openForRead("base/myFile.txt");
while(!%file.isEoF())
    %wholeFile = %wholeFile NL %file.readLine();
%file.close();
strReplace(%wholeFile,"stuff to delete\n","");
%file.openForWrite("base/myFile.txt");
%file.writeLine(%wholeFile);
%file.close();
%file.delete();

--- End code ---

Using another file

--- Code: ---fileCopy("base/myFile.txt","base/myFileBuffer.txt");
%file = new FileObject();
%file.openForWrite("base/myFile.txt");
%fileBuffer = new FileObject();
%fileBuffer.openForRead("base/myFileBuffer.txt");
while(!%fileBuffer.isEoF())
{
%line = %fileBuffer.readLine();
if(%line !$= "stuff to delete")
%file.writeLine(%line);
}
%file.close();
%file.delete();
%fileBuffer.close();
%fileBuffer.delete();
fileDelete("base/myFileBuffer.txt");

--- End code ---
Both untested
Ipquarx:
Or you could base it on single lines (using arrays) and search through them for a certain word or text, and then set that line to a blank value, and then write the entire file, so it wont write the line you overwrote with  a blank value.

allthough its not a very efficiant way, it's the easiest, doesnt have a size limit (the real limit is either your computers power or how big you need the file to be), and is also more easily adaptable to many other things, like editing and adding lines.
jes00:
Does not work.

--- Code: ---function removePerson(%name)
{
%file = new FileObject();
%file.openForRead("Add-Ons/Client_Chatbot/AllowedNames.txt");
while(!%file.isEOF())
{
%wholeFile = %wholeFile NL %file.readLine();
}
%file.close();
strReplace(%wholeFile,%name @ "\n","");

%file.openForWrite("Add-Ons/Client_Chatbot/AllowedNames.txt");
%file.writeLine(%wholeFile);

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

--- End code ---
Uristqwerty:
Is Client_Chatbot a .zip? If so, then writing to that file will create a folder called Client_Chatbot, and write the modified file to it, leaving the original unchanged.
jes00:

--- Quote from: Uristqwerty on December 21, 2011, 03:23:20 PM ---Is Client_Chatbot a .zip? If so, then writing to that file will create a folder called Client_Chatbot, and write the modified file to it, leaving the original unchanged.

--- End quote ---
No it's not a .zip
Navigation
Message Index
Next page
Previous page

Go to full version