Help trimming off filename from path

  • Thread starter Thread starter PvtPile
  • Start date Start date
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
 
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.
 
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\
 
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
 
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.
 
thats a good point...my code would only work on the systems that use the \
path separator, thanks for the correction
 
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!
 
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!
 
Back
Top