Home All Groups Group Topic Archive Search About

Help trimming off filename from path

Author
2 May 2006 9:56 PM
PvtPile
I've got a form where i need to pull both the directory of a filename
and the directory with the filename of an .inf file... I figured the
easiest way to do this was to have an <input id=test type=file
runat=server> control for the user to find the .inf file, and then make
vb cut off the filename upon submission of the form. I just can't
figure out how to trim off the filename leaving just the directory it's
located in.


just as a test i'm trying to display the content as follows

Label1.Text = formINFLocation.Value.Trim(formDriverINF.FileName)

I did this hoping to dynamically trim off any inf filename that is
chosen... though it seems that the Trim function does not work the way
i was assuming it did. Is there another way to remove the filename from
this location? I have a hunch it's far more complicated than my simple
idea here. :D

Author
2 May 2006 10:03 PM
Herfried K. Wagner [MVP]
"PvtPile" <derek.pe***@gmail.com> schrieb:
> I've got a form where i need to pull both the directory of a filename
> and the directory with the filename of an .inf file... I figured the
> easiest way to do this was to have an <input id=test type=file
> runat=server> control for the user to find the .inf file, and then make
> vb cut off the filename upon submission of the form. I just can't
> figure out how to trim off the filename leaving just the directory it's
> located in.

Check out the 'System.IO.Path' class and its shared methods.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
2 May 2006 10:38 PM
Mythran
Show quote Hide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:%23ZDAERjbGHA.4932@TK2MSFTNGP03.phx.gbl...
> "PvtPile" <derek.pe***@gmail.com> schrieb:
>> I've got a form where i need to pull both the directory of a filename
>> and the directory with the filename of an .inf file... I figured the
>> easiest way to do this was to have an <input id=test type=file
>> runat=server> control for the user to find the .inf file, and then make
>> vb cut off the filename upon submission of the form. I just can't
>> figure out how to trim off the filename leaving just the directory it's
>> located in.
>
> Check out the 'System.IO.Path' class and its shared methods.
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>

Yeah, System.IO.Path.GetDirectoryName gets the directory path you want :)

So...

Dim dirName As String = System.IO.Path.GetDirectoryName(...)

:)

HTH,
Mythran
Author
2 May 2006 10:06 PM
iwdu15
try something along the lines of this:

UNTESTED

Label1.Text = strPath.Substring(0, strPath.LastIndexOf("\"))

that should give you the directory with the last \

ex:

C:\SomeFolder\AnotherFolder\

--
-iwdu15
Author
2 May 2006 11:13 PM
Herfried K. Wagner [MVP]
"iwdu15" <jmmgoalsteratyahoodotcom> schrieb:
> Label1.Text = strPath.Substring(0, strPath.LastIndexOf("\"))
>
> that should give you the directory with the last \
>
> ex:
>
> C:\SomeFolder\AnotherFolder\

Directory paths in Windows typically do not include a trailing backslash.
In addition, note that not all systems necessarily use "\" as path separator
character and thus your code won't work on these systems.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
2 May 2006 11:23 PM
iwdu15
thats a good point...my code would only work on the systems that use the \
path separator, thanks for the correction
--
-iwdu15
Author
2 May 2006 11:23 PM
PvtPile
Thanks iwdu15

That worked great!

I hate it when people just say "It worked, thanks." without showing us
the results... so here it is.

Dim pathname As String = FileUpload.Value
Label1.Text = (FileUpload.Value.Substring(0,
FileUpload.Value.LastIndexOf("\")))

Again, thanks for all the help on this topic!
Author
3 May 2006 12:05 AM
iwdu15
glad i could help, just make sure you check Herfried's post above about your
solution
--
-iwdu15
Author
3 May 2006 12:24 AM
PvtPile
Indeed valid. The reason, howerver, is that i need this input is so
that i can use it in a shell command at a later date. The format that
was extracted was exactly what was needed. The directory will be used
to reference a file on an nt machine only. Great work guys... you guys
are too smart!