[RESOLVED] Process multiple Files
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:
Private Sub Command1_Click()
Dim FSys As New Scripting.FileSystemObject
Dim entries As Variant
Dim dir_name As String
Dim i As Integer
On Error Resume Next
CommonDialog1.ShowOpen
If Err.Number = cdlCancel Then
Exit Sub
ElseIf Err.Number <> 0 Then
MsgBox "Error " & Format$(Err.Number) & _
" selecting files." & vbCrLf & Err.Description
Exit Sub
End If
List1.Clear
'entries = Split(CommonDialog1.FileName, vbNullChar)
entries = Split(CommonDialog1.filename, vbNullChar)
' See if there is more than one file.
If UBound(entries, 1) = LBound(entries, 1) Then
' There is only one file name.
List1.AddItem entries(LBound(entries, 1))
Else
' Get the directory name.
dir_name = entries(LBound(entries, 1))
If Right$(dir_name, 1) <> "\" Then dir_name = dir_name & "\"
' Get the file names.
For i = LBound(entries, 1) + 1 To UBound(entries, 1)
List1.AddItem dir_name & entries(i)
Next i
End If
If List1.ListCount = 0 Then GoTo ext
For X = 0 To (List1.ListCount - 1)
List1.ListIndex = X
Next
FSys.GetAbsolutePathName (List1.Text)
If (Len(List1.Text) > 9) Then
Dim shortname As String ' receives short-filename equivalent
Dim slength As Long ' receives length of short-filename equivalent
' Make room in the buffer to receive the 8.3 form of the filename.
shortname = Space(256)
' Get the 8.3 form of the filename specified.
'the file must exist for the api to do it's stuff
slength = GetShortPathName(List1.Text, shortname, 256)
' Remove the trailing null and display the result.
shortname = Left(shortname, slength)
MsgBox shortname, vbInformation, "shortname"
Text1.Text = shortname
Open "C:\fb\testbatch.bat" For Output As #1
Print #1, "c:\hd\psmd.exe" & Space(1) & Text1.Text & Space(1) & ">" & Space(1) & "C:\hB\test.Txt"
Close #1
Shell ("cmd /c C:\fb\testbatch.bat")
End if
End Sub
Thx !
Re: Process multiple Files
lokiloki2,
What is the purpose of this code:
VB Code:
For X = 0 To (List1.ListCount - 1)
List1.ListIndex = X
Next
Re: Process multiple Files
VB Code:
If List1.ListCount = 0 Then GoTo ext
There doesn't appear to be an "ext" label either.
chem
Re: Process multiple Files
This allows you to select multiple filenames to open directly into the CDC.
VB Code:
Private Sub cmdOpen_Click()
' CancelError is True.
On Error GoTo ErrHandler
' Set Flags
CommonDialog1.Flags = cdlOFNAllowMultiselect Or cdlOFNLongNames
' Set filters.
CommonDialog1.Filter = "All Files (*.*)|*.*|Text" & _
"Files (*.txt)|*.txt|Batch Files (*.bat)|*.bat"
' Specify default filter.
CommonDialog1.FilterIndex = 2
' Display the Open dialog box.
CommonDialog1.ShowOpen
' Call the open file procedure.
' OpenFile (CommonDialog1.FileName)
Exit Sub
ErrHandler:
' User pressed Cancel button.
Exit Sub
End Sub
Re: Process multiple Files
Should not have been in the code. My bad.
If List1.ListCount = 0 Then GoTo ext
Re: Process multiple Files
Dglienna,
The code you posted, will that run the .exe on each file ?
Re: Process multiple Files
VB Code:
For X = 0 To (List1.ListCount - 1)
List1.ListIndex = X
Next
Doesn't belong, was testing out some code and forgot to delete this.
Re: Process multiple Files
Quote:
Originally Posted by lokiloki2
Dglienna,
The code you posted, will that run the .exe on each file ?
I'm not sure. I think it will open each one in a separate instance. I know that's the case with Word, for instance. I've never really tried to use it.
Re: Process multiple Files
This will not work.
The exe I run on each file listed in the Listbox produces a checksum that I need to save to a textfile. If multiple files are selected in the CommonDialog, only the last line in the listbox gets processed. I need to process each file if someone selects more than one file.
THX