Check if directory is empty in Ruby

前端 未结 6 709
太阳男子
太阳男子 2020-12-14 15:57

How can I check to see if a directory is empty or not in Ruby? Is there something like:

Dir.exists?(\"directory\")

(I know that that functi

6条回答
  •  一个人的身影
    2020-12-14 16:04

    Here is my template for this one.FYI , i am looking for a certain match of files inside the source.

    mydir = "/home/to/mydir"
    
    Dir.chdir(mydir)
    
    if Dir.entries(mydir).select(|x| x != '.' && x != '..' && x =~ /\regex.txt\z/).size > 0
    
    
           do_something 
    
    elsif Dir.entries(mydir).select(|x| x != '.' && x != '..' && x =~ /\regex.txt\z/).size < 0
    
          do_something_else 
    
    else 
    
          puts "some warning message"
    end
    

    let me know if anything :)

提交回复
热议问题