使用 ruby​​ 递归运行目录中的文件

发布于 2024-07-16 03:06:35 字数 343 浏览 6 评论 0原文

我现在正在编写脚本,它必须运行目录及其子文件夹中的每个 ruby​​ 脚本。

例如

run-all.rb
- scripts
  - folder1
    - script1.rb
    - script2.rb
  - folder2
    - script3.rb
    - script4.rb

,由于服务器是 Windows 服务器,我通常会使用批处理文件,但首席开发人员坚持认为一切都必须在 ruby​​ 中完成,因为一些成员拥有 Mac,可能不理解 Windows 批处理文件。

正如这个问题可能已经暴露的那样,我对 Ruby 的了解非常基础。

I'm working on script right now which has to run each ruby script in a directory and its subfolders.

e.g.

run-all.rb
- scripts
  - folder1
    - script1.rb
    - script2.rb
  - folder2
    - script3.rb
    - script4.rb

As the server is a Windows server I would normally use a batch file but the head dev insists everything must be done in ruby as some members have Macs and may not understand Windows Batch Files.

As the question may have given away, my knowledge of Ruby is very basic.

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

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

发布评论

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

评论(2

落花随流水 2024-07-23 03:06:35

取决于你所说的“运行”是什么意思。 要只执行同一个 ruby​​ 进程中每个脚本中的代码,这可以解决问题:

Dir["scripts/**/*.rb"].each{|s| load s }

但是如果您想在它自己的 ruby​​ 进程中运行每个脚本,那么请尝试以下操作:

Dir["scripts/**/*.rb"].each{|s| puts `ruby #{s}` }

只需将其中一个放入以下内容中run-all.rb 和 run ruby run-all.rb 构成命令行。

Depends what you mean by "run". To just execute the code that is in each script within the same ruby process, this will do the trick:

Dir["scripts/**/*.rb"].each{|s| load s }

But it you want to run each script in it's own ruby process, then try this:

Dir["scripts/**/*.rb"].each{|s| puts `ruby #{s}` }

Just put the either of these in the contents of run-all.rb and the run ruby run-all.rb form the command line.

陈年往事 2024-07-23 03:06:35

像这样的东西可能应该有效:

def process_directory(basedir)
puts basedir
Find.find(basedir.chomp) do |path|
    if FileTest.directory?(path)
        if File.basename(path)[0] == ?.
            Find.prune       # Don't look any further into this directory.
        else
            next
        end
    else
        puts path
    end
end

Something like this should probably work:

def process_directory(basedir)
puts basedir
Find.find(basedir.chomp) do |path|
    if FileTest.directory?(path)
        if File.basename(path)[0] == ?.
            Find.prune       # Don't look any further into this directory.
        else
            next
        end
    else
        puts path
    end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文