For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > November 2005 > Path only from CommonDialog









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Path only from CommonDialog
The Merg

2005-11-28, 6:55 pm

Does anyone know of a simple way to get the path without the filename from
the return of a common dialog control. In my app, I want the user to select
the path where data is stored. I can easily use the common dialog to allow
the user to "open" a data file in the directory they want to use. Instead of
actually performing any task on the file they select, I just want to see the
directory path that the file is in. The filename property of the dialog
control includes the filename and the path. Do I need to search on the path
to find the last "\", find the length of the filename after the "\", then
knock that off the filename property? Or is there a much easier way that I'm
missing?

Thanks,
The Merg

--
Today's problems don't worry me,
I haven't solved yesterday's yet.


alpine

2005-11-28, 6:55 pm

On Mon, 28 Nov 2005 09:24:41 -0500, "The Merg"
<howard_mergler@NOSPAM.themerg.net> wrote:

>Does anyone know of a simple way to get the path without the filename from
>the return of a common dialog control. In my app, I want the user to select
>the path where data is stored. I can easily use the common dialog to allow
>the user to "open" a data file in the directory they want to use. Instead of
>actually performing any task on the file they select, I just want to see the
>directory path that the file is in. The filename property of the dialog
>control includes the filename and the path. Do I need to search on the path
>to find the last "\", find the length of the filename after the "\", then
>knock that off the filename property? Or is there a much easier way that I'm
>missing?
>
>Thanks,
>The Merg



Use the BrowseForFolder API instead of the file dialog. You should be
able to find an example at http://vbnet.mvps.org/

HTH,
Bryan
_______________________________
Bryan Stafford
New Vision Software
newvision_don'tspam@mvps.org
FM

2005-11-28, 6:55 pm

This function was posted here some time ago which does the trick:

strFolderPath = StripFileName(.Filename)

Public Function StripFileName(FileName As String) As String
Dim i As Integer
StripFileName = FileName
i = InStrRev(FileName, "\")
If i > 0 Then
'Return only the path:
StripFileName = Left$(StripFileName, i)
End If
End Function


"The Merg" <howard_mergler@NOSPAM.themerg.net> wrote in message
news:udrlycC9FHA.1416@TK2MSFTNGP09.phx.gbl...
> Does anyone know of a simple way to get the path without the filename from
> the return of a common dialog control. In my app, I want the user to

select
> the path where data is stored. I can easily use the common dialog to allow
> the user to "open" a data file in the directory they want to use. Instead

of
> actually performing any task on the file they select, I just want to see

the
> directory path that the file is in. The filename property of the dialog
> control includes the filename and the path. Do I need to search on the

path
> to find the last "\", find the length of the filename after the "\", then
> knock that off the filename property? Or is there a much easier way that

I'm
> missing?
>
> Thanks,
> The Merg
>
> --
> Today's problems don't worry me,
> I haven't solved yesterday's yet.
>
>



Paul Clement

2005-11-28, 6:55 pm

On Mon, 28 Nov 2005 09:24:41 -0500, "The Merg" <howard_mergler@NOSPAM.themerg.net> wrote:

¤ Does anyone know of a simple way to get the path without the filename from
¤ the return of a common dialog control. In my app, I want the user to select
¤ the path where data is stored. I can easily use the common dialog to allow
¤ the user to "open" a data file in the directory they want to use. Instead of
¤ actually performing any task on the file they select, I just want to see the
¤ directory path that the file is in. The filename property of the dialog
¤ control includes the filename and the path. Do I need to search on the path
¤ to find the last "\", find the length of the filename after the "\", then
¤ knock that off the filename property? Or is there a much easier way that I'm
¤ missing?

Yes, you have to parse the resulting value, which is relatively easy to do using the InStrRev
function.


Paul
~~~~
Microsoft MVP (Visual Basic)
Joe O

2005-11-28, 6:55 pm

' WARNING!!!! Air code not tested
By the way I dont remember if there is any commondialog property to extract
file path
Dim sName As String, iPos
sName = CommonDialogX.FileName
iPos = InStr(1, (StrReverse(sName)), "\")
MsgBox Left(sName, Len(sName) - iPos)

'Another way
Dim sName As String, iPos
sName = CommonDialogX.FileName
iPos = Len(CommonDialogX.FileTitle)
MsgBox Left(sName, Len(sName) - iPos)



"The Merg" <howard_mergler@NOSPAM.themerg.net> wrote in message
news:udrlycC9FHA.1416@TK2MSFTNGP09.phx.gbl...
> Does anyone know of a simple way to get the path without the filename from
> the return of a common dialog control. In my app, I want the user to

select
> the path where data is stored. I can easily use the common dialog to allow
> the user to "open" a data file in the directory they want to use. Instead

of
> actually performing any task on the file they select, I just want to see

the
> directory path that the file is in. The filename property of the dialog
> control includes the filename and the path. Do I need to search on the

path
> to find the last "\", find the length of the filename after the "\", then
> knock that off the filename property? Or is there a much easier way that

I'm
> missing?
>
> Thanks,
> The Merg
>
> --
> Today's problems don't worry me,
> I haven't solved yesterday's yet.
>
>



Mike L

2005-11-28, 6:55 pm

Here's a one-liner than seems to work good for me:

strPathOfFolder = Replace(CommonDialog1.FileName, "\" &
CommonDialog1.FileTitle, "")


Mike L

2005-11-28, 6:55 pm

"Mike L" <pillowpc2001@paonline.com> wrote:
> Here's a one-liner than seems to work good for me:
>
> strPathOfFolder = Replace(CommonDialog1.FileName, "\" &
> CommonDialog1.FileTitle, "")


Oops, seems OE put it on two lines. This will work better:

strPathOfFolder = Replace(CommonDialog1.FileName, _
"\" & CommonDialog1.FileTitle, "")


Joe O

2005-11-28, 6:55 pm

Great

"Mike L" <pillowpc2001@paonline.com> wrote in message
news:uCfBX5C9FHA.2576@TK2MSFTNGP12.phx.gbl...
> "Mike L" <pillowpc2001@paonline.com> wrote:
>
> Oops, seems OE put it on two lines. This will work better:
>
> strPathOfFolder = Replace(CommonDialog1.FileName, _
> "\" & CommonDialog1.FileTitle, "")
>
>



Dave

2005-11-28, 6:55 pm

Use the BrowseForFolder API as suggested by Alpine, any solution using the
OpenFile dialog will fail if the folder is empty, and it looks stupid having
to explain to the user to select any file but "don't worry as the file
itself won't be touched".

Best Regards
Dave O.

"The Merg" <howard_mergler@NOSPAM.themerg.net> wrote in message
news:udrlycC9FHA.1416@TK2MSFTNGP09.phx.gbl...
> Does anyone know of a simple way to get the path without the filename from
> the return of a common dialog control. In my app, I want the user to
> select the path where data is stored. I can easily use the common dialog
> to allow the user to "open" a data file in the directory they want to use.
> Instead of actually performing any task on the file they select, I just
> want to see the directory path that the file is in. The filename property
> of the dialog control includes the filename and the path. Do I need to
> search on the path to find the last "\", find the length of the filename
> after the "\", then knock that off the filename property? Or is there a
> much easier way that I'm missing?
>
> Thanks,
> The Merg
>
> --
> Today's problems don't worry me,
> I haven't solved yesterday's yet.
>



Mike L

2005-11-28, 6:55 pm

"Mike L" <pillowpc2001@paonline.com> wrote:
> Here's a one-liner than seems to work good for me:
>
> strPathOfFolder = Replace(CommonDialog1.FileName, _
> "\" & CommonDialog1.FileTitle, "")


Just realized a small problem with this code: if one of the folder names in
the path starts with the file title (including the extension), it will
remove that as well.

Ex. The file name "C:\My Documents\Test.exe Files\Test.exe" will result in
the path "C:\My Documents Files".


Ken Halter

2005-11-28, 6:55 pm

"Mike L" <pillowpc2001@paonline.com> wrote in message
news:Okum%23dD9FHA.2576@TK2MSFTNGP12.phx.gbl...
> "Mike L" <pillowpc2001@paonline.com> wrote:
>
> Just realized a small problem with this code: if one of the folder names
> in



No problem... using the wrong control anyway so "unpredictable results" are
sure to follow.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..


The Merg

2005-11-29, 6:55 pm

Thanks for all the suggestions. I went to vbnet.mvps.org as suggested and
found a Browse Dialog control by Brad Martinez. I haven't put it in my app
yet, but the description seems to fit exactly what I want. It just allows
you to browse and select a folder without showing files.

- The Merg

--
Today's problems don't worry me,
I haven't solved yesterday's yet.


"Dave" <nobody@nowhere.com> wrote in message
news:OtuL9aD9FHA.2036@TK2MSFTNGP14.phx.gbl...
> Use the BrowseForFolder API as suggested by Alpine, any solution using the
> OpenFile dialog will fail if the folder is empty, and it looks stupid
> having to explain to the user to select any file but "don't worry as the
> file itself won't be touched".
>
> Best Regards
> Dave O.
>
> "The Merg" <howard_mergler@NOSPAM.themerg.net> wrote in message
> news:udrlycC9FHA.1416@TK2MSFTNGP09.phx.gbl...
>
>



J French

2005-11-29, 6:55 pm

On Tue, 29 Nov 2005 10:33:58 -0500, "The Merg"
<howard_mergler@NOSPAM.themerg.net> wrote:

>Thanks for all the suggestions. I went to vbnet.mvps.org as suggested and
>found a Browse Dialog control by Brad Martinez. I haven't put it in my app
>yet, but the description seems to fit exactly what I want. It just allows
>you to browse and select a folder without showing files.


The SHBrowseForFolder Dialog

- I've never used it, as the problem is that the user wants to see the
files in the directory, just to know that they are in the right place.

Usually I resort to the old Drive/Dir/File selection stuff
- it may look naff - but ....
alpine

2005-11-29, 6:55 pm

On Tue, 29 Nov 2005 17:05:48 +0000 (UTC), erewhon@nowhere.uk (J
French) wrote:

>On Tue, 29 Nov 2005 10:33:58 -0500, "The Merg"
><howard_mergler@NOSPAM.themerg.net> wrote:
>
>
>The SHBrowseForFolder Dialog
>
>- I've never used it, as the problem is that the user wants to see the
>files in the directory, just to know that they are in the right place.
>
>Usually I resort to the old Drive/Dir/File selection stuff
>- it may look naff - but ....



With the new UI for the browse for folder dialog, the user *can* see
the files in the directory if you set the correct flags. Of course,
they have to be running an OS/shell that supports the new UI.

HTH,
Bryan
_______________________________
Bryan Stafford
New Vision Software
newvision_don'tspam@mvps.org
J French

2005-11-30, 7:55 am

On Tue, 29 Nov 2005 11:52:47 -0700, alpine
<alpine_don'tsendspam@mvps.org> wrote:

>On Tue, 29 Nov 2005 17:05:48 +0000 (UTC), erewhon@nowhere.uk (J
>French) wrote:
>


<snip>

[color=darkred]
>With the new UI for the browse for folder dialog, the user *can* see
>the files in the directory if you set the correct flags. Of course,
>they have to be running an OS/shell that supports the new UI.


Interesting - thanks
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com