How do I get a listing of only files using Dir.glob?

How can I return a list of only the files, not directories, in a specified directory?

I have my_list = Dir.glob(script_path.join("*"))

This returns everything in the directory,including subdirectories. I searched but haven't been able to find the answer.

-------------Problems Reply------------

In addition to Mark's answer, Dir.entries will give back directories. If you just want the files, you can test each entry to see if it's a file or a directory, by using file?.

Dir.entries('/home/theiv').select { |f| File.file?(f) }

Replace /home/theiv with whatever directory you want to look for files in.

Also, have a look at File. It provides a bunch of tests and properties you can retrieve about files.

Dir.glob('*').select { |fn| File.file?(fn) }

Entries don't do rescursion i think. If you want the files in the subdirs also use

puts Dir['**/**/*'].select { |f| File.file?(f) }

If you want to do it in one go instead of first creating an array and then iterating over it with select, you can do something like:

my_list = []
Dir.foreach(dir) { |f| my_list << f if File.file?(f) }

It sounds like you're looking for Dir.entries:

Returns an array containing all of the filenames in the given directory. Will raise a SystemCallError if the named directory doesn’t exist.

If searching Google for how to solve this problem isn't turning up any results, you can look through the Ruby documentation.

Category:ruby Views:1 Time:2011-10-06
Tags: ruby

Related post

  • What is unit testing? 2008-08-04

    I saw many questions asking 'how' to unit test in a specific language, but no question asking 'what', 'why', and 'when'. What is it? What does it do for me? Why should I use it? When should I use it (also when not)? What are some common pitfalls and

  • Using a DOS command in Ruby script 2008-08-05

    How do I call shell commands from inside of a Ruby program? How do I then get output from these commands back into Ruby? --------------Solutions------------- This explanation is based on this commented Ruby script from a friend of mine. If you want t

  • DISCUSSION - Uploading and Storing Mass Images 2008-08-06

    So I'm using an app that stores images heavily in the DB. What's your outlook on this? I'm more of a type to store the location in the filesystem, than store it directly in the DB. What do you think are the pros/cons? --------------Solutions---------

  • storing files to a database or just to the local hard-disk? 2008-08-06

    So I'm using an app that stores images heavily in the DB. What's your outlook on this? I'm more of a type to store the location in the filesystem, than store it directly in the DB. What do you think are the pros/cons? --------------Solutions---------

  • Where is a good place to start programming GUIs for windows? 2008-08-07

    I have experience writing console and network client/server applications in C and C++, but I know next to nothing about using the win32 visual API, MFC, Qt, wxWidgets, etc. Where is a good place to start, and what method should I specialize in, so as

  • How to include PHP files that require an absolute path? 2008-08-07

    I have a directory structure like the following; /script.php /inc/include1.php /inc/include2.php /objects/object1.php /objects/object2.php /soap/soap.php Now, I use those objects in both script.php and /soap/soap.php, I could move them, but I want th

  • How do I retrieve my MySQL username and password? 2008-08-07

    I lost my MySQL username and password. How do I retrieve it? --------------Solutions------------- Stop the MySQL process. Start the MySQL process with the --skip-grant-tables option. Start the MySQL console client with the -u root option. List all th

  • Best Multi-Language Documentation Generator 2008-08-07

    What is the best documentation generator? I want to something that will easily add templates for documenting functions, classes, etc. I know there are several tools out there -- from Visual Studio plugins to external applications that take code files

  • Where can I get the Windows Workflow "wca.exe" application? 2008-08-07

    I am walking through the MS Press Windows Workflow Step-by-Step book and in chapter 8 it mentions a tool with the filename "wca.exe". This is supposed to be able to generate workflow communication helper classes based on an interface you provide it.

  • How do I update Ruby Gems from behind a Proxy (ISA-NTLM) 2008-08-07

    Firewall I'm behind is running Microsoft ISA server (NTLM only mode), anyone have success getting their Ruby gems to install/update via Ruby SSPI gem or other method? ... or am I just being lazy? Edit: rubysspi-1.2.4 does not work UPDATE: This also w

  • ruby script/plugin discover RuntimeError 2008-08-07

    Firewall I'm behind is running Microsoft ISA server (NTLM only mode), anyone have success getting their Ruby gems to install/update via Ruby SSPI gem or other method? ... or am I just being lazy? Edit: rubysspi-1.2.4 does not work UPDATE: This also w

  • How to easily consume a web service from PHP 2008-08-07

    Is there available any tool for PHP which can be used to generate code for consuming a web service based on its WSDL? Something comparable to clicking "Add Web Reference" in Visual Studio or the Eclipse plugin which does the same thing for Java. ----

  • Vista or XP for Dev Machine 2008-08-07

    I am about to get a new PC from work, and it will include the option to have either Vista Business as the OS, or a downgrade to XP Pro. Aside from a tiny bit of testing, I have never used Vista, but overall I have heard many more bad reports than goo

  • Can I configure Visual Studio NOT to change StartUp Project everytime I open a file from one of the projects? 2008-08-07

    Let's say that there is a solution that contains two projects (Project1 and Project2). Project1 is set as a StartUp Project (its name is displayed in a bold font). I double-click some file in Project2 to open it. The file opens, but something else ha

  • How to know when to send a 304 Not Modified response 2008-08-07

    I'm writing a resource handling method where I control access to various files, and I'd like to be able to make use of the browser's cache. My question is two-fold: Which are the definitive HTTP headers that I need to check in order to know for sure

  • MAPI and managed code experiences? 2008-08-07

    Using MAPI functions from within managed code is officially unsupported. Apparently, MAPI uses its own memory management and it crashes and burns within managed code (see here and here) All I want to do is launch the default e-mail client with subjec

  • SQL Server 2005 and 2008 on same developer machine? 2008-08-07

    Has anyone tried installing SQL Server 2008 Developer on a machine that already has 2005 Developer installed? I am unsure if I should do this, and I need to keep 2005 on this machine for the foreseeable future in order to test our application easily.

  • HTTP: Generating ETag Header 2008-08-07

    How do I generate an ETag HTTP header for a resource file? --------------Solutions------------- An etag is an arbitrary string that the server sends to the client that the client will send back to the server the next time the file is requested. The e

  • Simple MOLAP solution 2008-08-07

    To analyze lots of text logs I did some hackery that looks like this Locally import logs into Access Reprocess Cube link to previous mdb in Analisis Service 2000 (yes it is 2k) Use Excel to visualize Cube (it is not big - up to milions raw entries) M

  • Video Compression: What is discrete cosine transform? 2008-08-07

    I've implemented an image/video transformation technique called discrete cosine transform. This technique is used in MPEG video encoding. I based my algorithm on the ideas presented at the following URL: http://vsr.informatik.tu-chemnitz.de/~jan/MPEG

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.090 (s). 11 q(s)