Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.basic.visual.misc > #269

Re: Splitting a string containg filename+path and arguemnts

From Karl E. Peterson <karl@exmvps.org>
Newsgroups microsoft.public.vb.general.discussion, comp.lang.basic.visual.misc
Subject Re: Splitting a string containg filename+path and arguemnts
Date 2011-06-15 11:14 -0700
Organization exMVPs.org
Message-ID <itaspj$oik$1@dont-email.me> (permalink)
References <itaab6$n61$1@dont-email.me>

Cross-posted to 2 groups.

Show all headers | View raw


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://vfred.mvps.org

Back to comp.lang.basic.visual.misc | Previous | NextPrevious in thread | Find similar


Thread

Splitting a string containg filename+path and arguemnts Leo <ttdhead@gmail.com> - 2011-06-15 22:59 +1000
  Re: Splitting a string containg filename+path and arguemnts Deanna Earley <dee.earley@icode.co.uk> - 2011-06-15 15:21 +0100
  Re: Splitting a string containg filename+path and arguemnts Karl E. Peterson <karl@exmvps.org> - 2011-06-15 11:14 -0700

csiph-web