Convert .CSV to Tab Delimited

 'This VBScript is for converting CSV files to TSV (tab delimited) files by batch.  
 'Copy and paste it to a Notepad and save with a vbs extension (e.g. CSV2TSV.vbs) under the same folder of csv files.  
 'Double click CSV2TSV.vbs to do the conversion.  
 'The converted files will be saved as *.tsv while the original files remain.  
   
 Dim objFSO, objFile, objFileTSV  
 Dim strLine, strNewLine, strNewFileName  
 Dim TotalFilesConverted, FileNameLength  
 set WshShell = CreateObject("WScript.Shell")  
 Set objFSO = CreateObject("scripting.filesystemobject")  
 strCurPath = objFSO.GetAbsolutePathName(".")  
 TotalFilesConverted = 0  
 For Each objFile In objFSO.getfolder(strCurPath).Files  
      If UCase(Right(objFile.Name, 4)) = ".CSV" Then  
           Result = WshShell.Popup("Converting " & objFile.Name & " ...",3,"")  
           FileNameLength = Len(objFile.Name)-4  
           strNewFileName = Left(objFile.Name,FileNameLength) & ".tsv"  
           Set objFile = objFSO.OpenTextFile(objFile, 1)  
           Set objFileTSV = objFSO.CreateTextFile(strNewFileName)  
           Do Until objFile.AtEndOfStream  
                strLine = objFile.ReadLine  
                If instr(strLine,Chr(34)) =0 Then  
                     strNewLine = Replace(strLine,",",vbTab)  
                Else  
                     Call LineQuote(strNewLine)  
                End if  
                objFileTSV.WriteLine strNewLine  
           Loop  
           objFile.Close  
           TotalFilesConverted = TotalFilesConverted +1  
           objFileTSV.Close  
      End If  
 Next  
   
   
 Sub LineQuote(strNewLine)  
 Dim LineLength, Linepos, Quote, QuoteCount, Quotepos  
 LineLength = Len(strLine)  
 Linepos =1  
 strNewLine =""  
 Quote = False  
 QuoteCount = 0  
 Do While Linepos <= LineLength  
      Quotepos = instr(Mid(strLine,Linepos,LineLength-Linepos+1),Chr(34))  
      If Quotepos = 1 Then  
           If Linepos < LineLength Then  
                If Mid(strLine,Linepos,2) = Chr(34) & Chr(34) and Quote Then  
                     strNewLine = strNewLine & Chr(34)  
                     Linepos = Linepos +2  
                Else 'one quote  
                     QuoteCount = QuoteCount +1  
                     If QuoteCount =2 Then  
                          Quote = False  
                          QuoteCount =0  
                     Else  
                          Quote = True  
                     End if  
                     Linepos = Linepos +1  
                End if  
           Else 'last character  
                Linepos = Linepos +1  
           End if  
      Elseif Quotepos >1 Then  
           If Quote Then  
                strNewLine = strNewLine + Mid(strLine,Linepos,Quotepos-1)  
           Else 'not Quote  
                strNewLine = strNewLine + Replace(Mid(strLine,Linepos,Quotepos-1),",",vbTab)  
           End if  
           Linepos = Linepos +Quotepos -1  
      Elseif Quotepos =0 Then  
           strNewLine = strNewLine + Replace(Mid(strLine,Linepos,LineLength-Linepos+1),",",vbTab)  
           Linepos = LineLength +1  
      End If  
 Loop  
 End Sub  
   

No comments:

Post a Comment