Created
November 1, 2012 17:57
-
-
Save pootsbook/3995378 to your computer and use it in GitHub Desktop.
PHP -> Ruby: Listing Directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Removed the HTML for readability | |
<?php | |
$Directory = opendir("../"); | |
while($entryName = readdir($Directory)) { | |
$dirArray[] = $entryName; | |
} | |
closedir($Directory); | |
$jCount = count($dirArray); | |
sort($dirArray); | |
for($j=0; $j<$jCount; $j++) { | |
$file = $dirArray[$j]; | |
if ( substr($file,0,1) != "." ) { | |
echo $file; | |
echo filetype($file); | |
echo filesize($file); | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# collect the file_names in the given directory | |
file_names = Dir.entries("../") | |
# remove hidden files from the resultant array | |
file_names.reject! {|d| d.chars.first.eql?('.') } | |
# sort the array | |
file_names.sort! | |
# turn each file_name string into a File object | |
files = file_names.map {|f| File.new(f) } | |
# iterate through the files and print the required information | |
files.each do |file| | |
puts file.path | |
puts File.extname(file.path) | |
puts file.size | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment