AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Read all files in a folder. (https://forums.alliedmods.net/showthread.php?t=225581)

Kia 09-06-2013 11:33

Read all files in a folder.
 
Hello everybody,

I'd like to read out the content of all files in a folder, the files aren't registered somewhere.
Basically I want something like this :

PHP Code:

for each file in folder
{
          for 
each line in file
          
{
                    
read out content
                    compare with a string
                    
return true or false if containi
          
}


I know how to do the "for each line in file" part, but I don't know how to get all files in a folder.

mottzi 09-06-2013 11:38

Re: Read all files in a folder.
 
http://www.amxmodx.org/doc/index.htm...2Fread_dir.htm

Kia 09-06-2013 12:30

Re: Read all files in a folder.
 
Thanks. :)

mottzi 09-06-2013 12:34

Re: Read all files in a folder.
 
Bether method: http://forums.alliedmods.net/showthread.php?t=135752

Kia 09-06-2013 12:46

Re: Read all files in a folder.
 
Thanks, I tried to use it, but I get this problem.

Quote:

Couldn't read file "file.txt"
PHP Code:

new szConfigDir[256], szRepoDir[256]
    
get_configsdir(szConfigDircharsmax(szConfigDir))
    
    
formatex(szRepoDircharsmax(szRepoDir), "%s/xRepo/repos/"szConfigDir)
    
    new 
szName[128], szURL[128]
    new 
iLineiLengthsBuffer[256]
    
    new 
szFileName[64]
    new 
hDir open_dir(szRepoDirszFileNamecharsmax(szFileName))
    
    
client_print(idprint_chat"%s"szRepoDir)
    
    if(!
hDir)
        return
    
    do
    {
        while(
read_file(szFileNameiLine++, sBuffercharsmax(sBuffer), iLength)) 
        {
            if((
sBuffer[0] == ';' || !iLength))
                continue
            
            
strtok(sBufferszNamecharsmax(szName), szURLcharsmax(szURL), '|'0)
            
            if(
containi(szNameszPlugin))
                
client_print(idprint_console"%s"szName)
        }
    }
    while ( 
next_filehDirszFileNamecharsmaxszFileName ) ) )
    
close_dir(hDir


Clauu 09-06-2013 15:04

Re: Read all files in a folder.
 
Try some debuging, replace do-while with only while and then skip . and .. from being stored. And also after you have finished from reading one file reset the iLine contor but maybe read_file can do this at every new file you should check.

fysiks 09-06-2013 22:49

Re: Read all files in a folder.
 
Quote:

Originally Posted by Kia (Post 2029505)
Thanks, I tried to use it, but I get this problem.



PHP Code:

new szConfigDir[256], szRepoDir[256]
    
get_configsdir(szConfigDircharsmax(szConfigDir))
    
    
formatex(szRepoDircharsmax(szRepoDir), "%s/xRepo/repos/"szConfigDir)
    
    new 
szName[128], szURL[128]
    new 
iLineiLengthsBuffer[256]
    
    new 
szFileName[64]
    new 
hDir open_dir(szRepoDirszFileNamecharsmax(szFileName))
    
    
client_print(idprint_chat"%s"szRepoDir)
    
    if(!
hDir)
        return
    
    do
    {
        while(
read_file(szFileNameiLine++, sBuffercharsmax(sBuffer), iLength)) 
        {
            if((
sBuffer[0] == ';' || !iLength))
                continue
            
            
strtok(sBufferszNamecharsmax(szName), szURLcharsmax(szURL), '|'0)
            
            if(
containi(szNameszPlugin))
                
client_print(idprint_console"%s"szName)
        }
    }
    while ( 
next_filehDirszFileNamecharsmaxszFileName ) ) )
    
close_dir(hDir


Where do you find this output that you speak of? Does this file actually exist? Also, don't use read_file(). Use fopen(), fgets(), fclose(), etc.

Kia 09-07-2013 01:48

Re: Read all files in a folder.
 
The error is shown in the console while debugging, and the file exists already.

fysiks 09-07-2013 02:49

Re: Read all files in a folder.
 
Quote:

Originally Posted by Kia (Post 2029795)
The error is shown in the console while debugging, and the file exists already.

Then I guess you need to start debugging it yourself. First change to the correct file functions. Then, you should use the full filepath as the filename when opening the file.

ConnorMcLeod 09-07-2013 02:54

Re: Read all files in a folder.
 
Paths are relative to current folder, you have to format the full one.


All times are GMT -4. The time now is 18:45.

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