Home Forums Data Warehousing Integration Services Whats the easiest way to read the first 10 characters of a flat file RE: Whats the easiest way to read the first 10 characters of a flat file

  • in a kind-of-similar thread, someone had a table full of varbinary(max) datatypes full of graphic images, without the filename/extension, so they couldn't figure out if the file was *.jpg, *.gif or what.

    That thread is here if you are curious:

    http://www.sqlservercentral.com/Forums/Topic1178898-392-1.aspx

    when i was helping there, i wrote a snippet that read the first ten bytes in vb.NET from everything in a local folder so i could do the analysis and make some TSQL code to map the first ten bytes to known extensions.

    here's the vb code i used:

    Dim myFilePath As String = String.Empty

    Dim myFileBrowser As New OpenFileDialog

    Try

    With myFileBrowser

    'With statement is used to execute statements using a particular object, here,_

    'setting filters so that Text files and All Files choice appears in the Files of Type box

    'in the dialog

    If .ShowDialog() = DialogResult.OK Then

    'showDialog method makes the dialog box visible at run time

    myFilePath = .FileName

    End If

    End With

    Catch ex As Exception

    MsgBox(ex.Message)

    Finally

    'If Not (sr Is Nothing) Then

    ' sr.Close()

    'End If

    End Try

    'if we have a valid path, we can go forward.

    If myFilePath <> String.Empty Then

    ' Open a file that is to be loaded into a byte array

    Dim oFile As System.IO.FileInfo

    oFile = New System.IO.FileInfo(myFilePath)

    Dim oFileStream As System.IO.FileStream = oFile.OpenRead()

    Dim lBytes As Long = oFileStream.Length

    'the above would read the whole file into the byte array, we want just the 1st 10 bytes for testing.

    lBytes = 10

    If (lBytes > 0) Then

    Dim fileData(lBytes - 1) As Byte

    ' Read the file into a byte array

    oFileStream.Read(fileData, 0, lBytes)

    oFileStream.Close()

    Debug.Print(oFile.Extension & "||" & BitConverter.ToString(fileData))

    End If

    End If

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!