Splitting a string of multiple filenames
I had a question that stumped me.
One of my apps allows users to use 'open with' to open a file in it. It works great with single files. With multiple files, however, this is where the problem starts.
With one file it works like this:
1. Get file name in command string (e.g. "c:\file.txt")
2. Remove quotes (e.g. c:\file.txt)
3. Process file info (time, date, size, etc...)
4. Put it in ListView
But with more than one file, it crashes:
1. Get file names in command string (e.g. "c:\file.txt" "c:\file2.txt")
2. Remove quotes (e.g. c:\file.txt c:\file2.txt)
3. Process file info-ONLY DOES THE FIRST FILE
4. Put it in ListView-SOMETIMES DOES THE FIRST FILE
Is there a way to split the multiple file strings (maybe use " " (thats quotespacequote) as delimiter) and create a loop?
If you want to see the source code for how it works now, download this file:
http://prdownloads.sourceforge.net/inferno/Source_Inferno12B_Redist.zip?download (VB6 Code)
Thanks in advance for the help.
[1079 byte] By [
yudi] at [2007-11-11 10:04:13]

# 1 Re: Splitting a string of multiple filenames
where u have problem exactly ? I drag multi files and no problem occure explain more or tell me the procedure you want to update .
Amahdy at 2007-11-11 17:23:15 >

# 2 Re: Splitting a string of multiple filenames
Interesting!, You should do this first change all quote space quote marks to a comma then remove and extra quotes and then use the split function to seperate out the files.
Something like this:
Dim Files() As String
cmdString = Replace(cmdString, """ """, ",") 'conver to " " to ,
cmdString = Replace(cmdString, """", "") 'Remove extra quotes
Files = Split(cmdString, ",")
For I = 0 to UBound(Files)
Debug.Print Files(I)
Next I
...
Erase Files
This way if you have files like "C:\My Folder\My Data.txt" "C:\My Folder\My Stuff.txt"
and you can still get the file names.
# 3 Re: Splitting a string of multiple filenames
You may also find this function helpful: http://www.dev-archive.com/vb2themax/Tip/19083
# 4 Re: Splitting a string of multiple filenames
Actually Phil's link is a better function I tested it, and it works well. It even allows a seperator character imbeded with in a quoted item, as well as a quote within a non quoted item and it still splits the string correctly.
# 5 Re: Splitting a string of multiple filenames
Could you give an example of how to use Phil's function? I tried and couldn't get it to work.
I'll try the other one next.
yudi at 2007-11-11 17:27:19 >

# 6 Re: Splitting a string of multiple filenames
Here is a little test sub that uses the SplitQuoted() function
Sub SplitTest()
Dim I As Long
Dim Buf As String
Dim Fls() As String
'set buf to test string of "c:\file.txt" "c:\file2.txt"
Buf = """c:\file.txt"" ""c:\file2.txt"""
Fls = SplitQuoted(Buf, " ")
For I = 0 To UBound(Fls)
Debug.Print Fls(I)
Next I
Erase Fls
' Debug Window Results:
' SplitTest
' c:\file.txt
' c:\file2.txt
End Sub
# 7 Re: Splitting a string of multiple filenames
I finally just used this:
Dim Files() As String
Dim I As Integer
Files = Split(Command, Chr(34) & Chr(32) & Chr(34)) 'Quote, Space, Quote
For I = 0 To UBound(Files)
RemoveQuotes(Files(I))
Debug.Print Files(I)
Next I
It seems to work pretty well.
yudi at 2007-11-11 17:29:21 >

# 8 Re: Splitting a string of multiple filenames
I'm trying to do the same thing, only I'm not getting the full string of file names in Command$. If I select multiple files in Explorer, then do "Open With" and select my app, my app only sees the last file selected. I'm using Windows 2000 and VB6. To keep things simple for now, I just added:
msgbox Command$
in my form's onload event.
Any ideas what's going on here?
# 9 Re: Splitting a string of multiple filenames
That is strange. Does it do the same thing if you remove the $ from Command?
yudi at 2007-11-11 17:31:30 >

# 10 Re: Splitting a string of multiple filenames
No difference with $ or without finally it's a $tring :
http://i14.tinypic.com/43ho30y.jpg
Amahdy at 2007-11-11 17:32:30 >

# 11 Re: Splitting a string of multiple filenames
Your problem is the Open With option. It only sends one file to the command line. If you drag and drop the files or Use Send To, then you will get all of the files in the command line.
# 12 Re: Splitting a string of multiple filenames
That should work, but you might want to try this:
Private Sub Form_Load()
Dim Files() As String
Dim I As Integer
Files = Split(Command, Chr(34) & Chr(32) & Chr(34))
For I = 0 To UBound(Files)
MsgBox Files(I)
Next I
End Sub
That will split the file names and output each one in a message box.
yudi at 2007-11-11 17:34:26 >

# 13 Re: Splitting a string of multiple filenames
Actually that routine may not always work because you don't always get quotes arround your file names. For example:
Folder: C:\textfiles\
tf1.txt
tf 2.txt
tf3.txt
tf 4.txt
Would return a command line of:
C:\textfiles\tf1.txt "C:\textfiles\tf 2.txt" C:\textfiles\tf3.txt "C:\textfiles\tf 4.txt"
As you see only the filename that have spaces in them have quotes around them. So using the SplitQuoted() function referenced by the link in Phil's post would work better than the streight Split. Here is an example with the SplitQuoted Function cleaned up just a bit. It was hardcoding the number of elements in the array to be 100. So: Sub SplitTest()
Dim Files() As String
Dim I As Long
Files = SplitQuoted(Command, " ")
For I = LBound(Files) To UBound(Files)
Debug.Print Files(I)
Next I
End Sub
'SplitQuoted - A split variant that deals correctly with quoted elements
' split a string, dealing correctly with quoted items
'
' TEXT is the string to be split
' SEPARATOR is the separator char (default is comma)
' QUOTES is the character used to quote strings (default is """",
' the double quote)
' you can also use a character pair (eg "{}") if the opening
' and closing quotes are different
'
' for example you can split the following string
' arr() = SplitQuoted("[one,two],three,[four,five]", , "[]")
' into 3 items, because commas inside []
' are not taken into account
' UPDATE: Thanks to Kevan Brown, Chief Technology Officer, CommercExec, Inc.
' for spotting an error that made the function fail under certain conditions
Function SplitQuoted(ByVal Text As String, Optional ByVal Separator As String = ",", Optional ByVal Quotes As String = """") As String()
Dim res() As String
Dim resCount As Long
Dim index As Long
Dim startIndex As Long
Dim endIndex As Long
Dim length As Long
Dim sepCode As Integer
Dim quoteCode As Integer
length = Len(Text)
res = Split("")
' a null string is a special case
' return the same uninitialized error that Split would return
If length = 0 Then
SplitQuoted = Split("")
Exit Function
End If
' integer ASCII codes of separators
sepCode = Asc(Separator)
quoteCode = Asc(Quotes)
startIndex = 1
Do While index < length
index = index + 1
Select Case Asc(Mid(Text, index, 1))
Case sepCode
' we've found the end of an item
' if endIndex<>0 then the item is quoted
If endIndex = 0 Then endIndex = index
' make room in the array, if necessary
If resCount > UBound(res) Then
ReDim Preserve res(resCount) As String
End If
'store the element
res(resCount) = Mid(Text, startIndex, endIndex - startIndex)
resCount = resCount + 1
' prepare for next element
startIndex = index + 1
endIndex = 0
Case quoteCode
startIndex = index + 1
' search for the closing quote
endIndex = InStr(startIndex, Text, Right(Quotes, 1))
index = endIndex
End Select
Loop
' store the last item
If endIndex = 0 Then endIndex = length + 1
' trim or expand the array, as necessary
ReDim Preserve res(resCount) As String
' store the element
res(resCount) = Mid(Text, startIndex, endIndex - startIndex)
SplitQuoted = res()
Erase res
End Function
