User Control Panel
Advertisements
HELP US, HELP YOU!
Author
Message
draget Not Yet a God Joined: 29 Dec 2004Posts: 367 Location: Australia
Posted: Tue May 17, 2005 10:32 am Post subject:
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 Joined: 03 Feb 2004Posts: 3776 Location: Michigan votes : 4
Posted: Tue May 17, 2005 11:34 am Post subject:
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 Joined: 18 Apr 2004Posts: 2557 Location: London, England votes : 7
Posted: Tue May 17, 2005 6:00 pm Post subject:
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 Joined: 29 Dec 2004Posts: 367 Location: Australia
Posted: Wed May 18, 2005 10:29 am Post subject:
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 Joined: 29 Dec 2004Posts: 367 Location: Australia
Posted: Thu May 19, 2005 11:49 am Post subject:
anyone know of a good prog that does auto indentation etc?
Back to top
dan0211 Not Yet a God Joined: 23 Dec 2003Posts: 395
Posted: Fri May 20, 2005 7:28 pm Post subject:
Oh come on, indenting is easy 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 Joined: 06 Jan 2004Posts: 562 Location: Netherlands
Posted: Sat May 21, 2005 12:57 am Post subject:
UltraEdit 11.10. The leet.
Back to top