Tuesday, May 10, 2016

VBA: Recognizing Unix-style Files

I was trying to figure out a good way to determine if a file being read has a line terminator of CR/LF (Windows/DOS) or LF (Unix/Linux). Since VBA Line Input assumes that the file is in the Windows format (CR/LF line terminator), it reads in the entire file if the file is in the Unix format. Here's a function that determines how many columns are in a CSV file regardless the format format (except for the Mac format, which terminates each line with CR):
  1. ' ----------------------------------------------------------------------------  
  2. ' get the number of columns by reading the 1st line which should be the header  
  3. '  
  4. Private Function GetNumCols(pFile) As Integer  
  5.     Dim data As String  
  6.     Dim temp As Variant  
  7.   
  8.   
  9.     Open pFile For Input As #1  
  10.     Line Input #1, data  
  11.     Close #1  
  12.     ' play the following trick in case CSV is in the Unix format  
  13.     ' algorithm:  
  14.     ' 1. read one line. If Unix file, it will read the entire file  
  15.     ' 2. split the line with linefeed char  
  16.     ' 3. if there are more than 1 linefeed chars, it's unix  
  17.     ' 4. if Unix, split the first element of the array that were split by  
  18.     '    linefeed by ","  
  19.     ' 5. if Windows, split the input line by ","  
  20.     temp = Split(data, vbLf)  
  21.     If UBound(temp) > 2 Then  
  22.         temp = Split(temp(1), ",")  
  23.     Else  
  24.         temp = Split(data, ",")  
  25.     End If  
  26.     GetNumCols = UBound(temp) + 1  
  27. End Function  

2 comments:

  1. Thanks for sharing this information. I really like your blog post very much. You have really shared a informative and interesting blog post with people..
    microsoft excel vba training

    ReplyDelete
  2. Thank you! I am glad you like it.

    ReplyDelete