Assuming foo does not exist, you will receive no such file or directory error for: Dir. Iulian Onofrei 8, 9 9 gold badges 64 64 silver badges bronze badges. Glad that fileutils is the first alternative in the answer. Show 4 more comments. Licysca Licysca 1, 10 10 silver badges 4 4 bronze badges. One shall use File. Suppose there's a normal file with the same name.
You couldn't create a directory in such case. It also creates a race condition. The file may be created after the check but before creation. DonReba How so? Are the methods File. Licysca please consider updating your answer. This is not a clean answer and it is getting unjustified upvotes. Another simple way: Dir. If you want to create nested directories then this doesn't work.
For e. So the reliable solution is using FileUtils. How about just Dir. Vidar Vidar 11 11 silver badges 14 14 bronze badges. Since the underlying implementation relies on the unlink 2 system call, the type of exception raised depends on its error type see linux.
Returns true if the named file is a directory, or a symlink that points at a directory, and false otherwise. Returns true if the named file is executable by the effective user and group id of this process.
See eaccess 3. Windows does not support execute permissions separately from read permissions. On Windows, a file is only considered executable if it ends in. Returns true if the named file is executable by the real user and group id of this process. See access 3. A more complex example which also resolves parent directory is as follows. Returns the extension the portion of file name in path starting from the last period.
If path is a dotfile, or starts with a period, then the starting dot is not dealt with the start of the extension. An empty string will also be returned when the period is the last character in path. If the file argument is a symbolic link, it will resolve the symbolic link and use the file referenced by the link. Returns true if path matches against pattern.
The pattern is not a regular expression; instead it follows rules similar to shell filename globbing. It may contain the following metacharacters:. Matches any file. Can be restricted by other values in the glob. Matches any one character in set. Behaves like a Regexp union? The same glob pattern and flags are used by Dir::glob.
Returns true if the named file exists and the effective group id of the calling process is the owner of the file. Returns false on Windows. Equivalent to File::chmod , but does not follow symbolic links so it will change the permissions associated with the link, not the file referenced by the link. Often not available. Equivalent to File::chown , but does not follow symbolic links so it will change the owner associated with the link, not the file referenced by the link.
Returns number of files in the argument list. Creates a new name for an existing file using a hard link. Not available on all platforms. Same as File::stat , but does not follow the last symbolic link. Instead, reports on the link itself. Sets the access and modification times of each named file to the first two arguments.
If a file is a symlink, this method acts upon the link itself as opposed to its referent; for the inverse behavior, see File. Returns the number of file names in the argument list. Returns the modification time for the named file as a Time object. Opens the file named by filename according to the given mode and returns a new File object. See IO. If a file is being created, permission bits may be given in perm. These mode and permission bits are platform dependent; on Unix systems, see open 2 and chmod 2 man pages for details.
The new File object is buffered mode or non-sync mode , unless filename is a tty. With no associated block, File. If the optional code block is given, it will be passed the opened file as an argument and the File object will automatically be closed when the block terminates. The value of the block will be returned from File. If a file is being created, its initial permissions may be set using the perm parameter. See File.
Returns true if the named file exists and the effective used id of the calling process is the owner of the file. Returns true if the named file is readable by the effective user and group id of this process.
Returns true if the named file is readable by the real user and group id of this process. Returns the real absolute pathname of pathname in the actual filesystem. The real pathname doesn't contain symlinks or useless dots. Returns the real absolute pathname of pathname in the actual filesystem not containing symlinks or useless dots. Renames the given file to the new name. Raises a SystemCallError if the file cannot be renamed.
Splits the given string into a directory and a file component and returns them in a two-element array. See also File::dirname and File::basename.
Returns a File::Stat object for the named file see File::Stat. Raises a NotImplemented exception on platforms that do not support symbolic links.
Returns the current umask value for this process. Also, have a look at File. It provides a bunch of tests and properties you can retrieve about files.
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:. The difference between them is the former returns complete path, and the latter returns only filename. So be careful about the following mapping segment. Dir[] , Dir. It sounds like you're looking for Dir. Returns an array containing all of the filenames in the given directory.
If searching Google for how to solve this problem isn't turning up any results, you can look through the Ruby documentation. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How do I get a listing of only files using Dir. Ask Question. Asked 10 years, 3 months ago. Active 2 years, 11 months ago. Viewed 19k times.
0コメント