0

I have the following code which reads each line of a csv and cleans up each row. The rows are all path\ file name directories. I am having an issue where the script cannot find a path\file because the file name has a - in it. The - (dash) is read by ruby as \x96 . Does anyone know how to get it to not do that, and to read the - as a dash?

This is what I have, but it is not working:

CSV.foreach("#{batch_File_Dir_sdata}") do |ln|
        line_number += 1
        pathline = ln.to_s
        log_linemsg = "Source #{line_number}= #{pathline}"
        log_line = ["#{$cname}","#{log_linemsg}","","",]
        puts log_linemsg
        insert_logitems(connection, table_namelog, log_line)
        if pathline.include?("\\")
            cleanpath = pathline.gsub!("\\\\","\\")
            #cleanpath = cleanpath.gsub!("[","")
            #cleanpath = cleanpath.gsub!("]","")
            cleanpath.gsub!("\"","")
    #THIS IS THE LINE WHERE I AM TRYING TO FIX THE ISSUE        
            cleanpath.gsub!("\\x96","\-")
            cleanpath.slice!(0)
            cleanpath.chop!
            #puts "Clean path - has backslash\n#{cleanpath}"
        else
            cleanpath = pathline
            #puts "#{cleanpath}"
            #puts "Clean path - has NO backslash\n#{cleanpath}"
        end

Any help would be greatly appreciated.

missscripty
  • 529
  • 2
  • 11
  • 30
  • might be helpful to add a few of the lines that have issues so others can test without conjecture. Also what actual issue is it having because it seems like it might be easier yo split the line on a "\" and then join it back together. – engineersmnky Apr 25 '14 at 20:39
  • sorry. a possible line is M:\path1\Path 2\Path3\File - name.doc To make this more difficult, I found the - is not a simple dash. Its an extended dash. That is why it's having the problem. I've been looking it up for hours! – missscripty Apr 25 '14 at 20:45
  • and the output you are hoping for? – engineersmnky Apr 25 '14 at 20:46
  • 2
    Based on [this answer](http://stackoverflow.com/a/9420531/3438854) and what I _think_ you're asking, `encode` might be the way to go. Eliminate/convert anything that's not path-friendly, then slice and dice. – John C Apr 25 '14 at 20:52
  • I need it to read the file path as is. I tried encode and it removes that dash in ruby. If I can't get ruby to read the file and path that exists, it won't find the file. I can't change the file name. It has to be what it is. – missscripty Apr 25 '14 at 21:30

0 Answers0