Author Topic: Read All Files In Folder  (Read 2063 times)

So I was adding a little something to my bot where you could have it save notes.
Code: [Select]
function cbnewnote(%notetitle,%s1,%s2,%s3,%s4,%s5,%s6,%s7,%s8,%s9,%s10,%s11,%s12,%s13,%s14,%s15,%s16,%s17,%s18,%s29)
{
%file = new fileobject();
%file.openforwrite("config/client/ConsoleBot/notes/"@%notetitle@".txt");
%file.writeline(%s1 SPC %s2 SPC %s3 SPC %s4 SPC %s5 SPC %s6 SPC %s7 SPC %s8 SPC %s9 SPC %s10 SPC %s11 SPC %s12 SPC %s13 SPC %s14 SPC %s15 SPC %s16 SPC %s17 SPC %s18 SPC %s19);
%file.close();
%file.delete();
echo($cbmn@"The note "@%notetitle@" saved successfully.");
}
function cbreadnote(%filename)
{
%file = new fileobject();
%file.openforread("config/client/ConsoleBot/notes/"@%filename@".txt");
%readnote = %file.readline();
echo($cbnm SPC %readnote);
%file.close();
%file.delete();
}
The reason it is spaced out like
         this
is because I just took it from a part of the whole script.
So anyways, I need it to open up ConsoleBot/notes and read all the names of the files inside notes.
Could anyone help?
« Last Edit: May 18, 2013, 08:36:24 PM by Radíowave »

Code: [Select]
%file = findFirstFile("config/client/ConsoleBot/notes/*.txt");
while(%file !$= "")
{
    echo(%file);
    %file = findNextFile("config/client/ConsoleBot/notes/*.txt");
}

You would do this:

Code: [Select]
%path = "config/client/ConsoleBot/notes/*.txt";
%count = getFileCount(%path);
for(%i = 0; %i < %count; %i ++)
{
     %file = findNextFile(%path);
     echo(%file);
}

EDIT: Xalos posted while I was typing.. Both methods work.

EDIT: Xalos posted while I was typing.. Both methods work.

Huh, I didn't know getFileCount() was a thing.
Something learned!

You would do this:

Code: [Select]
%path = "config/client/ConsoleBot/notes/*.txt";
%count = getFileCount(%path);
for(%i = 0; %i < %count; %i ++)
{
     %file = findNextFile(%path);
     echo(%file);
}

EDIT: Xalos posted while I was typing.. Both methods work.
Huh, this was ALMOST exactly what I wanted.
It would be better if it would just say the name by itself, but it says config/client/ConsoleBot/thenamehere.txt
I mean, I could live with it, but it would be better to only show the file name.
« Last Edit: May 19, 2013, 01:21:49 AM by Radíowave »

Huh, this was ALMOST exactly what I wanted.
It would be better if it would just say the name by itself, but it says config/client/ConsoleBot/thenamehere.txt
I mean, I could live with it, but it would be better to only show the file name.

Change the echo to echo(fileName(%file));

Another method:

Code: [Select]
%mask = "config/client/ConsoleBot/notes/*.txt";

for ( %file = findFirstFile( %mask ) ; %file !$= "" ; %file = findNextFile( %mask ) )
{
     echo( %file );
}