LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default Use Dir() to fill out missing components from Path?

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


On Apr 25, 4:00*pm, Dan Williams
wrote:
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. *:-)

On Apr 25, 3:47*pm, "Tim Williams" <timjwilliams at gmail dot com
wrote:



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


"Dan Williams" wrote in message


...
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


On Apr 25, 2:33 pm, Dan Williams
wrote:


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


On Apr 25, 1:05 pm, "Rick Rothstein \(MVP - VB\)"


wrote:
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


"Dan Williams" wrote in message


...
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


On Apr 25, 9:41 am, "Rick Rothstein \(MVP - VB\)"


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


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


Rick


"Dan Williams" wrote in message


...
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- Hide quoted text -


- Show quoted text -- Hide quoted text -


- Show quoted text -- Hide quoted text -


- Show quoted text -- Hide quoted text -


- Show quoted text -


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Missing fill handle Speedbump Excel Discussion (Misc queries) 0 May 27th 10 09:53 PM
Fill-in missing information below LuisM Excel Discussion (Misc queries) 2 March 6th 10 06:55 PM
FILL COLOR BOX MISSING Louie Excel Discussion (Misc queries) 1 October 17th 05 09:08 PM
Fill in missing months Charles P. \(Pat\) Upshaw Excel Discussion (Misc queries) 4 June 9th 05 04:11 AM
Missing WrapText from range object in Office Web Components Ricci Gian Maria Excel Programming 0 May 21st 05 10:29 AM


All times are GMT +1. The time now is 07:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"