Pobieranie listy folderów w katalogu
Jak uzyskać listę folderów, które istnieją w określonym katalogu z Rubim?
Dir.entries()
wygląda blisko, ale nie wiem, jak ograniczyć się tylko do folderów.
12 answers
Jordan jest blisko, ale Dir.entries
nie zwraca pełnej ścieżki, której File.directory?
oczekuje. Spróbuj tego:
Dir.entries('/your_dir').select {|entry| File.directory? File.join('/your_dir',entry) and !(entry =='.' || entry == '..') }
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-23 20:53:24
Znalazłem to bardziej przydatne i łatwe w użyciu:
Dir.chdir('/destination_directory')
Dir.glob('*').select {|f| File.directory? f}
Pobiera wszystkie foldery w bieżącym katalogu, z wyłączeniem .
i ..
.
Do rekurencji folderów wystarczy użyć **
zamiast *
.
Linia Dir.glob
może być również przekazana do Dir.chdir
jako blok:
Dir.chdir('/destination directory') do
Dir.glob('*').select { |f| File.directory? f }
end
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-22 13:06:37
Moim zdaniem {[2] } nadaje się znacznie lepiej do nazw plików niż zwykłe ciągi.
require "pathname"
Pathname.new(directory_name).children.select { |c| c.directory? }
To daje tablicę wszystkich katalogów w tym katalogu jako obiekty Pathname.
Jeśli chcesz mieć stringi
Pathname.new(directory_name).children.select { |c| c.directory? }.collect { |p| p.to_s }
Jeśli directory_name
było absolutne, te ciągi też są absolutne.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-23 20:55:24
Rekurencyjnie Znajdź wszystkie foldery pod określonym katalogiem:
Dir.glob 'certain_directory/**/*/'
Wersja nie rekurencyjna:
Dir.glob 'certain_directory/*/'
Uwaga: Dir.[]
działa jak Dir.glob
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-23 20:54:01
Możesz użyć File.directory?
z modułu FileTest
, aby dowiedzieć się, czy plik jest katalogiem. Połączenie tego z Dir.entries
sprawia, że jest ładny (ish)-liner:
directory = 'some_dir'
Dir.entries(directory).select { |file| File.directory? File.join(directory, file}
Edit: Updated per ScottD ' s correction.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-12-14 06:06:39
directory = 'Folder'
puts Dir.entries(directory).select { |file| File.directory? File.join(directory, file)}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-12-14 09:05:24
Dir.glob('/your_dir').reject {|e| !File.directory?(e)}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-26 18:16:56
$dir_target = "/Users/david/Movies/Camtasia 2/AzureMobileServices.cmproj/media"
Dir.glob("#{$dir_target}/**/*").each do |f|
if File.directory?(f)
puts "#{f}\n"
end
end
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-15 10:58:52
Myślę, że możesz przetestować każdy plik, aby sprawdzić, czy jest to katalog z FileTest.directory? (file_name)
. Zobacz dokumentację FileTest aby uzyskać więcej informacji.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-23 20:56:42
Do ogólnego rozwiązania, którego prawdopodobnie chcesz użyć
Dir.glob(File.expand_path(path))
To będzie działać ze ścieżkami takimi jak ~/*/
(wszystkie foldery w katalogu domowym).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-25 15:12:39
Możemy połączyć odpowiedź Borhai odpowiedź Johannesa, aby uzyskać całkiem eleganckie rozwiązanie do uzyskania nazw katalogów w folderze.
# user globbing to get a list of directories for a path
base_dir_path = ''
directory_paths = Dir.glob(File.join(base_dir_path, '*', ''))
# or recursive version:
directory_paths = Dir.glob(File.join(base_dir_path, '**', '*', ''))
# cast to Pathname
directories = directory_paths.collect {|path| Pathname.new(path) }
# return the basename of the directories
directory_names = directories.collect {|dir| dir.basename.to_s }
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-07 04:10:06
Only folders ("."i".."są wyłączone): {]}
Dir.glob(File.join(path, "*", File::SEPARATOR))
Foldery i pliki:
Dir.glob(File.join(path, "*"))
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-26 11:17:27