Author Topic: Deleting With File Objects  (Read 2506 times)

What would be the function to delete something with file objects?

fileDelete("path/to/file");
Or do you mean deleting something inside the file?
« Last Edit: December 19, 2011, 08:44:58 PM by Headcrab Zombie »

Deleting files uses an independent command. There's no direct relation to fileObjects.

You may also find isFile("path/to/file") useful in your file deleting adventures.

fileDelete("path/to/file");
Or do you mean deleting something inside the file?
Inside the file.

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: [Select]
%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();

Using another file
Code: [Select]
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");
Both untested

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.

Does not work.
Code: [Select]
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();
}

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.

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.
No it's not a .zip

You should just create the file in base/config/. If you're trying to censor people's chat you don't want to see, there's no way I can think of to do it client-sided.

If you're trying to censor people's chat you don't want to see, there's no way I can think of to do it client-sided.

Badspot includes clientside chat censoring by default. Or did I imagine that?

So the file is now in config/client/ChatBot/allowedNames.txt but that still does not fix my problem.

Badspot includes clientside chat censoring by default. Or did I imagine that?
I think you imagined that.