Link to home
Start Free TrialLog in
Avatar of prosit
prositFlag for United States of America

asked on

Create an array of files from commondialog showopen.

I have chosen some files with the commondialog showopen thingy, and have them stored in the CommonDialog1.filename but now I would like them in in array split up, they are all on one line after each other.

HELP!

Tax
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Using VB6? Use the Split function.
Avatar of Vbmaster
Vbmaster

Using VB5? Use something like this..

Function GetRows(TextStr As String, ByRef Rad() As String, ChrLine As String, Optional MaxCount As Integer = 0, Optional RemoveEmptyLines As Boolean) As Long

  Dim RadNr As Long
  Dim TeckenNr As Long
  Dim Tecken As String
 
  If (Len(TextStr) = 0) Then
    ReDim Rad(0)
    GetRows = 0
    Exit Function
  End If
 
  ReDim Rad(10)
  RadNr = 1
  TeckenNr = 0
  TextStr = TextStr
 
  Do Until (TeckenNr >= Len(TextStr))
    TeckenNr = TeckenNr + 1
    Tecken = Mid$(TextStr, TeckenNr, 1)
    If (Tecken = ChrLine) Then
      If (RadNr Mod 10 = 0) Then ReDim Preserve Rad(RadNr + 10)
      RadNr = RadNr + 1
      If (ChrLine = Chr$(13)) Then TeckenNr = TeckenNr + 1
    Else
      If (RadNr = MaxCount) Then
        Rad(RadNr) = Mid$(TextStr, TeckenNr)
        Exit Do
      Else
        Rad(RadNr) = Rad(RadNr) + Tecken
      End If
    End If
  Loop
 
  'Ta bort alla tomma rader
  If RemoveEmptyLines Then
    For RadNr = RadNr To 1 Step -1
      If (Len(Rad(RadNr)) <> 0) Then Exit For
    Next
  End If
  ReDim Preserve Rad(RadNr)
 
  GetRows = RadNr
 
End Function
Forget the function above, this one is much nicer..

Function GetRows2(Text As String, Rad() As String, Divider As String) As Long
 
  Dim RadNr As Long
  Dim TeckenNr As Long
  Dim LenDivider As Long
  Dim OldTeckenNr As Long
 
  RadNr = 0
  OldTeckenNr = 1
  TeckenNr = InStr(Text, Divider)
  LenDivider = Len(Divider)
 
  Do Until (TeckenNr = 0)
    RadNr = RadNr + 1
    ReDim Preserve Rad(RadNr - 1)
    Rad(RadNr - 1) = Mid$(Text, OldTeckenNr, TeckenNr - OldTeckenNr)
    OldTeckenNr = TeckenNr + LenDivider
    TeckenNr = InStr(OldTeckenNr, Text, Divider)
  Loop
 
  If (OldTeckenNr <= Len(Text)) Then
    RadNr = RadNr + 1
    ReDim Preserve Rad(RadNr - 1)
    Rad(RadNr - 1) = Mid$(Text, OldTeckenNr)
  End If
 
  GetRows2 = RadNr
 
End Function

Avatar of prosit

ASKER

It's 6 :)

I'll look at split thanks

T
Avatar of prosit

ASKER

Alright, got the split working, but what's the delimiter in the showopen?  It gives the first "G:\My" the second "documents" and so forth, so I need to decide what's splitting them up.

Any ideas?

J
ASKER CERTIFIED SOLUTION
Avatar of Vbmaster
Vbmaster

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 prosit

ASKER

What I'm doing now is:

Dim Names() As String

Private Sub Command1_Click()
    With CommonDialog1
        If .CancelError = True Then Exit Sub
        .Flags = cdlOFNHideReadOnly + cdlOFNAllowMultiselect _
                   + cdlOFNExplorer
        .Filter = "Text Files (*.Txt)|*.Txt|"
        .FilterIndex = 1
        .ShowOpen
    End With
    Names = Split(CommonDialog1.FileName)
End Sub

Names = all files on one line, I don't know what delimiter it uses.

The help says it's spaces, but that does not work???

J
Avatar of prosit

ASKER

VB Master you are the man!

Thanks all!

J
Why using the commondialog-show and splitting afterwards?

I use my own filelist-box, giving control about the way the user can select (single-, multiple-, etc) the files and I have no split problem. Just a test whether ot not an item is selected.
Why using the commondialog-show and splitting afterwards?

I use my own filelist-box, giving control about the way the user can select (single-, multiple-, etc) the files and I have no split problem. Just a test whether ot not an item is selected.
nico5038: because it looks better, and the user is probably more used to the standard Win9x-look of the dialog. ;)
Vbmaster: True for the standard windows user. However a multiselect is not often used by average users. I myself used the listbox solution to perform manipulations on the selected files, thus having the possibility to show the progress of the application by scrolling the filelist to the currently processing file.:)
Avatar of prosit

ASKER

nico5038:

Can I take a look at it?

J
Funch:

Create your own form with the following components:

CommandButton    btnSelectAll
CommonDialog     CommonDialog1
VB.ComboBox      Combo1
VB.DirListBox    Dir1
VB.DriveListBox  Drive1
VB.FileListBox   File1

Add this code behind the form:

Option Explicit
'
' Sample program for "controlled" file selection
'
Private Sub Combo1_Change()
File1.Pattern = Combo1.Text
File1.Refresh
End Sub

Private Sub Combo1_Click()
' Put itemdata in file filter
File1.Pattern = Left(Combo1.List(Combo1.ListIndex), 5)
File1.Refresh
End Sub

Private Sub Drive1_Change()
On Error GoTo Error_Drive1_Change
   
' Set directory path.
Dir1.Path = Drive1.Drive
Error_Drive1_Change:
' Check for errors like "no floppy"
If Err = 68 Then
    MsgBox "This drive is not available"
End If
End Sub

Private Sub Dir1_Change()
    File1.Path = Dir1.Path  ' Set file path.
End Sub
Private Sub Form_Activate()
Form_Load
End Sub

Private Sub Form_Load()
Dim Entry As String          ' Declare variables.
On Error GoTo errorhandling
' Init combobox
    Combo1.Clear
' Fill listbox with default selection
    Combo1.AddItem "*.*     - All files"
    Combo1.AddItem "*.txt   - Text files"
    Combo1.AddItem "*.dll   - Modules  "
    Combo1.ListIndex = 0
errorhandling:
    Select Case Err
    Case 0
        ' No action
    Case Else
        MsgBox "Error: " & Err & " Beschrijving: " & Err.Description
    End Select
End Sub

Private Sub btnSelectAll_Click()
Dim i As Integer
' Toggle between select all and deselect all
' This processing can also be used to reset file under process
If btnSelectAll.Caption = "&Select All" Then
    btnSelectAll.Caption = "&DeSelect All"
    For i = 0 To File1.ListCount - 1    ' Loop through entire list.
        File1.Selected(i) = True        ' Set property of entry
    Next i
    ' This statement is used to reset the listbox to show the first entry
    ' When set to comment, the 'set selected' will scroll the file1 filelistbox
    ' to the bottom
    File1.TopIndex = 0
Else
    btnSelectAll.Caption = "&Select All"
    For i = 0 To File1.ListCount - 1
        File1.Selected(i) = False
    Next i
End If
End Sub

The btnSelect gives one example of processing the filelistbox, but you can ofcourse 'invert' the setting of the entry to the testing of the value set by the user!

Success
Avatar of prosit

ASKER

The selectall does not work in this example, but I get the point.

Thanks

t