[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

Splitting a string containg filename+path and arguemnts

Leo

6/15/2011 12:59:00 PM

I am writing a Shell() replacement that I intend to be a drop in
replacement. I am using ShellExecuteEx so I can parse a n image or text
file in. The problem comes with spliting the arguements from the
path+filename, what is the best way to split them assuming possible
paths with spaces that aren't quoted. An example that works with
Shell() and WinExec (I know I shouldn't be using it) but I strugle with
spliting is C:\Program Files\Windows NT\Accessories\wordpad.exe
C:\Users\username\Documents\somertf.rtf . PathRemoveArgs gets confused
and cuts the path at the space in "Program Files".

--
ClassicVB Users Regroup! comp.lang.basic.visual.misc
Free usenet access at http://www.eternal-sep...


2 Answers

Dee Earley

6/15/2011 2:22:00 PM

0

On 15/06/2011 13:59, Leo wrote:
> I am writing a Shell() replacement that I intend to be a drop in
> replacement. I am using ShellExecuteEx so I can parse a n image or text
> file in. The problem comes with spliting the arguements from the
> path+filename, what is the best way to split them assuming possible
> paths with spaces that aren't quoted. An example that works with Shell()
> and WinExec (I know I shouldn't be using it) but I strugle with spliting
> is C:\Program Files\Windows NT\Accessories\wordpad.exe
> C:\Users\username\Documents\somertf.rtf . PathRemoveArgs gets confused
> and cuts the path at the space in "Program Files".

The "official" way is to split on the first unquoted space.
You can try to be more intelligent by splitting on eahc in turn until
you get a match for a valid file.
Alterntaively, just pass the whole lot to ShellExecute as lpFile and let
it handle it. It's a lot more forgiving...

--
Dee Earley (dee.earley@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk...

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)

Karl E. Peterson

6/15/2011 6:14:00 PM

0

It happens that Leo formulated :
> I am writing a Shell() replacement that I intend to be a drop in replacement.
> I am using ShellExecuteEx so I can parse a n image or text file in. The
> problem comes with spliting the arguements from the path+filename, what is
> the best way to split them assuming possible paths with spaces that aren't
> quoted. An example that works with Shell() and WinExec (I know I shouldn't be
> using it) but I strugle with spliting is C:\Program Files\Windows
> NT\Accessories\wordpad.exe C:\Users\username\Documents\somertf.rtf .
> PathRemoveArgs gets confused and cuts the path at the space in "Program
> Files".

If you have a string that contains an unquoted path with embedded
spaces *and* arguments, you're fairly hosed. Where are you getting
that from? I think you'll need to attack, or reconsider, the source.

If you just need to split filename and filepath, I use these...

Private Declare Sub PathStripPathW Lib "shlwapi" (ByVal lpszPath As
Long)
Private Declare Function PathRemoveBackslashW Lib "shlwapi" (ByVal
lpszPath As Long) As Long


Public Function PathStripFile(ByVal Path As String) As String
Dim FileName As String
' Removes the path portion of a fully qualified path and file.
FileName = PathStripPath(Path)
' Remove filename from original path.
If Len(FileName) < Len(Path) Then
Path = Left$(Path, Len(Path) - Len(FileName))
Else
Path = ""
End If
' Take off trailing backslash for consistency.
PathStripFile = PathRemoveBackslash(Path)
End Function

Public Function PathStripPath(ByVal Path As String) As String
Dim Buffer As String
' Removes the path portion of a fully qualified path and file.
Buffer = String$(MAX_PATH, 0)
Mid$(Buffer, 1) = Path
Call PathStripPathW(StrPtr(Buffer))
PathStripPath = TrimNull(Buffer)
End Function

Public Function PathRemoveBackslash(ByVal Path As String) As String
Dim Buffer As String
' Removes the trailing backslash from a given path.
Buffer = String$(MAX_PATH, 0)
Mid$(Buffer, 1) = Path
Call PathRemoveBackslashW(StrPtr(Buffer))
' Results point to position within buffer of null terminator,
' or to the last character if there was no backslash, so it's
' almost insane to do the math. Far easier to TrimNull!
PathRemoveBackslash = TrimNull(Buffer)
End Function

--
..NET: It's About Trust!
http://vfre...