Author Topic: File manipulation!(bottom post please read)  (Read 1047 times)

I need some fuctions on how to manipulate some files.

Find a string, replace it.
Find a string, get the contents.
Removing a string or w/e, and how to remove a line along with that string?

%test = Find "423" in config/server/werwerasfgsfg.txt

If 423 = true... message...
false...



What is the proper way to create a file if it doesn't exist?
« Last Edit: December 27, 2008, 08:29:21 PM by Kalphiter »

You mean like a windows file?
I dont think it is possible with Torque script.


Searching for a string:
What this does is it searches a file at %path for an instanced of %string using strPos. More specifically, it checks each line with strPos, and if it finds the %string it returns 1 (true). If it gets to the end of the file (isEOF()), it will return 0 (false). You could even edit this to return the %path of the file if it finds it, if you're making a search-esque function. In which case, you should also edit the last return to -1 to check for no file found.

Code: [Select]
function searchFileForStr(%path,%string) {
     %file=new FileObject();
     %file.openForRead(%path);
     while(!%file.isEOF()) {
          if(strPos(%file.readLine(),%string)!=-1) {
               %file.close();
               %file.delete();
               return 1;
          }
     }
     %file.close();
     %file.delete();
     return 0;
}

Creating a file if it doesn't exist:
By putting this in your file code, you will be using isFile() to check if a file at the path exists. If not, it creates a blank file at the path with no contents. After closing the file it moves on to the next file events you may have.

Code: [Select]
%path="config/server/exampleFile.txt";
%file=new FileObject();
if(!isFile(%path)) {
     %file.openForWrite(%path);
     %file.writeLine(" ");
     %file.close();
}
//> Other file events go here
%file.delete();

Warning - while you were typing a new reply has been posted. You may wish to review your post.
Hunter :3

EDIT:
For replacing a bit of text in a file, look at the script I used for the Saving Variables add-on.
« Last Edit: December 27, 2008, 08:18:16 PM by Truce »

Removing a string/removing a line with that...

Removing a string/removing a line with that...

What I was trying to say is in the Saving Variables add-on, I had read the lines of a file into seperate variables. Then, to save the file, I wrote all those variables into a file. That way I would be able to edit parts of the file in the middle. If you adapt that method, you can make a line/string removal system.

inb4 truce's third post.