Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

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?
0
DamianVB
Asked:
DamianVB
  • 2
1 Solution
 
aelatikCommented:
Dim sFile As String
Dim sCommand As String
Dim sWorkDir As String

sFile = Right(Text1, InStr(1, StrReverse(Text1), "\", vbTextCompare) - 1)
sCommand = vbNullString
sWorkDir = Mid(Text1, 1, Len(Text1) - InStr(1, StrReverse(Text1), "\", vbTextCompare) + 1)
ShellExecute hwnd, "open", sFile, sCommand, sWorkDir, 1
0
 
aelatikCommented:
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)
0
 
Jason EvansSenior Software DeveloperCommented:
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.
0
 
DamianVBAuthor Commented:
I just tried that code aelatik and it now works perfectly, thanks very much :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now