Link to home
Start Free TrialLog in
Avatar of DamianVB
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?
ASKER CERTIFIED SOLUTION
Avatar of aelatik
aelatik
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First try this to see if the positions are correct, if not just play with the + 1 and - 1

MsgBox Mid(Text1, 1, Len(Text1) - InStr(1, StrReverse(Text1), "\", vbTextCompare) + 1)
MsgBox Right(Text1, InStr(1, StrReverse(Text1), "\", vbTextCompare) - 1)
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\Files\File.txt")

will print "C:\Temp\Files"

Hope this helps.
Jas.
Avatar of DamianVB
DamianVB

ASKER

I just tried that code aelatik and it now works perfectly, thanks very much :)