In this code, I have a list of files through a CommonDialog into a Listbox.

The user can select multiple files. If only one file is selected, works. If two or more are selected, Output only writes the last path/file line in the list. How can I get the Output to write, creating a bat file, with all path/files in the listbox ?

I tried to loop through the files and process each file in the listbox without running Shell but could never get it to work correctly.

VB Code:
  1. Private Sub Command1_Click()
  2. Dim FSys As New Scripting.FileSystemObject
  3.  
  4.  
  5. Dim entries As Variant
  6. Dim dir_name As String
  7. Dim i As Integer
  8.  
  9.     On Error Resume Next
  10.     CommonDialog1.ShowOpen
  11.     If Err.Number = cdlCancel Then
  12.         Exit Sub
  13.     ElseIf Err.Number <> 0 Then
  14.         MsgBox "Error " & Format$(Err.Number) & _
  15.             " selecting files." & vbCrLf & Err.Description
  16.             Exit Sub
  17.     End If
  18. List1.Clear
  19.     'entries = Split(CommonDialog1.FileName, vbNullChar)
  20.     entries = Split(CommonDialog1.filename, vbNullChar)
  21.  
  22.     ' See if there is more than one file.
  23.     If UBound(entries, 1) = LBound(entries, 1) Then
  24.         ' There is only one file name.
  25.         List1.AddItem entries(LBound(entries, 1))
  26.        
  27.     Else
  28.         ' Get the directory name.
  29.         dir_name = entries(LBound(entries, 1))
  30.         If Right$(dir_name, 1) <> "\" Then dir_name = dir_name & "\"
  31.  
  32.         ' Get the file names.
  33.         For i = LBound(entries, 1) + 1 To UBound(entries, 1)
  34.            
  35.             List1.AddItem dir_name & entries(i)
  36.         Next i
  37.     End If
  38.  
  39. If List1.ListCount = 0 Then GoTo ext
  40.  
  41. For X = 0 To (List1.ListCount - 1)
  42.  
  43. List1.ListIndex = X
  44. Next
  45.  
  46. FSys.GetAbsolutePathName (List1.Text)
  47. If (Len(List1.Text) > 9) Then
  48.         Dim shortname As String  ' receives short-filename equivalent
  49.         Dim slength As Long  ' receives length of short-filename equivalent
  50.  
  51.   ' Make room in the buffer to receive the 8.3 form of the filename.
  52.         shortname = Space(256)
  53.  
  54.     ' Get the 8.3 form of the filename specified.
  55.     'the file must exist for the api to do it's stuff
  56.  
  57.         slength = GetShortPathName(List1.Text, shortname, 256)
  58.     ' Remove the trailing null and display the result.
  59.  
  60.         shortname = Left(shortname, slength)
  61.       MsgBox shortname, vbInformation, "shortname"
  62.      Text1.Text = shortname
  63.  
  64.      Open "C:\fb\testbatch.bat" For Output As #1
  65.     Print #1, "c:\hd\psmd.exe" & Space(1) & Text1.Text & Space(1) & ">" & Space(1) & "C:\hB\test.Txt"
  66.            
  67. Close #1
  68.  
  69. Shell ("cmd /c C:\fb\testbatch.bat")
  70.  
  71. End if
  72. End Sub
Thx !