Cómo comprobar si existe un directorio / archivo / enlace simbólico con un comando en Ruby
Existe una única forma de detectar si un directorio/archivo/enlace simbólico/etc. entidad (más generalizada) existe?
Necesito una sola función porque necesito comprobar una matriz de rutas que podrían ser directorios, archivos o enlaces simbólicos. Sé que File.exists?"file_path"
funciona para directorios y archivos, pero no para enlaces simbólicos (que es File.symlink?"symlink_path"
).
54
Author: codeforester, 2011-02-04
2 answers
El módulo de archivo estándar tiene las pruebas de archivo habituales disponibles:
RUBY_VERSION # => "1.9.2"
bashrc = ENV['HOME'] + '/.bashrc'
File.exist?(bashrc) # => true
File.file?(bashrc) # => true
File.directory?(bashrc) # => false
Usted debe ser capaz de encontrar lo que quiere allí.
OP: "Gracias, pero necesito los tres verdaderos o falsos"
, Obviamente, no. Ok, intenta algo como:
def file_dir_or_symlink_exists?(path_to_file)
File.exist?(path_to_file) || File.symlink?(path_to_file)
end
file_dir_or_symlink_exists?(bashrc) # => true
file_dir_or_symlink_exists?('/Users') # => true
file_dir_or_symlink_exists?('/usr/bin/ruby') # => true
file_dir_or_symlink_exists?('some/bogus/path/to/a/black/hole') # => false
110
Author: the Tin Man,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-02 06:42:53
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-02 06:42:53
¿Por qué no definir su propia función File.exists?(path) or File.symlink?(path)
y usarla?
12
Author: Gintautas Miliauskas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-04 11:52:28
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-04 11:52:28