Dir.glob fails when using File.join

On a Windows machine using SU 2020
I am grabbing the skp file name from the model.
project = Sketchup.active_model.path.split(’.’)[0]

Then I am joining additional folders to the path
file_spec = File.join(proj, ‘rul’, ‘*.txt’)

The result is
E:\Users\Owner\My Documents\new test/rul/*.txt

Dir.glob(file_spec) # fails

If I replace the backslash with a forward slash
proj.gsub!(’\’, ‘/’)

Then it Dir.glob works

What is the best way to handle this for MacOS and Windows

one way is use #expand_path to get the file separators…

project = File.expand_path(Sketchup.active_model.path)[0..-5]


Ruby uses ‘/’ as the path separator, which makes it problematic that model#path on Windows returns a path using ‘’. This causes Dir.glob to fail because in its pattern, ‘’ is explicitly reserved as the escape character.

I think your gsub! is the most obvious and portable way to fix the issue, as it is harmless on Mac and does what you want on Windows.

def slash(path)
  return path if !WIN

Don’t use the bang form as you could get nil returned upon no change.

a backslash is a legal mac filename char, people do use it… (ill advisably)

letting ruby supply the ruby path is safer…


This is actually dangerous, as folder names can include periods.

Safer …

def project_path(model = Sketchup.active_model)
  path = slash(model.path)
  dir = File.dirname(path)
  File.join( dir, model.title, rul)

# Elsewhere ...
file_spec = File.join(project_path(), '*.txt')

Good catch. gsub and gsub! differ in this respect. Ya gotta love consistency!

Thanks guys - this should give me enough to clean things up.

  # replace