Enhancement to handle multiple files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SAIRAAM
    New Member
    • Feb 2007
    • 45

    Enhancement to handle multiple files

    hi all

    i am right now examing one of the project thats avaliable.in which they have taken single log file as input. the code for it is given below.

    Code:
    Private Sub cmdInputBrowse_Click()
       On Error GoTo ErrorLine
       Dim fname As String
       Dim fso As New FileSystemObject
       Dim ext, newext As Variant
       Dim stat, i As Integer
       stat = 0
       
        If txtInputPath.Text <> "" Then
            If fso.FileExists(txtInputPath.Text) Then
                fname = Split(txtInputPath.Text, Mid(txtInputPath.Text, InStrRev(txtInputPath.Text, "\") + 1))(0)
            ElseIf fso.FolderExists(txtInputPath.Text) Then
                fname = txtInputPath.Text
            Else
                fname = Split(txtInputPath.Text, Mid(txtInputPath.Text, InStrRev(txtInputPath.Text, "\") + 1))(0)
                
                If InStr(1, txtInputPath.Text, ".") > 0 Then
                    ext = "*." & Split(Mid(txtInputPath.Text, InStrRev(txtInputPath.Text, "\") + 1), ".")(1)
                Else
                    ext = "*.csv"
                End If
                MsgBox ext
                stat = 1
            End If
            
            If Not fso.FolderExists(fname) Then
                MsgBox "Folder specified does not exist", , "Input Log File"
                txtInputPath.Text = ""
                Exit Sub
            End If
            
            CDBrowseFile.InitDir = fname
        Else
            CDBrowseFile.InitDir = App.Path
        End If
        
        CDBrowseFile.Filter = "*.csv|*.csv;" & Chr(124) & "*.log|*.log;" & Chr(124) & "*.txt|*.txt" & Chr(124) & "*.*|*.*"
      
        
        
        CDBrowseFile.DialogTitle = "Select the Log File"
        
        
        If stat = 0 Then
            CDBrowseFile.FilterIndex = 1
        Else
            If ext = "*.csv" Then
                CDBrowseFile.FilterIndex = 1
            ElseIf ext = "*.log" Then
                CDBrowseFile.FilterIndex = 2
            ElseIf ext = "*.txt" Then
                CDBrowseFile.FilterIndex = 3
            Else
                CDBrowseFile.FilterIndex = 4
            End If
        End If
        CDBrowseFile.ShowOpen
        If CDBrowseFile.FileName <> "" Then
            If fso.FileExists(CDBrowseFile.FileName) = True Then
                txtInputPath.Text = CDBrowseFile.FileName
                gstrInputFile = Mid(txtInputPath.Text, InStrRev(txtInputPath.Text, "\") + 1)
                CmdCompute.Enabled = True
                CDBrowseFile.FileName = ""
            Else
                MsgBox "File does not exist", vbCritical, "Input File"
            End If
        End If
       If txtInputPath.Text <> "" Then
        TxtOutputPath.Text = Mid(txtInputPath.Text, 1, InStrRev(txtInputPath.Text, "\") - 1)
        End If
        
        If TxtInputTimerFile.Text = "" Then
            TxtInputTimerFile.Text = Mid(txtInputPath.Text, 1, InStrRev(txtInputPath.Text, "\") - 1) & "\"
        End If
        Exit Sub
    
    ErrorLine:
        'MsgBox "Error-Number:" & Err.Number & vbCrLf & "Error Description:" & Err.DESCRIPTION & vbCrLf & "Error At: Selecting input file"
    End Sub
    the extensions that are specified here are log files extensions.... can i enhance this code to take in multiple log files as input...if i can then where should i edit the existing code to take multiple files..

    can anyone guide me in this

    awating a reply

    thanks in advance for whatever help i get

    thanks
    sairaam
  • iburyak
    Recognized Expert Top Contributor
    • Nov 2006
    • 1016

    #2
    Code:
    1. CDBrowseFile.Filter = "*.csv|*.csv;" & Chr(124) & "*.log|*.log;" & Chr(124) & "*.txt|*.txt" & Chr(124) & "*.*|*.*"
      
        
        
    2.    CDBrowseFile.DialogTitle = "Select the Log File"
        
        
    3.    If stat = 0 Then
    4.        CDBrowseFile.FilterIndex = 1
    Line one in code above controls which file extensions to show.
    You can change file index in line 4 to a different number like.

    Code:
    CDBrowseFile.FilterIndex = 3
    Good Luck.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      One thing I would recommend is to move the browsing code into a separate Sub, rather than having it hanging off a button like this. Once you encapsulate it as a self-contained routine, it becomes much simpler to reuse it or change the way that you call it.

      Comment

      • SAIRAAM
        New Member
        • Feb 2007
        • 45

        #4
        Originally posted by iburyak
        Code:
        1. CDBrowseFile.Filter = "*.csv|*.csv;" & Chr(124) & "*.log|*.log;" & Chr(124) & "*.txt|*.txt" & Chr(124) & "*.*|*.*"
          
            
            
        2.    CDBrowseFile.DialogTitle = "Select the Log File"
            
            
        3.    If stat = 0 Then
        4.        CDBrowseFile.FilterIndex = 1
        Line one in code above controls which file extensions to show.
        You can change file index in line 4 to a different number like.

        Code:
        CDBrowseFile.FilterIndex = 3
        Good Luck.
        hi


        i had changed the file index as you had specified.... but now also am not able to select multiple files... the selection here i mean selecting more than 1 file at a stroke....

        can u help me

        thanks
        sairaam

        Comment

        • SAIRAAM
          New Member
          • Feb 2007
          • 45

          #5
          Originally posted by Killer42
          One thing I would recommend is to move the browsing code into a separate Sub, rather than having it hanging off a button like this. Once you encapsulate it as a self-contained routine, it becomes much simpler to reuse it or change the way that you call it.
          sir am not allowed to change the existing code entirely... can just modify it so cant move into a sub since the existing buutton system should be present....can u suggest me some other idea

          thanks for suggestion

          thanks
          sairaam

          Comment

          • iburyak
            Recognized Expert Top Contributor
            • Nov 2006
            • 1016

            #6
            Sorry, at first I didn't really understand your question correctly.

            Make your textbox MultiLine = True

            Use code below.
            Code:
            Private Sub Command1_Click()
             On Error GoTo ErrorLine
               Dim fname As String
               Dim fso As New FileSystemObject
               Dim ext, newext As Variant
               Dim stat, i As Integer
               stat = 0
               
                If txtInputPath.Text <> "" Then
                    If fso.FileExists(txtInputPath.Text) Then
                        fname = Split(txtInputPath.Text, Mid(txtInputPath.Text, InStrRev(txtInputPath.Text, "\") + 1))(0)
                    ElseIf fso.FolderExists(txtInputPath.Text) Then
                        fname = txtInputPath.Text
                    Else
                        fname = Split(txtInputPath.Text, Mid(txtInputPath.Text, InStrRev(txtInputPath.Text, "\") + 1))(0)
                        
                        If InStr(1, txtInputPath.Text, ".") > 0 Then
                            ext = "*." & Split(Mid(txtInputPath.Text, InStrRev(txtInputPath.Text, "\") + 1), ".")(1)
                        Else
                            ext = "*.csv"
                        End If
                        MsgBox ext
                        stat = 1
                    End If
                    
                    If Not fso.FolderExists(fname) Then
                        MsgBox "Folder specified does not exist", , "Input Log File"
                        txtInputPath.Text = ""
                        Exit Sub
                    End If
                    
                    CDBrowseFile.InitDir = fname
                Else
                    CDBrowseFile.InitDir = App.Path
                End If
                
                CDBrowseFile.Filter = "*.csv|*.csv;" & Chr(124) & "*.log|*.log;" & Chr(124) & "*.txt|*.txt" & Chr(124) & "*.*|*.*"
                CDBrowseFile.Flags = cdlOFNAllowMultiselect
                
                
                CDBrowseFile.DialogTitle = "Select the Log File"
                
                
                If stat = 0 Then
                    CDBrowseFile.FilterIndex = 1
                Else
                    If ext = "*.csv" Then
                        CDBrowseFile.FilterIndex = 1
                    ElseIf ext = "*.log" Then
                        CDBrowseFile.FilterIndex = 2
                    ElseIf ext = "*.txt" Then
                        CDBrowseFile.FilterIndex = 3
                    Else
                        CDBrowseFile.FilterIndex = 4
                    End If
                End If
               
                
                CDBrowseFile.ShowOpen
                If CDBrowseFile.FileName <> "" Then
                    Dim retFiles, j As Integer
                    retFiles = Split(CDBrowseFile.FileName, " ")
                    If UBound(retFiles) = 0 Then
                        txtInputPath.Text = retFiles(0)
                    Else
                        For j = 1 To UBound(retFiles)
                            txtInputPath.Text = txtInputPath.Text & vbCrLf & retFiles(0) & "\" & retFiles(j)
                        Next
                    End If
            '        If fso.FileExists(CDBrowseFile.FileName) = True Then
            '            txtInputPath.Text = CDBrowseFile.FileName
            '            gstrInputFile = Mid(txtInputPath.Text, InStrRev(txtInputPath.Text, "\") + 1)
            '            CmdCompute.Enabled = True
            '            CDBrowseFile.FileName = ""
            '        Else
            '            MsgBox "File does not exist", vbCritical, "Input File"
            '        End If
                End If
                
               If txtInputPath.Text <> "" Then
                TxtOutputPath.Text = Mid(txtInputPath.Text, 1, InStrRev(txtInputPath.Text, "\") - 1)
                End If
                
                If TxtInputTimerFile.Text = "" Then
                    TxtInputTimerFile.Text = Mid(txtInputPath.Text, 1, InStrRev(txtInputPath.Text, "\") - 1) & "\"
                End If
                Exit Sub
            
            ErrorLine:
                'MsgBox "Error-Number:" & Err.Number & vbCrLf & "Error Description:" & Err.DESCRIPTION & vbCrLf & "Error At: Selecting input file"
            End Sub
            I commented out some of your code because wasn't sure how you want to change it with multiple files:

            [PHP]
            ' If fso.FileExists( CDBrowseFile.Fi leName) = True Then
            ' txtInputPath.Te xt = CDBrowseFile.Fi leName
            ' gstrInputFile = Mid(txtInputPat h.Text, InStrRev(txtInp utPath.Text, "\") + 1)
            ' CmdCompute.Enab led = True
            ' CDBrowseFile.Fi leName = ""
            ' Else
            ' MsgBox "File does not exist", vbCritical, "Input File"
            ' End If[/PHP]

            Good Luck.

            Comment

            • SAIRAAM
              New Member
              • Feb 2007
              • 45

              #7
              Originally posted by iburyak
              Sorry, at first I didn't really understand your question correctly.

              Make your textbox MultiLine = True

              Use code below.
              Code:
              Private Sub Command1_Click()
               On Error GoTo ErrorLine
                 Dim fname As String
                 Dim fso As New FileSystemObject
                 Dim ext, newext As Variant
                 Dim stat, i As Integer
                 stat = 0
                 
                  If txtInputPath.Text <> "" Then
                      If fso.FileExists(txtInputPath.Text) Then
                          fname = Split(txtInputPath.Text, Mid(txtInputPath.Text, InStrRev(txtInputPath.Text, "\") + 1))(0)
                      ElseIf fso.FolderExists(txtInputPath.Text) Then
                          fname = txtInputPath.Text
                      Else
                          fname = Split(txtInputPath.Text, Mid(txtInputPath.Text, InStrRev(txtInputPath.Text, "\") + 1))(0)
                          
                          If InStr(1, txtInputPath.Text, ".") > 0 Then
                              ext = "*." & Split(Mid(txtInputPath.Text, InStrRev(txtInputPath.Text, "\") + 1), ".")(1)
                          Else
                              ext = "*.csv"
                          End If
                          MsgBox ext
                          stat = 1
                      End If
                      
                      If Not fso.FolderExists(fname) Then
                          MsgBox "Folder specified does not exist", , "Input Log File"
                          txtInputPath.Text = ""
                          Exit Sub
                      End If
                      
                      CDBrowseFile.InitDir = fname
                  Else
                      CDBrowseFile.InitDir = App.Path
                  End If
                  
                  CDBrowseFile.Filter = "*.csv|*.csv;" & Chr(124) & "*.log|*.log;" & Chr(124) & "*.txt|*.txt" & Chr(124) & "*.*|*.*"
                  CDBrowseFile.Flags = cdlOFNAllowMultiselect
                  
                  
                  CDBrowseFile.DialogTitle = "Select the Log File"
                  
                  
                  If stat = 0 Then
                      CDBrowseFile.FilterIndex = 1
                  Else
                      If ext = "*.csv" Then
                          CDBrowseFile.FilterIndex = 1
                      ElseIf ext = "*.log" Then
                          CDBrowseFile.FilterIndex = 2
                      ElseIf ext = "*.txt" Then
                          CDBrowseFile.FilterIndex = 3
                      Else
                          CDBrowseFile.FilterIndex = 4
                      End If
                  End If
                 
                  
                  CDBrowseFile.ShowOpen
                  If CDBrowseFile.FileName <> "" Then
                      Dim retFiles, j As Integer
                      retFiles = Split(CDBrowseFile.FileName, " ")
                      If UBound(retFiles) = 0 Then
                          txtInputPath.Text = retFiles(0)
                      Else
                          For j = 1 To UBound(retFiles)
                              txtInputPath.Text = txtInputPath.Text & vbCrLf & retFiles(0) & "\" & retFiles(j)
                          Next
                      End If
              '        If fso.FileExists(CDBrowseFile.FileName) = True Then
              '            txtInputPath.Text = CDBrowseFile.FileName
              '            gstrInputFile = Mid(txtInputPath.Text, InStrRev(txtInputPath.Text, "\") + 1)
              '            CmdCompute.Enabled = True
              '            CDBrowseFile.FileName = ""
              '        Else
              '            MsgBox "File does not exist", vbCritical, "Input File"
              '        End If
                  End If
                  
                 If txtInputPath.Text <> "" Then
                  TxtOutputPath.Text = Mid(txtInputPath.Text, 1, InStrRev(txtInputPath.Text, "\") - 1)
                  End If
                  
                  If TxtInputTimerFile.Text = "" Then
                      TxtInputTimerFile.Text = Mid(txtInputPath.Text, 1, InStrRev(txtInputPath.Text, "\") - 1) & "\"
                  End If
                  Exit Sub
              
              ErrorLine:
                  'MsgBox "Error-Number:" & Err.Number & vbCrLf & "Error Description:" & Err.DESCRIPTION & vbCrLf & "Error At: Selecting input file"
              End Sub
              I commented out some of your code because wasn't sure how you want to change it with multiple files:

              [PHP]
              ' If fso.FileExists( CDBrowseFile.Fi leName) = True Then
              ' txtInputPath.Te xt = CDBrowseFile.Fi leName
              ' gstrInputFile = Mid(txtInputPat h.Text, InStrRev(txtInp utPath.Text, "\") + 1)
              ' CmdCompute.Enab led = True
              ' CDBrowseFile.Fi leName = ""
              ' Else
              ' MsgBox "File does not exist", vbCritical, "Input File"
              ' End If[/PHP]

              Good Luck.

              am really thankful for the guidence that you are doing... here am able to select muliple files but am not able to display them on to the input path...i set that text box multiline property to true also....


              can i have any suggessions for it..

              thanks
              sairaam

              Comment

              • SAIRAAM
                New Member
                • Feb 2007
                • 45

                #8
                am in urgent need please can some one help me out...to solve the problem which i have mentioned earlier...


                thanks
                sairaam

                Comment

                • sumaiya
                  New Member
                  • Apr 2007
                  • 43

                  #9
                  Yeh that is one thing i wanted to know as well

                  Comment

                  Working...