How to count the number of files in a directory?
#1

I want to count how many registered player the server currently has.
Now my user accounts are on (/scriptfiles/UserAccounts/)

And I want to count how many files exist in that folder, so if I have 4 accounts, the message should return 4

Example:
pawn Code:
public OnPlayerConnect(playerid)
{
    new filecount;
    filecount = // dunno what to do here
    SendClientMessage(playerid, 0xFFFFFF, "Welcome to the server. Database has %i registered accounts",filecount);
    return 1;
}
I don't know what I just did there^ but you got the point.
Oh and by the way I use y_ini to load and save my userfiles if that helps
Reply
#2

pawn Code:
filecount = fexist("/UserAccounts/*");
I think that should work.
Reply
#3

pawn Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
if(dialogid == R_DIALOG)
{
     if(!response) return Kick(playerid);
     if(strlen(inputtext) < 6 || strlen(inputtext) > 100)
     {
        SendClientMessage(playerid, 0xFF0000AA,"Character Length is 5-15");
        ShowPlayerDialog(playerid, R_DIALOG, DIALOG_STYLE_PASSWORD,"Registration","Your account isn't registered\n Please enter your desired password below","Register","Quit");
     }
     else if(strlen(inputtext) > 0 && strlen(inputtext) < 100)
     {
        new INI:file = INI_Open(UserAccount(playerid));
        INI_SetTag(file,"data");
        INI_WriteInt(file, "Password", udb_hash(inputtext));
        INI_WriteInt(file,"Scores", 0);
        INI_WriteInt(file,"Money", 0);
        INI_WriteInt(file,"AdminLevel", 0);
        INI_WriteInt(file,"Kills", 0);
        INI_WriteInt(file,"Deaths", 0);
        INI_WriteInt(file,"Helper", 0);
        INI_Close(file);
        PlayerInfo[playerid][pLogged] = 1;
        new filecount;
        new string3[256];
        new string[100];
  filecount = fexist("/UserAccounts/*");
        format(string3,sizeof(string3),"Database has {FF9900}%i{FFFFFF} registered accounts",filecount);
        SendClientMessage(playerid, 0xFFFFFF, string3);
        format(string, sizeof(string),"Your password is: {00FF00}%s", inputtext);
        SendClientMessage(playerid, 0xFFFFFF, string);
     }
   }
Once in the server, it says there are 0 registered accounts when in reality there are already 4 accounts in the /scriptfiles/UserAccounts folder
Reply
#4

If you just have started/enabled registrations, you could add a variable TotalRegisteredPlayers++; under every successful registration, save it, and whenever required just format it and send it!
Am unexperienced with ini, hence I suggested this way. Cheers!
Reply
#5

Quote:
Originally Posted by Rajat_Pawar
View Post
If you just have started/enabled registrations, you could add a variable TotalRegisteredPlayers++; under every successful registration, save it, and whenever required just format it and send it!
Can't I just count how many files is there in the directory and return the result in SendClientMessage instead?
Reply
#6

have a look at the FileManager plugin, theres a little script in the topic describing how to list a directory - how to make it count up, is up to you ^^
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)