AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Looping through files inside a directory (https://forums.alliedmods.net/showthread.php?t=234199)

Marcus_Brown001 01-26-2014 22:14

Looping through files inside a directory
 
I haven't been able to find anything to accomplish what I am trying to do. I don't know if it is possible, either. The goal is to loop through every file inside a directory, and grab the names of all the files inside it.

Any help and/or advice is appreciated!

Mitchell 01-26-2014 23:11

Re: Looping through files inside a directory
 
As bubka3 recommended: https://forums.alliedmods.net/showthread.php?p=602270
Code:


public ReadFileFolder(String:path[]){
        new Handle:dirh = INVALID_HANDLE;
        new String:buffer[256];
        new String:tmp_path[256];
        new FileType:type = FileType_Unknown;
        new len;
       
        len = strlen(path);
        if (path[len-1] == '\n')
                path[--len] = '\0';

        TrimString(path);
       
        if(DirExists(path)){
                dirh = OpenDirectory(path);
                while(ReadDirEntry(dirh,buffer,sizeof(buffer),type)){
                        len = strlen(buffer);
                        if (buffer[len-1] == '\n')
                                buffer[--len] = '\0';

                        TrimString(buffer);

                        if (!StrEqual(buffer,"",false) && !StrEqual(buffer,".",false) && !StrEqual(buffer,"..",false)){
                                strcopy(tmp_path,255,path);
                                StrCat(tmp_path,255,"/");
                                StrCat(tmp_path,255,buffer);
                                if(type == FileType_File){
                                        if(downloadtype == 1){
                                                ReadItem(tmp_path);
                                        }
                                        else{
                                                ReadItemSimple(tmp_path);
                                        }
                                }
                                else{
                                        ReadFileFolder(tmp_path);
                                }
                        }
                }
        }
        else{
                if(downloadtype == 1){
                        ReadItem(path);
                }
                else{
                        ReadItemSimple(path);
                }
        }
        if(dirh != INVALID_HANDLE){
                CloseHandle(dirh);
        }
}

Although looping through Directories could make the server lag at times. Cache the results of each file names in the directory.

friagram 01-27-2014 01:50

Re: Looping through files inside a directory
 
It should be okay, if you are not doing tons of file operations. I'd not recommend looking through the sprays / download directory.

Anyhow, you should never need to search through a directory. It would be better to use keyvalues and store relevant info there.. Or of need be, sql. Or at the very least, use these as indices/caches for your files so you don't have to search directories.

Mathias. 01-27-2014 02:04

Re: Looping through files inside a directory
 
Like friagram said, finding file isnt a big deal but if you going to read/write to every single files and that you have a tons of them, this could be a issue.

Marcus_Brown001 01-27-2014 09:18

Re: Looping through files inside a directory
 
Yeah, I thought about doing KeyValues for it. I'll try this method first. I will end up limiting how many files a client can have, so it won't get too outrageous to loop through them.

Thanks for the help. I should have thought to look at SM Downloader :|

** Edit **

Problem is resolved. The snippet from SM Downloader go the job done. Thanks!

Mitchell 01-27-2014 09:26

Re: Looping through files inside a directory
 
Yeah my bad when i was using this I forgot i had a sum over 5,000 and more files.


All times are GMT -4. The time now is 06:24.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.