User Control Panel
Advertisements

HELP US, HELP YOU!

Subroutine: recursive directory scanning

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Commands
View unanswered posts
Author Message
Cer
Upgraded Agent
Upgraded Agent


Joined: 03 Feb 2004
Posts: 3776
Location: Michigan
Reputation: 146.9
votes: 4

PostPosted: Wed Mar 16, 2005 8:16 pm    Post subject: Reply with quote

This subroutine will (or should) be able to fit properly within any bot's code, since this is all 100% Perl code and is just a subroutine.

This subroutine will scan a folder and return the file paths of a given extension, and (if specified) will go on a recursive loop to also search subdirectories.

For example, if you have a directory tree like this:
Code:
commands/<br />     admins/<br />          alert.pl<br />          block.pl<br />     masters/<br />          hidden/<br />               cloak.pl<br />               perl.pl<br />          visible/<br />               addupdate.pl<br />     public/<br />          allusers/<br />               menu.pl<br />               stats.pl<br />          special/<br />               jeopardy.pl<br />     copyright.pl


This subroutine, if you tell it to scan "./commands", it will return an array like this:
Code:
./commands/admins/alert.pl<br />./commands/admins/block.pl<br />./commands/masters/hidden/cloak.pl<br />./commands/masters/hidden/perl.pl<br />./commands/masters/visible/addupdate.pl<br />./commands/public/allusers/menu.pl<br />./commands/public/allusers/stats.pl<br />./commands/public/special/jeopardy.pl<br />./commands/copyright.pl


So it scans the directory for a given file type (in this case, "pl") and also scan all the folders it finds inside of that folder, and returns a full path to your command's files.

And, unless your commands need proper names within the bot (i.e. "alert", "block", etc) you can make a command loading code in just a couple lines:
Code:
my @commands = &scanDir('./commands',1,'.pl');<br />foreach my $file (@commands) {<br />   require $file;<br />}

And then all your commands have been loaded. Smile

Anyway, this kind of code has all kinds of purposes. Now finally, here's the code:
Code:
sub scanDir {<br />     # Directory data.<br />     my ($dir,$recurse,@ext) = @_;<br /><br />     # $dir = directory to scan<br />     # $recurse = recursion (1 or 0)<br />     # @ext = list of file extensions.<br /><br />     # List of all files.<br />     my @list = ();<br /><br />     # Open the directory.<br />     opendir (DIR, "$dir");<br />     foreach my $file (sort(grep(!/^\./, readdir(DIR)))) {<br />          # Is this another directory?<br />          if (-d "$dir/$file") {<br />               # Recursing?<br />               if ($recurse) {<br />                    # Get this directory too.<br />                    my @subs = &scanDir("$dir/$file",1,@ext);<br />                    push (@list,@subs);<br />                    next;<br />               }<br />               else {<br />                    next;<br />               }<br />          }<br /><br />          my $good = 0;<br /><br />          # Check the file type.<br />          foreach my $type (@ext) {<br />               if ($file =~ /$type$/i) {<br />                    $good = 1;<br />               }<br />          }<br /><br />          if (!@ext) {<br />               $good = 1;<br />          }<br /><br />          # Good file?<br />          if ($good == 1) {<br />               push (@list,"$dir/$file");<br />          }<br />     }<br />     closedir (DIR);<br /><br />     # Return the list.<br />     return @list;<br />}


USAGE:
@files = &scanDir (DIRECTORY, RECURSION, @EXTENSIONS);
Quote:
DIRECTORY = The initial folder to scan.
RECURSION = 1 or 0 -- If 1, it will also scan subdirectories within the initial DIRECTORY and return a list of those files as well.
EXTENSIONS = An array of file extensions to search for (or omit and it will return ALL non-directory files).

_________________
Current Site (2008) http://www.cuvou.com/
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Commands All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group