Skip to content

Instantly share code, notes, and snippets.

@felmoltor
Created August 27, 2014 16:09
Show Gist options
  • Save felmoltor/a9f9445413c210b1b6c4 to your computer and use it in GitHub Desktop.
Save felmoltor/a9f9445413c210b1b6c4 to your computer and use it in GitHub Desktop.
####################################################
# List all the files recursively under a directory #
####################################################
def ls_r_files(path)
path.gsub!(/\/+$/,"")
if path[-2,2] != "/." and path[-2,3] != "/.."
if File.directory?(path)
fentries = []
entries = Dir.entries(path)
entries.each{|entry|
if entry != "." and entry != ".."
if File.directory?("#{path}/#{entry}")
ientries = ls_r_files("#{path}/#{entry}")
if !ientries.nil?
ientries.each{|ientry|
fentries << ientry
}
end
else
fentries << "#{path}/#{entry}"
end
end
}
return fentries
else
return path
end
else
return nil
end
end
##########################################################
# List all the directories recursively under a directory #
##########################################################
def ls_r_directories(path)
path.gsub!(/\/+$/,"")
if path[-2,2] != "/." and path[-2,3] != "/.."
if File.directory?(path)
dentries = []
entries = Dir.entries(path)
entries.each{|entry|
if entry != "." and entry != ".."
if File.directory?("#{path}/#{entry}")
dentries << "#{path}/#{entry}"
ientries = ls_r_directories("#{path}/#{entry}")
if !ientries.nil?
ientries.each{|ientry|
dentries << ientry
}
end
end
end
}
return dentries
end
else
return nil
end
end
#########
f = ls_r_files("/home/felmoltor/Tools/test1")
d = ls_r_directories("/home/felmoltor/Tools/test1")
f.each{|file| puts file }
d.each{|dir| puts dir }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment