User Control Panel
Advertisements

HELP US, HELP YOU!

dirs

 
Post new topic   Reply to topic    Bot Depot Forum Index -> General Programming Questions
View unanswered posts
Author Message
draget
Not Yet a God
Not Yet a God


Joined: 29 Dec 2004
Posts: 367
Location: Australia
Reputation: 32.2Reputation: 32.2Reputation: 32.2

PostPosted: Tue May 17, 2005 10:32 am    Post subject: Reply with quote

I am writing the follow script which for a given folder will search all subfolders and eventually return a list of the files.

Code:
$i = 1;<br /><br /><br />my $program = &feel($i, "./");<br /><br />print $program;<br /><br />  open (FILE, ">>out.txt");<br /><br />print FILE $program;<br /><br />close(FILE);<br /><br />sub feel {<br />$i = $i+1;<br /><br />my $current = shift;<br />my $path = shift;<br /><br />my $list = "";<br /><br />opendir ($current, "$path");<br />   foreach my $file (sort(grep(!/^\./, readdir($current)))) {<br /><br />   <br />if(opendir($i, $file)){<br />   <br /><br /><br />   my $feelit = &feel($i, $file);<br /><br />   $list .= "$feelit";   <br /><br />}<br /><br />else {<br /><br />$list .= "$file\n";<br /><br /><br /><br />}<br /><br />   }<br />   closedir ($current);<br /><br /><br /><br /><br />return $list;<br /><br />}<br />


problem being, at present it only works back one level. also i think there may be an issue with using if(opendir($i, $file)){.

Any help woul be appreciated, thanks.


draget
Back to top
Cer
Upgraded Agent
Upgraded Agent


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

PostPosted: Tue May 17, 2005 11:34 am    Post subject: Reply with quote

I actually wrote a subroutine in my bot for doing this same thing (with recursion, too, so it can go any number of levels deep).

Usage:
my @files = &scanDir (DIRECTORY[, RECURSE[, @EXTENSIONS]]);

Directory = initial folder to scan
Recurse = 1 to scan subdirs, 0 to not
Extensions = list of extensions to find, or omit for find any file.

It returns an array of the file paths to each file (i.e.
Quote:
C:/AUTOEXEC.BAT
C:/Documents and Settings/Owner/Desktop/MSN.lnk
etc...


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 />}

_________________
Current Site (2008) http://www.cuvou.com/
Back to top
darkmonkey
The Merovingian
The Merovingian


Joined: 18 Apr 2004
Posts: 2557
Location: London, England
Reputation: 39.3Reputation: 39.3Reputation: 39.3Reputation: 39.3
votes: 7

PostPosted: Tue May 17, 2005 6:00 pm    Post subject: Reply with quote

Draget: Indent. No one is going to help you fix your code (Cer just gives his away...) if it's messy. Indent. Like Cer.
_________________
~ Josh
[ Need bot hosting on a dedicated server? PM me. ]
Back to top
draget
Not Yet a God
Not Yet a God


Joined: 29 Dec 2004
Posts: 367
Location: Australia
Reputation: 32.2Reputation: 32.2Reputation: 32.2

PostPosted: Wed May 18, 2005 10:29 am    Post subject: Reply with quote

thanks for that cer, i modified my original a bit and it worked from another guys advice just before you posted lol.

DM: good point ill do that in the futrure
Back to top
draget
Not Yet a God
Not Yet a God


Joined: 29 Dec 2004
Posts: 367
Location: Australia
Reputation: 32.2Reputation: 32.2Reputation: 32.2

PostPosted: Thu May 19, 2005 11:49 am    Post subject: Reply with quote

anyone know of a good prog that does auto indentation etc?
Back to top
dan0211
Not Yet a God
Not Yet a God


Joined: 23 Dec 2003
Posts: 395

Reputation: 37.8Reputation: 37.8Reputation: 37.8Reputation: 37.8

PostPosted: Fri May 20, 2005 7:28 pm    Post subject: Reply with quote

Oh come on, indenting is easy Razz

Just tab the line underneath a {

ie

if ($msg eq 'bleh') {
----Tab Here--- Some code here
----Tab Here---More Code
----Tab Here---Even More Code
}

^ No tab there.

Just get into the habit of doing that when you're coding.

Also, some editors will do the tabbing for you as you write the characters. (EG Crimson Editor)
Back to top
Siebe
God Like
God Like


Joined: 06 Jan 2004
Posts: 562
Location: Netherlands
Reputation: 39.8Reputation: 39.8Reputation: 39.8Reputation: 39.8

PostPosted: Sat May 21, 2005 12:57 am    Post subject: Reply with quote

UltraEdit 11.10. The leet. Razz
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> General Programming Questions 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