Directory search

S

STEPHEN BECKER I V

I am writing a program to go in all the folders, then in to there folders....

def dirsearch (coming_in) #declare coming_in is the directory to be searched
print "\n\n\n",coming_in," line 2 \n\n\n" #testing
Dir.chdir(coming_in) #change to the directory
mainloc = Dir.getwd #new base
file = Array.new

print "\n\n\n",mainloc," line 7 \n\n\n" #testing

loc = Dir.open(mainloc) #open the folder
loc.each {|x| file.push(x) } #get the folders and files

dir = Array.new
count = file.size #amounts

if count==0 then break end#check if there are any files
print "-----------list------------",count,"--"
while count>0
if(!(file[count-1].include? ?.)) #for windows assume that file has an
extension and no folders have a .
reform = mainloc +'/'+ file[count-1] # create list of folders
print reform
dir.push(reform)
end
count-=1
end
print "\n"
if !(dir.size==0) then #check if any folders to go in to
many = dir.size-1;
while many>0
print dir[many],"loop at 28th\n"
if(!(dir[many].nil?))then #having a problem with nil folders? any help?
dirsearch(dir[many])#call function with new directory
end
many-=1
end
end
end

dirsearch("c:") # light the candle


i am using SciTE in windows XP. It just seems to be cutting out after
about 5 runs of the function. Am i writing over the same variables?
Becker
 
A

Ara.T.Howard

I am writing a program to go in all the folders, then in to there folders....

def dirsearch (coming_in) #declare coming_in is the directory to be searched
print "\n\n\n",coming_in," line 2 \n\n\n" #testing
Dir.chdir(coming_in) #change to the directory
mainloc = Dir.getwd #new base
file = Array.new

print "\n\n\n",mainloc," line 7 \n\n\n" #testing

loc = Dir.open(mainloc) #open the folder
loc.each {|x| file.push(x) } #get the folders and files

dir = Array.new
count = file.size #amounts

if count==0 then break end#check if there are any files
print "-----------list------------",count,"--"
while count>0
if(!(file[count-1].include? ?.)) #for windows assume that file has an
extension and no folders have a .
reform = mainloc +'/'+ file[count-1] # create list of folders
print reform
dir.push(reform)
end
count-=1
end
print "\n"
if !(dir.size==0) then #check if any folders to go in to
many = dir.size-1;
while many>0
print dir[many],"loop at 28th\n"
if(!(dir[many].nil?))then #having a problem with nil folders? any help?
dirsearch(dir[many])#call function with new directory
end
many-=1
end
end
end

dirsearch("c:") # light the candle


i am using SciTE in windows XP. It just seems to be cutting out after
about 5 runs of the function. Am i writing over the same variables?
Becker


this might be of help

harp:~/eg/ruby > ruby -e 'p Dir["**/*"]'
["vsdb", "vsdb/vsdb-0.0.0", "vsdb/vsdb-0.0.0/ruby_vsdb.c", "vsdb/vsdb-0.0.0/mkmf.log", "vsdb/vsdb-0.0.0/extconf.rb", "vsdb/vsdb-0.0.0/Makefile", "vsdb/vsdb-0.0.0/ruby_vsdb.o", "vsdb/vsdb-0.0.0/vsdb.so", "vsdb/vsdb-0.0.0/vsdb", "vsdb/vsdb-0.0.0/vsdb/dbd.13", "vsdb/vsdb-0.0.0/vsdb/dbd.13/db.13", "vsdb/vsdb-0.0.0/vsdb/db.cache", "vsdb/vsdb-0.0.0/test.rb", "vsdb/vsdb-0.0.0/core.9780", "vsdb/vsdb-0.0.0/core.9781", "vsdb/vsdb-0.0.0/core.9791", "vsdb/vsdb-0.0.0/core.9823", "vsdb/vsdb-0.0.0/jib", "vsdb/vsdb-0.0.0/vsdb_test.rb","templates", "templates/main1.rb", "templates/inc.rb", "templates/class.rb", "templates/test.rb", "templates/main0.rb", "templates/out", "templates/foo", "templates/stp", "templates/stp/inc.rb", "templates/stp/class.rb", "templates/foo-0.0.0", "templates/foo-0.0.0/lib", "templates/foo-0.0.0/lib/foo-0.0.0", "templates/foo-0.0.0/lib/foo-0.0.0/bar.rb", "templates/foo-0.0.0/lib/foo-0.0.0.rb", "templates/lib", "templates/lib/foo-0.0.0", "templates/lib/foo-0.0.0/bar.rb", "templates/lib/foo-0.0.0.rb", "templates/lib/foo.rb", "templates/main2.rb", "templates/install.rb", "templates/foobar", "templates/newclass", "templates/newinc", "templates/newmain", "templates/main4.rb","templates/main3.rb", "templates/main5.rb", "templates/doc", "templates/doc/created.rid", "templates/doc/rdoc-style.css", "templates/doc/files", "templates/doc/files/logging_rb.html", "templates/doc/classes", "templates/doc/classes/c.src", "templates/doc/classes/c.src/M000001.html", "templates/doc/classes/c.src/M000002.html", "templates/doc/classes/LogMethods.src", "templates/doc/classes/LogMethods.src/M000003.html", "templates/doc/classes/LogMethods.src/M000004.html", "templates/doc/classes/LogMethods.src/M000005.html", "templates/doc/classes/LogMethods.src/M000006.html", "templates/doc/classes/LogMethods.src/M000007.html", "templates/doc/classes/LogMethods.src/M000008.html", "templates/doc/classes/LogMethods.src/M000009.html", "templates/doc/classes/LogMethods.src/M000010.html", "templates/doc/classes/LogMethods.src/M000011.html", "templates/doc/classes/LogMethods.src/M000012.html", "templates/doc/classes/LogMethods.src/M000013.html", "templates/doc/classes/Logging.src", "templates/doc/classes/Logging.src/M000014.html", "templates/doc/classes/A.html", "templates/doc/classes/c.html", "templates/doc/classes/LoggerExt.html", "templates/doc/classes/LogMethods.html", "templates/doc/classes/Logging.html", "templates/doc/fr_file_index.html", "templates/doc/fr_class_index.html", "templates/doc/fr_method_index.html", "templates/doc/index.html", "templates/stdout", "templates/log", "templates/logging.rb", "templates/util.rb", "templates/quote.rb", "templates/log.20040613", "templates/log.0", "templates/log.1", "templates/=main5.rb.conf", "templates/main5.rb.conf", "rq-all", "rq-all/packages", "rq-all/packages/sqlite-ruby-1.3.1.tar.gz", "rq-all/packages/posixlock-0.0.1.tgz", "rq-all/packages/arrayfields-3.4.0.tgz", "rq-all/packages/lockfile-1.1.0.tgz", "rq-all/packages/sqlite-2.8.15.tar.gz", "rq-all/packages/ruby-1.8.1.tar.gz", "rq-all/packages/rq-0.1.5.tgz", "rq-all/install.sh", "rq-all/README", "rq-all.tgz"]


check out the docs for Dir::[]

cheers.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
P

Paul Duncan

--AJ3oM32U01nchLSz
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

* STEPHEN BECKER I V ([email protected]) said:
I am writing a program to go in all the folders, then in to there
folders....=20

Hi Stephen,

It looks like all you do is print out the directory name. This might be
a bit closer to what you're looking for:

def recurse(path, max_depth =3D 5)
Dir::entries(path).select do |file|
file !~ /^\./ && test(?d, File::join(path, file))=20
end.each do |dir|=20
puts File::join(path, dir)
recurse(File::join(path, dir), max_depth - 1) if max_depth > 0
end
end

Hope this helps...
def dirsearch (coming_in) #declare coming_in is the directory to be searc= hed
print "\n\n\n",coming_in," line 2 \n\n\n" #testing
Dir.chdir(coming_in) #change to the directory
mainloc =3D Dir.getwd #new base
file =3D Array.new=20
=20
print "\n\n\n",mainloc," line 7 \n\n\n" #testing
=20
loc =3D Dir.open(mainloc) #open the folder
loc.each {|x| file.push(x) } #get the folders and files
=20
dir =3D Array.new=20
count =3D file.size #amounts
=20
if count=3D=3D0 then break end#check if there are any files
print "-----------list------------",count,"--"
while count>0
if(!(file[count-1].include? ?.)) #for windows assume that file has an
extension and no folders have a .
reform =3D mainloc +'/'+ file[count-1] # create list of folders
print reform
dir.push(reform)
end
count-=3D1
end
print "\n"
if !(dir.size=3D=3D0) then #check if any folders to go in to
many =3D dir.size-1;
while many>0=20
print dir[many],"loop at 28th\n"
if(!(dir[many].nil?))then #having a problem with nil folders? any help?
dirsearch(dir[many])#call function with new directory
end
many-=3D1
end
end
end
=20
dirsearch("c:") # light the candle
=20
=20
i am using SciTE in windows XP. It just seems to be cutting out after
about 5 runs of the function. Am i writing over the same variables?
Becker
=20

--=20
Paul Duncan <[email protected]> pabs in #ruby-lang (OPN IRC)
http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562

--AJ3oM32U01nchLSz
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBVJh3zdlT34LClWIRAppfAJ99TEp4afxH5gsD14dm+JLV+tFWbgCgkTtO
O4UKunW68kPwl6a/QQDvbGw=
=ap6y
-----END PGP SIGNATURE-----

--AJ3oM32U01nchLSz--
 
S

Stephan Kämper

STEPHEN said:
I am writing a program to go in all the folders, then in to there folders....

You might like to read
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/20172

and the answers to that one. What's done there is to add a walker method
to class DIR - which as the name implies walks through the directory and
uses a passed block to do something with the current path name.

I currently have code like this

class Dir
def walk ( prefix = nil, &walker )
e = self.entries
e.delete "."
e.delete ".."

e.collect!{ | x | File.join( prefix, x ) } if prefix

if block_given?
e.each { | x |
if File.directory?( x )
Dir.new( x ).walk( x, &walker )
end
yield x
}
end
nil
end
end

Dir.new( "E:/" ).walk( "E:/" ) { | p | puts p if p =~ /ruby/ }

But note that this code won't work on XP if there are any directories of
files which can only be read with administrator rights: You'll run into
a 'permission denied (Errno::EACCES)'...
I'll have to work on that.


Happy rubying

Stephan
 
R

Robert Klemme

On Sat, 25 Sep 2004, STEPHEN BECKER I V wrote:


this might be of help

harp:~/eg/ruby > ruby -e 'p Dir["**/*"]'
["vsdb", "vsdb/vsdb-0.0.0", "vsdb/vsdb-0.0.0/ruby_vsdb.c",
"vsdb/vsdb-0.0.0/mkmf.log", "vsdb/vsdb-0.0.0/extconf.rb",
"vsdb/vsdb-0.0.0/Makefile", "vsdb/vsdb-0.0.0/ruby_vsdb.o",
"vsdb/vsdb-0.0.0/vsdb.so", "vsdb/vsdb-0.0.0/vsdb",
"vsdb/vsdb-0.0.0/vsdb/dbd.13", "vsdb/vsdb-0.0.0/vsdb/dbd.13/db.13",
"vsdb/vsdb-0.0.0/vsdb/db.cache", "vsdb/vsdb-0.0.0/test.rb",
"vsdb/vsdb-0.0.0/core.9780", "vsdb/vsdb-0.0.0/core.9781",
"vsdb/vsdb-0.0.0/core.9791", "vsdb/vsdb-0.0.0/core.9823",
"vsdb/vsdb-0.0.0/jib", "vsdb/vsdb-0.0.0/vsdb_test.rb", "templates",
"templates/main1.rb", "templates/inc.rb", "templates/class.rb",
"templates/test.rb", "templates/main0.rb", "templates/out", "templates/foo",
"templates/stp", "templates/stp/inc.rb", "templates/stp/class.rb",
"templates/foo-0.0.0", "templates/foo-0.0.0/lib",
"templates/foo-0.0.0/lib/foo-0.0.0",
"templates/foo-0.0.0/lib/foo-0.0.0/bar.rb",
"templates/foo-0.0.0/lib/foo-0.0.0.rb", "templates/lib",
"templates/lib/foo-0.0.0", "templates/lib/foo-0.0.0/bar.rb",
"templates/lib/foo-0.0.0.rb", "templates/lib/foo.rb", "templates/main2.rb",
"templates/install.rb", "templates/foobar", "templates/newclass",
"templates/newinc", "templates/newmain", "templates/main4.rb",
"templates/main3.rb", "templates/main5.rb", "templates/doc",
"templates/doc/created.rid", "templates/doc/rdoc-style.css",
"templates/doc/files", "templates/doc/files/logging_rb.html",
"templates/doc/classes", "templates/doc/classes/c.src",
"templates/doc/classes/c.src/M000001.html",
"templates/doc/classes/c.src/M000002.html",
"templates/doc/classes/LogMethods.src",
"templates/doc/classes/LogMethods.src/M000003.html",
"templates/doc/classes/LogMethods.src/M000004.html",
"templates/doc/classes/LogMethods.src/M000005.html",
"templates/doc/classes/LogMethods.src/M000006.html",
"templates/doc/classes/LogMethods.src/M000007.html",
"templates/doc/classes/LogMethods.src/M000008.html",
"templates/doc/classes/LogMethods.src/M000009.html",
"templates/doc/classes/LogMethods.src/M000010.html",
"templates/doc/classes/LogMethods.src/M000011.html",
"templates/doc/classes/LogMethods.src/M000012.html",
"templates/doc/classes/LogMethods.src/M000013.html",
"templates/doc/classes/Logging.src",
"templates/doc/classes/Logging.src/M000014.html",
"templates/doc/classes/A.html", "templates/doc/classes/c.html",
"templates/doc/classes/LoggerExt.html",
"templates/doc/classes/LogMethods.html",
"templates/doc/classes/Logging.html", "templates/doc/fr_file_index.html",
"templates/doc/fr_class_index.html", "templates/doc/fr_method_index.html",
"templates/doc/index.html", "templates/stdout", "templates/log",
"templates/logging.rb", "templates/util.rb", "templates/quote.rb",
"templates/log.20040613", "templates/log.0", "templates/log.1",
"templates/=main5.rb.conf", "templates/main5.rb.conf", "rq-all",
"rq-all/packages", "rq-all/packages/sqlite-ruby-1.3.1.tar.gz",
"rq-all/packages/posixlock-0.0.1.tgz",
"rq-all/packages/arrayfields-3.4.0.tgz",
"rq-all/packages/lockfile-1.1.0.tgz",
"rq-all/packages/sqlite-2.8.15.tar.gz", "rq-all/packages/ruby-1.8.1.tar.gz",
"rq-all/packages/rq-0.1.5.tgz", "rq-all/install.sh", "rq-all/README",
"rq-all.tgz"]


There is also Find:
["."]
["./test"]
["./test/test-file.txt"]
["./test/t.rb"]
["./test/gsub-bm.rb"]
["./lib"]
["./lib/ruby"]
["./lib/ruby/WWParser.rb"]
["./lib/ruby/verbose2.rb"]
["./lib/ruby/verbose.rb"]
["./lib/ruby/TriesTest.rb"]
["./lib/ruby/Tries.rb"]
["./lib/ruby/Scanner.rb"]
["./lib/ruby/RandomSet.rb"]
["./lib/ruby/InstanceCache.rb"]
["./lib/ruby/generator.rb"]
["./lib/ruby/FileNames.rb"]
["./lib/ruby/ExtendedEnumerable.r
["./lib/ruby/Dependency.rb"]
["./lib/ruby/Associations.rb"]
["./foo"]
["./bin"]
["./bin/marshal-test.rb.bak"]
["./bin/marshal-test.rb"]
["./bin/marshal-test.bak"]
["./bin/marshal-test-bm.rb.bak"]
["./bin/marshal-test-bm.rb"]
["./bin/find-files.rb"]
["./bin/find-files.bak"]
["./.xinitrc"]
["./.inputrc"]
["./.fvwm"]
["./.fvwm/.fvwm2rc~"]
["./.fvwm/.fvwm2rc.bak"]
["./.fvwm/.fvwm2rc"]
["./.fonts.cache-1"]
["./.file.cache"]
["./.emacs~"]
["./.emacs.d"]
["./.emacs.d/auto-save-list"]
["./.emacs"]
["./.cvspass"]
["./.bash_profile"]
["./.bash_history"]
["./.bashrc"]
["./.aliases"]
=> nil

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top