• CmnDialog

    Author
    Topic
    #464833

    Guys
    With the CmnDialog script as below, when the user selects multiple files, just the filenames are captured ( which is what I need ), but
    when a single file is selected, the full path is captured.
    Can the lines:
    Case 0 ‘if only one was selected we are done
    lstFTP.AddItem myFiles(0)

    be changed to just capture the fileame?
    Many thanks.

    Private Sub procAccTrans_Click(Index As Integer)
    On Error GoTo cError

    Dim i As Integer
    Dim myFiles() As String
    Dim myPath As String

    With CmnDialog1
    .MaxFileSize = 32000 ‘this will max out the buffer for the filenames array for large selections. *NEW*
    .CancelError = False ‘if cancel is pressed, the code jumps to cError because of the On Error statement above

    .Filter = “*.*”
    .FilterIndex = 2
    .InitDir = “L:MMPDFTransfer”
    .Flags = CD_FLAGS ‘this is where we tell it to use multiselect
    .ShowOpen

    myFiles = Split(.FileName, vbNullChar) ‘the Filename returned is delimeted by a null character because we selected the cdlOFNLongNames flag

    Select Case UBound(myFiles)
    Case 0 ‘if only one was selected we are done
    lstFTP.AddItem myFiles(0)

    Case Is > 0 ‘if more than one, we need to loop through it and append the root directory
    For i = 1 To UBound(myFiles)
    myPath = myFiles(0) & IIf(Right(myFiles(0), 1) “”, “”, “”) & myFiles(i)
    lstFTP.AddItem myFiles(i)
    Next i
    End Select

    End With
    Exit Sub

    cError:
    Beep
    MsgBox Err.Description ‘*NEW*
    End Sub

    Viewing 2 reply threads
    Author
    Replies
    • #1191688

      Try

      Code:
            Case 0   'if only one was selected we are done
                  Dim strFile As String
                  Dim intPos As Integer
                  ' Get filename with path
                  strFile = myFiles(0)
                  ' Get position of last backslash
                  intPos = InStrRev(strFile, "")
                  ' Extract part after last backslash, i.e. the filename
                  strFile = Mid(strFile, intPos + 1)
                  ' Add the filename
                  lstFTP.AddItem strFile
    • #1191689

      As ever !! perfect !! Thanks again…

    • #1191715

      Hi,

      I don,t have the examples in VB, but use this in C#

      DirectoryInfo dir = new DirectoryInfo(“C:\”);
      FileInfo[] files = dir.GetFiles(“*.*”,SearchOption.TopDirectoryOnly);
      foreach (FileInfo file in files)
      {

      Console.WriteLine(file.Name);
      }

      Ben

    Viewing 2 reply threads
    Reply To: CmnDialog

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: