Def traverse_dir (file_path) If file. directory? File_path dir. foreach (file_path) Do | file | if file! = "." And file! = ".. "Traverse_dir (file_path +"/"+ file) end else puts" file: # {file. basename (file_path)}, size: # {file. size (file_path)} "endendtraverse_dir ('d:/Apache-Tomcat ')
Def traverse_dir (file_path) If file. directory? File_path (the directory function for determining whether it is a path name or a file name) dir. foreach (file_path) Do | file | (the Dir class contains all files under this path) If file! = "." And file! = ".. "(If you encounter. and .. will stop. indicates the upper-level directory) traverse_dir (file_path + "/" + file) (nested iteration) end else puts "file :#{ file. basename (file_path)}, size: # {file. size (file_path)} "(two functions of file are used) endendtraverse_dir ('. ')
The file class and Dir class are used.