Link to home
Start Free TrialLog in
Avatar of dba123
dba123

asked on

Check extension of incoming file in If statement

I'm trying to check the extension tye of the incoming file.  The problem lies here:

The code:

    Sub Main()

        ProcessFiles("\\sss\\sss_input.txt", "\\sss\mnt_output\")
        'ProcessFiles("\\sss\naf_input.mer", "\\sss\naf_output\")

    End Sub

    Public Sub ProcessFiles(ByVal sIncomingfile As String, ByVal sOutputDirectory As String)
        Dim f As New Security.Permissions.FileIOPermission(Security.Permissions.PermissionState.None)
        f.AllLocalFiles = Security.Permissions.FileIOPermissionAccess.Read

        Dim file As New IO.FileInfo(sIncomingfile)

        Dim filefs As New IO.FileStream(file.FullName, IO.FileMode.Open)
        Dim reader As New IO.StreamReader(filefs)
        Dim counter As Integer = 0

        Dim CurrentFS As IO.FileStream
        Dim CurrentWriter As IO.StreamWriter

        If file.FullName.Substring(Len(sIncomingfile), -3) = ".txt" Then  <---------------- MALFORMED HERE
            While Not reader.Peek < 0
                Dim Line As String = reader.ReadLine
                If IsNumeric(Line.Substring(0, 1)) Then
                    Dim Parts() As String = Line.Split(" "c)
                    If Parts(0).Length = 8 Then
                        counter += 1
                        If Not CurrentWriter Is Nothing Then CurrentWriter.Flush() : CurrentWriter.Close()
                        CurrentFS = New IO.FileStream(IO.Path.Combine(IO.Path.GetDirectoryName(sOutputDirectory), Line.Substring(59, 4) & "[" & counter.ToString & "]" & Now.ToString("MM-dd-yyyy") & IO.Path.GetExtension(file.FullName)), IO.FileMode.Create)
                        CurrentWriter = New IO.StreamWriter(CurrentFS)
                    End If

                    If Not CurrentWriter Is Nothing Then
                        CurrentWriter.WriteLine(Line)
                    End If

                End If
            End While

            If Not CurrentWriter Is Nothing Then CurrentWriter.Flush() : CurrentWriter.Close()
            'Console.Read()

            MoveFiles(sOutputDirectory)

        Else
               ---DO SOMETHING, I'LL CODE THIS LATER
        End If
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dba123
dba123

ASKER

Thanks Bob, didn't know I could do that yet!
Avatar of dba123

ASKER

I get the Error: Length cannot be less than zero.

       Dim f As New Security.Permissions.FileIOPermission(Security.Permissions.PermissionState.None)
        f.AllLocalFiles = Security.Permissions.FileIOPermissionAccess.Read

        Dim file As New IO.FileInfo(sIncomingfile)

        Dim filefs As New IO.FileStream(file.FullName, IO.FileMode.Open)
        Dim reader As New IO.StreamReader(filefs)
        Dim counter As Integer = 0

        Dim CurrentFS As IO.FileStream
        Dim CurrentWriter As IO.StreamWriter
        Dim extension As String = IO.Path.GetExtension(file.FullName)

        If file.FullName.Substring(Len(extension), -3) = ".mnt" Then
Avatar of dba123

ASKER

hold on, let me write out extension to a msgbox before you reply to see what's going on.
Avatar of dba123

ASKER

well, it's working, the MsgBox returned .mnt so I don't need anything but extension now.  Thanks Bob!
Avatar of dba123

ASKER

Dim extension As String = IO.Path.GetExtension(file.FullName)