Raised This Month: $12 Target: $400
 3% 

Looping through files inside a directory


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Marcus_Brown001
AlliedModders Donor
Join Date: Nov 2012
Location: Illinois, United States
Old 01-26-2014 , 22:14   Looping through files inside a directory
Reply With Quote #1

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!
Marcus_Brown001 is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 01-26-2014 , 23:11   Re: Looping through files inside a directory
Reply With Quote #2

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.

Last edited by Mitchell; 01-26-2014 at 23:12.
Mitchell is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 01-27-2014 , 01:50   Re: Looping through files inside a directory
Reply With Quote #3

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.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 01-27-2014 , 02:04   Re: Looping through files inside a directory
Reply With Quote #4

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.
Mathias. is offline
Marcus_Brown001
AlliedModders Donor
Join Date: Nov 2012
Location: Illinois, United States
Old 01-27-2014 , 09:18   Re: Looping through files inside a directory
Reply With Quote #5

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!

Last edited by Marcus_Brown001; 01-27-2014 at 10:15.
Marcus_Brown001 is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 01-27-2014 , 09:26   Re: Looping through files inside a directory
Reply With Quote #6

Yeah my bad when i was using this I forgot i had a sum over 5,000 and more files.
Mitchell is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:52.


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