New topic: 

Convert a COMMA delimited file to a TAB delimited file

<http://forums.realsoftware.com/viewtopic.php?t=43951>

         Page 1 of 1
   [ 1 post ]                 Previous topic | Next topic          Author  
Message        DaveS          Post subject: Convert a COMMA delimited file to a 
TAB delimited filePosted: Thu May 10, 2012 3:30 pm                              
   
Joined: Sun Aug 05, 2007 10:46 am
Posts: 3667
Location: San Diego, CA                and HONOR Double quotes at the same time!

it will determine if the file is comma or tab delmited and convert it to TAB if 
comma otherwise leaves it alone

you need to supply INP_F and OUT_F as folderitems

Dim t As TextInputStream
Dim i As Integer
Dim j As Integer
Dim tb As Integer
Dim cm As Integer
Dim s As String
Dim temp(-1) As String
tb=0
cm=0
t=TextInputStream.Open(inp_f)
s=t.readall
t.close
s=ReplaceLineEndings(s,EndOfLine.UNIX)
list=Split(s,EndOfLine.UNIX)
// remove blank lines and determine delimiter
If list.ubound>0 Then
  For i=list.Ubound DownTo 0
  s=Trim(list(i))
  If s="" Then
  list.remove i
  Else
  tb=tb+CountFields(list(i),ChrB(9))-1
  cm=cm+CountFields(list(i),",")-1
  End If
  Next i
End If
//
// If commas out number TABS then it must be a comma delimited file
//
If cm>tb Then ' file is COMMA delimited! change it to TAB (watch out for ")
  For i=0 To list.ubound
  s=list(i)
  If InStr(s,ChrB(34))=0 Then ' no " so do it fast
  s=ReplaceAll(s,",",ChrB(9))
  Else
  temp=Split(s,",")
  For j=temp.ubound DownTo 1
    If Left(temp(j-1),1)=ChrB(34) And Right(temp(j),1)=ChrB(34) Then
    temp(j-1)=Mid(temp(j-1),2)+","+Left(temp(j),Len(temp(j))-1)
    temp.remove j
    End If
  Next j
  s=Join(temp,ChrB(9))
  s=ReplaceAll(s,ChrB(9)+ChrB(34),ChrB(9))
  s=ReplaceAll(s,ChrB(34)+ChrB(9),ChrB(9))
  s=ReplaceAll(s,ChrB(34)+ChrB(34),"'")
  End If
  list(i)=s
  Next i
End If
//
// Write the File back out
//
Dim xxx As TextOutputStream
xxx=TextOutputStream.Create(out_f)
s=Join(list,EndOfLine.UNIX)
xxx.write s
xxx.close
      
_________________
Dave Sisemore
MacPro, OSX 10.7.3 RB2011r3
Note : I am not  interested in any solutions that involve custom Plug-ins of 
any kind  
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 1 post ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to