Help trimming off filename from path

P

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
 
H

Herfried K. Wagner [MVP]

PvtPile said:
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.
 
G

Guest

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\
 
M

Mythran

Herfried K. Wagner said:
Check out the 'System.IO.Path' class and its shared methods.

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

So...

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

:)

HTH,
Mythran
 
H

Herfried K. Wagner [MVP]

iwdu15 said:
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.
 
G

Guest

thats a good point...my code would only work on the systems that use the \
path separator, thanks for the correction
 
P

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!
 
G

Guest

glad i could help, just make sure you check Herfried's post above about your
solution
 
P

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top