Solved

having a problem getting the working directory from a text box

Posted on 2003-11-04
4
191 Views
Last Modified: 2010-05-03
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
Comment
Question by:DamianVB
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
aelatik earned 125 total points
ID: 9679925
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
 
LVL 14

Expert Comment

by:aelatik
ID: 9679940
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
 
LVL 10

Expert Comment

by:MrClyfar
ID: 9679962
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
 

Author Comment

by:DamianVB
ID: 9679992
I just tried that code aelatik and it now works perfectly, thanks very much :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel Spreadsheet - Send email if certain value is selected. 14 87
Access 2016 VB code 9 103
Child Form in front 4 39
Set email body to html using vbscript 6 26
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now