DamianVB
asked on
having a problem getting the working directory from a text box
This is the code I have in my app:-
Dim sFile As String
Dim sCommand As String
Dim sWorkDir As String
sFile = (Text1)
sCommand = vbNullString
sWorkDir = (Text1)
ShellExecute hwnd, "open", sFile, sCommand, sWorkDir, 1
i need the sWorkDir to get the directory of text1, not the whole filename as it is currently doing, is there a simple way to do this?
Dim sFile As String
Dim sCommand As String
Dim sWorkDir As String
sFile = (Text1)
sCommand = vbNullString
sWorkDir = (Text1)
ShellExecute hwnd, "open", sFile, sCommand, sWorkDir, 1
i need the sWorkDir to get the directory of text1, not the whole filename as it is currently doing, is there a simple way to do this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi there.
Not sure if there is an API to do this already, doubt it, but you could try the following code:
Private Function GetDirectory(sFilename As String) As String
Dim sTmpFilename As String
Dim iSlashPos As Integer
sTmpFilename = StrReverse(sFilename)
iSlashPos = InStr(1, sTmpFilename, "\")
GetDirectory = Left$(sFilename, Len(sFilename) - (iSlashPos))
End Function
Example
Debug.Print GetDirectory("C:\Temp\File s\File.txt ")
will print "C:\Temp\Files"
Hope this helps.
Jas.
Not sure if there is an API to do this already, doubt it, but you could try the following code:
Private Function GetDirectory(sFilename As String) As String
Dim sTmpFilename As String
Dim iSlashPos As Integer
sTmpFilename = StrReverse(sFilename)
iSlashPos = InStr(1, sTmpFilename, "\")
GetDirectory = Left$(sFilename, Len(sFilename) - (iSlashPos))
End Function
Example
Debug.Print GetDirectory("C:\Temp\File
will print "C:\Temp\Files"
Hope this helps.
Jas.
ASKER
I just tried that code aelatik and it now works perfectly, thanks very much :)
MsgBox Mid(Text1, 1, Len(Text1) - InStr(1, StrReverse(Text1), "\", vbTextCompare) + 1)
MsgBox Right(Text1, InStr(1, StrReverse(Text1), "\", vbTextCompare) - 1)