Topic: Split a windows path?

I am trying to split a windows pathname down into the component bits but of course the bloody backslashes are calling problems. How would you split the following?

"C:\Documents and Settings\user\Desktop\screenshot.jpg"

The following doesn't work and I am not sure how to escape it.

"C:\Documents and Settings\user\Desktop\screenshot.jpg".split('\')

Using

"C:\Documents and Settings\user\Desktop\screenshot.jpg".split('\\')
results in
"C:Documents and SettingsuserDesktopscreenshot.jpg"

Basically I am trying to get the filename. I can't use the pathname2 gem to create a windows path because I am getting errors when I try and run it from the Gem.

Can anyone quikly give me the code I should put in the split command?

thanks

rJ

Re: Split a windows path?

the problem is that when you are constructing the string, "C:\Documents and Settings\user\Desktop\screenshot.jpg", it renders the backslashes as escape characters.  When you use this in a real application it wont be an issue, but since you're constructing the string for test purposes, you either need to escape the backslashes, or use single quotes.

"C:\\Documents and Settings\\user\\Desktop\\screenshot.jpg".split('\\')

or

'C:\Documents and Settings\user\Desktop\screenshot.jpg'.split('\\')

Re: Split a windows path?

Try this:

File.basename my_windows_path.gsub('\\', '/')
# or
File.basename 'C:\Documents and Settings\user\Desktop\screenshot.jpg'.gsub('\\', '/')

That should output 'screenshot.jpg'

Re: Split a windows path?

Many thanks all. Tis working now, just one problem left to solve! (Which I may end up asking in this groovy forum sooner or later).

Re: Split a windows path?

By all means ask! :-)

Re: Split a windows path?

I'm coming back to the described issue for Windows paths.
I can't figure out  where is the problem when executing 2 almost identical code blocks, - the first works without problems:

#encoding: utf-8
require 'spreadsheet'

folder = "\\\\Server100\\Folder1\\Folder2\\Folder3"
file = folder << "\\" << "olé-ole-file.xls"
Spreadsheet.client_encoding = 'UTF-8'
Spreadsheet.open(file) do |book|
  puts "sheets: #{book.worksheets.size}"
  sheet = book.worksheet(0)      
  puts "First sheet name: #{sheet.name}"
end

The second - not at all:

#encoding: utf-8
require 'spreadsheet'

folder = "\\\\Server100\\Folder1\\Folder2\\Folder3"
pattern = ""olé-ole-file*.xls""
Dir.chdir(folder)      
source_files = Dir.glob(pattern) 
recent_file = source_files.max_by { |f1| File.mtime(f1) }
file = folder << "\\" << recent_file
Spreadsheet.client_encoding = 'UTF-8'
Spreadsheet.open(file) do |book|
  puts "sheets: #{book.worksheets.size}"
  sheet = book.worksheet(0)      
  puts "First sheet name: #{sheet.name}"
end

What's wrong here? The error I get is the following:

C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.6.8/lib/spreadsheet/excel/reader.rb:1172:in `setup': undefined method `read' for false:FalseClass (NoMethodError)
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.6.8/lib/spreadsheet/excel/reader.rb:121:in `read'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.6.8/lib/spreadsheet/excel/workbook.rb:32:in `open'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.6.8/lib/spreadsheet.rb:62:in `open'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.6.8/lib/spreadsheet.rb:65:in `block in open'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.6.8/lib/spreadsheet.rb:64:in `open'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.6.8/lib/spreadsheet.rb:64:in `open'

It happened on Windows XP SP3 box, Ruby 1.9.3, spreadsheet 0.6.8.
Thank you.

Re: Split a windows path?

What is more strange, is that when I check if the file exists in the second block:

if File.exists?(file)
  puts "exists: #{File.stat(file).mtime}, #{File.readable?(file)}"
else
  puts "not"
end

it returns TRUE.

Last edited by Javix (2012-03-26 08:42:35)