atomicgs12
asked on
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.t xt"
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
Dim strPath As String = "c:\\docs\\folder1\\test.t
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Dim fullPathFileName As String = "c:\docs\folder1\test.txt "
Dim fi As New System.IO.FileInfo(fullPat hFileName)
MsgBox(fi.DirectoryName)
Dim fi As New System.IO.FileInfo(fullPat
MsgBox(fi.DirectoryName)
ASKER
jppinto -
for Dim fi As New System.IO.FileInfo(fullPat hFileName) , what happens if c:\docs\folder1 does not exist? That is ultimately what I am doing, checking if the directory exists.
Thanks
for Dim fi As New System.IO.FileInfo(fullPat
Thanks
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")
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
Thanks
I'll dispense wit the DIM's, I'm sure you know them...
FullPath = "c:\\docs\\folder\\test.tx t"
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
FullPath = "c:\\docs\\folder\\test.tx
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
Opps, typed too slow. Sorry.
MsgBox(My.Computer.FileSys
jppinto