江边闲话集

01/02/2008

Ruby遍历文件夹

Filed under: 一技之长 — Tags: , — 张太国 @ 17:26

哈哈,这么简单的问题居然也敢拿出来献丑。没办法,天生愚钝吧!
实现该功能在其他语言里那就是小菜一碟,当然ruby也是。不多说,贴代码:
 def traverse_dir(file_path)
    if File.directory? file_path
      puts "Dir:"+file_path
      Dir.foreach(file_path) do |file|
        if file!="." and file!=".."
        traverse_dir(file_path+"/"+file)
        end
      end
    else
      puts "File:"+file_path
    end
  end
 
traverse_dir "d:"

太简单了

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress