遍历一个目录中的每个文件

发布于 2024-08-26 09:58:36 字数 164 浏览 3 评论 0原文

如何在 ruby​​ 中编写循环以便可以在每个文件上执行代码块?

我是 ruby​​ 新手,我得出的结论是,执行此操作的方法是执行每个循环。
ruby 文件将从与我想要循环的目录不同的目录中执行。

我尝试过 Dir.foreach 但无法让它工作。

How do I write a loop in ruby so that I can execute a block of code on each file?

I'm new to ruby, and I've concluded that the way to do this is a do each loop.
The ruby file will be executed from a different directory than the directory I want to loop through.

I've tried the Dir.foreach and I couldn't get it to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

凉世弥音 2024-09-02 09:58:36

正如其他人所说, Dir::foreach< /a> 在这里是一个不错的选择。但是,请注意 Dir::foreach< code>Dir::entries 将始终包含 ... (当前目录和父目录)。您通常不想处理它们,因此您可以使用 Dir::each_child目录::children (如ma11hew28建议)或执行以下操作:

Dir.foreach('/path/to/dir') do |filename|
  next if filename == '.' or filename == '..'
  # Do work on the remaining files & directories
end

Dir::foreachDir::entries (以及 Dir::each_childDir::children)还包括隐藏文件和目录。通常这是您想要的,但如果不是,您需要采取一些措施来跳过它们。

或者,您可能需要查看 Dir::glob提供简单的通配符匹配:

Dir.glob('/path/to/dir/*.rb') do |rb_filename|
  # Do work on files & directories ending in .rb
end

As others have said, Dir::foreach is a good option here. However, note that Dir::foreach and Dir::entries will always include . and .. (the current and parent directories). You will generally not want to work on them, so you can use Dir::each_child or Dir::children (as suggested by ma11hew28) or do something like this:

Dir.foreach('/path/to/dir') do |filename|
  next if filename == '.' or filename == '..'
  # Do work on the remaining files & directories
end

Dir::foreach and Dir::entries (as well as Dir::each_child and Dir::children) also include hidden files & directories. Often this is what you want, but if it isn't, you need to do something to skip over them.

Alternatively, you might want to look into Dir::glob which provides simple wildcard matching:

Dir.glob('/path/to/dir/*.rb') do |rb_filename|
  # Do work on files & directories ending in .rb
end
花开雨落又逢春i 2024-09-02 09:58:36

这是我最喜欢的易于阅读的方法:

Dir.glob("*/*.txt") do |my_text_file|
  puts "working on: #{my_text_file}..."
end

您甚至可以扩展它以适用于子目录中的所有文件:

Dir.glob("**/*.txt") do |my_text_file| # note one extra "*"
  puts "working on: #{my_text_file}..."
end

This is my favorite method for being easy to read:

Dir.glob("*/*.txt") do |my_text_file|
  puts "working on: #{my_text_file}..."
end

And you can even extend this to work on all files in subdirs:

Dir.glob("**/*.txt") do |my_text_file| # note one extra "*"
  puts "working on: #{my_text_file}..."
end
猫弦 2024-09-02 09:58:36

Dir 还具有更短的语法来从目录中获取所有文件的数组:

Dir['dir/to/files/*'].each do |fname|
    # do something with fname
end

Dir has also shorter syntax to get an array of all files from directory:

Dir['dir/to/files/*'].each do |fname|
    # do something with fname
end
知你几分 2024-09-02 09:58:36
Dir.foreach("/home/mydir") do |fname|
  puts fname
end
Dir.foreach("/home/mydir") do |fname|
  puts fname
end
这个俗人 2024-09-02 09:58:36

find 库是专门为此任务而设计的:
https://ruby-doc.org/stdlib- 2.5.1/libdoc/find/rdoc/Find.html

require 'find'
Find.find(path) do |file|
  # process
end

这是一个标准的 ruby​​ 库,所以它应该可用

The find library is designed for this task specifically:
https://ruby-doc.org/stdlib-2.5.1/libdoc/find/rdoc/Find.html

require 'find'
Find.find(path) do |file|
  # process
end

This is a standard ruby library, so it should be available

风透绣罗衣 2024-09-02 09:58:36

跳过 . & ..,您可以使用 Dir ::each_child

Dir.each_child('/path/to/dir') do |filename|
  puts filename
end

Dir::children 返回一个文件名数组。

To skip . & .., you can use Dir::each_child.

Dir.each_child('/path/to/dir') do |filename|
  puts filename
end

Dir::children returns an array of the filenames.

呆° 2024-09-02 09:58:36

我喜欢这个,上面没有提到。

require 'pathname'

Pathname.new('/my/dir').children.each do |path|
    puts path
end

好处是你得到一个 Pathname 对象而不是字符串,你可以用它做有用的事情并进一步遍历。

I like this one, that hasn't been mentioned above.

require 'pathname'

Pathname.new('/my/dir').children.each do |path|
    puts path
end

The benefit is that you get a Pathname object instead of a string, that you can do useful stuff with and traverse further.

岁吢 2024-09-02 09:58:36
Dir.new('/my/dir').each do |name|
  ...
end
Dir.new('/my/dir').each do |name|
  ...
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文