Hey!
How do i check if a file exists, that starts with a certain identifier?
e.g. Files in Directory
file_1.rb
file_2.rb
test_1.rb
I only want to know if a file exists, that starts with “file_”
thx
Hey!
How do i check if a file exists, that starts with a certain identifier?
e.g. Files in Directory
file_1.rb
file_2.rb
test_1.rb
I only want to know if a file exists, that starts with “file_”
thx
Christian K. wrote:
I only want to know if a file exists, that starts with “file_”
Dir.entries(’.’).detect {|f| f.match /^file_/}
Replace ‘.’ with the name of the directory to check if it is not the
current directory. Will return the name of the first file to match if
there is one, else will return nil.
[mailto:[email protected]] On Behalf Of Christian K.
there are many ways.
these are just some…
Dir.glob(“file_*.rb”).empty?
#=> false
Dir.glob(“file_*.rb”).size>0
#=> true
Dir.glob(“file_*.rb”)
#=> [“file_1.rb”, “file_2.rb”, “file_3.rb”]
File.exist? “file_1.rb”
#=> true
File.exist? “file_2.rb”
#=> true
File.exist? “file_4.rb”
#=> false
kind regards -botp
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs