Extract path from string in Visual Basic

I have a full path passed as say: c:\docs\folder1\test.txt in a string variable as say:
Dim strPath As String = "c:\\docs\\folder1\\test.txt"
I want to extract the directory path without the folder name, so the result would be:
strPath = "c:\\docs\\folder". Of course the folder can be of any name so I DO NOT want to search just for "test.txt".

An example would be nice.

Thanks
atomicgs12Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
atomicgs12,

You included the VB Classic zone, so here is a VB6 / VBA / VBScript approach:


DirPath = Left(strPath, InStrRev(strPath, "\\") - 1)

Open in new window



Patrick
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jppintoCommented:
Dim fullPathFileName As String = "c:\docs\folder1\test.txt "
MsgBox(My.Computer.FileSystem.GetParentPath(fullPathFileName))

jppinto
0
jppintoCommented:
Dim fullPathFileName As String = "c:\docs\folder1\test.txt "
Dim fi As New System.IO.FileInfo(fullPathFileName)
MsgBox(fi.DirectoryName)
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

atomicgs12Author Commented:
jppinto -

for Dim fi As New System.IO.FileInfo(fullPathFileName), what happens if c:\docs\folder1 does not exist? That is ultimately what I am doing, checking if the directory exists.

Thanks
0
wdosanjosCommented:
Try the following.  The file or directory are not required to exist.

Dim dirname As String = System.IO.Path.GetDirectoryName("c:\docs\folder1\test.txt")

Open in new window

0
CodeCruiserCommented:
>That is ultimately what I am doing, checking if the directory exists.

Use IO.File.Exists and IO.Directory.Exists functions
0
atomicgs12Author Commented:
Just so we are all on the same page, I want to test if the directory exists, without the file name on the end. My logis is, test if directory exists, if not do nothing, if directory does exists, test for file name if file name exists add to file, if file does not exist and directory DOES exists create new file then add data.

Thanks
0
VBClassicGuyCommented:
I'll dispense wit the DIM's, I'm sure you know them...

FullPath = "c:\\docs\\folder\\test.txt"
i = InStrRev(FullPath, "\\")
If i Then
   PathName = Left(FullPath, i - 1)
   FileName = Mid(FullPath, i + 2)
   If Len(Dir(PathName)) Then
      If Len(Dir(FullPath)) Then
        '' CODE HERE TO ADD TO FILE
      Elae
         h = FreeFile
         Open FullPath For Output As #h
         Close #h
         '' CODE HERE TO ADD TO FILE
      End If
End If
0
VBClassicGuyCommented:
Opps, typed too slow. Sorry.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.