Use Dir() to fill out missing components from Path?

D

Dan Williams

I’d like a way to flesh out a file’s path the way the Dir() function
does. The Dir() function (like the DIR command in DOS) fills in
missing components depending on what the current Path is set to.

Suppose the current Path is

C:\BigFolder

Then all of the following

Dir(“SubFolder\myfile.xls”)
Dir(“C:SubFolder\myfile.xls”)
Dir(“C:\BigFolder\SubFolder\myfile.xls”)

...will be treated as attempts to find this file:

C:\BigFolder\SubFolder\myfile.xls

If the User enters any one of the above strings, I’d like to tell
them:

You are trying to access C:\BigFolder\SubFolder\myfile.xls

And if they just enter the string “OtherFile.xls”, I’d like to respond
with:

You are trying to access C:\BigFolder\OtherFile.xls

...etc. I’d like to be able to make the responses WHETHER THE FILE
EXISTS OR NOT, so that if the file does NOT exist, my error message
can include the whole attempted path.

Is there some easy way I haven’t thought of to do this? I’m guessing
you’d have to treat all these cases separately. Or does someone have
an already-written routine that would do this?

Dan Williams
danwPlanet
 
R

Rick Rothstein \(MVP - VB\)

You can use the CurDir function to see what the current directory is. For
example...

MsgBox "You are trying to access " & CurDir & "\" & UserEnteredFileName

Rick


I’d like a way to flesh out a file’s path the way the Dir() function
does. The Dir() function (like the DIR command in DOS) fills in
missing components depending on what the current Path is set to.

Suppose the current Path is

C:\BigFolder

Then all of the following

Dir(“SubFolder\myfile.xls”)
Dir(“C:SubFolder\myfile.xls”)
Dir(“C:\BigFolder\SubFolder\myfile.xls”)

....will be treated as attempts to find this file:

C:\BigFolder\SubFolder\myfile.xls

If the User enters any one of the above strings, I’d like to tell
them:

You are trying to access C:\BigFolder\SubFolder\myfile.xls

And if they just enter the string “OtherFile.xls”, I’d like to respond
with:

You are trying to access C:\BigFolder\OtherFile.xls

....etc. I’d like to be able to make the responses WHETHER THE FILE
EXISTS OR NOT, so that if the file does NOT exist, my error message
can include the whole attempted path.

Is there some easy way I haven’t thought of to do this? I’m guessing
you’d have to treat all these cases separately. Or does someone have
an already-written routine that would do this?

Dan Williams
danwPlanet
 
D

Dan Williams

No, see my examples. Inserting the current path before the User's
string would not make sense with any of the example strings I listed.

Dan
 
D

Dan Williams

No, see my examples.  Inserting the current path before the User's
string would not make sense with any of the example strings I listed.

Dan


















- Show quoted text -

(Oops, except the first exsmple.)
 
P

Peter T

I'm relatively familiar with the Dir function, I suspect Rick even more so.
However it is not at all clear from your OP what you are doing or what you
want.

Regards,
Peter T

No, see my examples. Inserting the current path before the User's
string would not make sense with any of the example strings I listed.

Dan
 
R

Rick Rothstein \(MVP - VB\)

Okay, I *think* I see what you are trying to do. Does this function do what
you want?

Function CompletePath(ByVal UserEnteredPath As String) As String
Dim X As Long
Dim Lengths As Long
Dim CurrentPath As String
Dim FileName As String
Dim Path As String
Dim TempPath As String
Dim Folders() As String
If Left(UserEnteredPath, 1) <> Left(UserEnteredPath, 1) And _
InStr(UserEnteredPath, ":") > 0 Then
CompletePath = UserEnteredPath
Else
Path = Mid$(UserEnteredPath, InStr(UserEnteredPath, ":") + 1)
If Left(Path, 1) = "\" Then Path = Mid(Path, 2)
Folders = Split(Path, "\")
For X = 0 To UBound(Folders) - 1
CurrentPath = CurrentPath & "\" & Folders(X)
If InStr(CurDir, CurrentPath) = 0 Then
Exit For
Else
Folders(X) = ""
End If
Next
CompletePath = CurDir & "\" & Join(Folders, "\")
Do While InStr(CompletePath, "\\")
CompletePath = Replace(CompletePath, "\\", "\")
Loop
End If
End Function


Rick


No, see my examples. Inserting the current path before the User's
string would not make sense with any of the example strings I listed.

Dan
 
D

Dan Williams

Wow, and here I thought I had included too much detail.

Rick, your function is pretty good -- it works for all 4 of my
examples -- I really didn't want anyone to write something from
scratch. But let me explain it again this way: Suppose the User's
string is put into a Dir() function and no file is found. I want to
be able to tell the User precisely WHAT file was not found, i.e., I
want it to show what the Dir() function looked for, even if it had to
fill in parts of the path.

Here are the only additional examples I can think of that don't work
with your function, but don't spend more time on it, since it should
be easy for me to modify your code to include them.

Current Path: C:\BigFolder

User Input: \radomfile.xls
Response: You are trying to access C:\radomfile.xls

User Input: D:\radomfile.xls
Response: You are trying to access D:\radomfile.xls

User Input: D:radomfile.xls
Response: You are trying to access D:\radomfile.xls

Thanks for the code!

Dan
 
D

Dan Williams

Oh, I see that the switch from the C: drive to another drive is
already handled in your code, but you made a typo -- the first line
should be

If Left(UserEnteredPath, 1) <> Left(CurDir, 1) And _
InStr(UserEnteredPath, ":") > 0 Then
 
T

Tim Williams

I'm sure you've considered this, but why not just have the user browse for
and select the file they want?
From my own experience, having to remember and then type in a path is not
something users like to be made to do.

Tim



Oh, I see that the switch from the C: drive to another drive is
already handled in your code, but you made a typo -- the first line
should be

If Left(UserEnteredPath, 1) <> Left(CurDir, 1) And _
InStr(UserEnteredPath, ":") > 0 Then
 
D

Dan Williams

Yes, I am in fact doing that. But if they type a path manually or
type just a file name knowing that the current path is usually right,
I want it to respond correctly to that, too. It's overkill, I
suppose. :)
 
D

Dan Williams

I've been meaning to post my eventual solution:
__________________

Function CompletePath(ByVal UserEnteredPath As String) As String
' Assumes that completely invalid path/filename
' combinations are detected elsewhere.

Dim myCD As String
Dim myInitialDir As String
Dim myTempDir As String

myCD = Left(CurDir, 1)
If Left(UserEnteredPath, 1) = "\" Then
' if \something
CompletePath = myCD & ":" & UserEnteredPath
ElseIf Mid(UserEnteredPath, 2, 2) = ":\" Then
' if X:\something
CompletePath = UserEnteredPath
ElseIf Left(UserEnteredPath, 2) = myCD & ":" Then
' if C:something
CompletePath = CurDir & "\" & Mid(UserEnteredPath, 3)
ElseIf Mid(UserEnteredPath, 2, 1) = ":" Then
' if X:something
myInitialDir = CurDir
ChDrive UserEnteredPath
myTempDir = CurDir
If Mid(myTempDir, 2) = ":\" Then
myTempDir = Left(myTempDir, 2)
End If
ChDrive myInitialDir
ChDir myInitialDir
CompletePath = myTempDir & "\" & Mid(UserEnteredPath, 3)
Else
' if something
CompletePath = CurDir & "\" & UserEnteredPath
End If
End Function
__________________

My earlier test examples (and the suggested code) failed to treat
these cases:

Current Drive: C:
Current CPath: C:\BigFolder
Current DPath: D:\DFolder

User Input: D:randomfile.xls
Response: You are trying to access D:\DFolder
\randomfile.xls

User Input: C:\SubFolder\BigFolder\myfile.xls
The For/Next loop approach thinks this is
C:\BigFolder\SubFolder\myfile.xls

User Input: C:\BigFolder\BigFolder\BigFolder\myfile.xls
The For/Next loop approach thinks this is
C:\BigFolder\SubFolder\myfile.xls

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top