Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving a Folder`s Path 1

Status
Not open for further replies.

Taff07

MIS
Jul 18, 2001
160
0
0
GB
Hi,

I`m trying to find some code that will allow a user to navigate through folders and when at the desired location will return that path for use in the future. I know there is code etc to do this but am unable to track it down.

Can anyone plz help.

Thanks in advance

Ian
 
I have heard that there are TreeView controls available, but have not had a chance to use them yet. In the past I used to DIR() to loop through and search for directories. Then I stored the resultant set into an arrary or temporary table (which I later deleted). htwh Steve Medvid
"IT Consultant & Web Master"
web page under development...
 
The directory can vary is the problem but the filenames will be the same.

I`m hoping to allow the user to browse through and select a folder rather than trying to type in the path and possibly getting it slightly wrong.

Thanks
 
Another approach is to use the Shell command to execute a DIR /s /on >temp.txt. Then read the temp.txt into a temporary table. ...
'create a directory listing by writing a batch file and executing it
sCommand = "dir " & lcBase_Path & "*.mdb /s /b /o-s >" & APP_PATH & "dir_list_mdb.lst"
Open APP_PATH & "dir.bat" For Output As #1
Print #1, sCommand
Close #1

wHandle = Shell(APP_PATH & "dir.bat")
DelayTime 10 'delay to wait for the directory list, required becasue Shell command is asyncronous.
appLoop wHandle

Open APP_PATH & "dir_list_mdb.lst" For Input As #1 'open the directory list file and load table
Do While Not EOF(1)... etc...


Public Function DelayTime(PauseTime As Integer)
Dim start
start = Timer ' Set start time.
Do While Timer < start + PauseTime
DoEvents ' Yield to other processes.
Loop
End Function
Sub appLoop(wHandle As Long)
On Error GoTo errorhandler
Do While 1
AppActivate wHandle, False
Loop
errorhandler:
Exit Sub
End Sub


Or use DIR()

From Help... here is how to loop through with DIR()

' Display the names in C:\ that represent directories.
MyPath = &quot;c:\&quot; ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> &quot;&quot; ' Start the loop.
' Ignore the current directory and the encompassing directory.
If MyName <> &quot;.&quot; And MyName <> &quot;..&quot; Then
' Use bitwise comparison to make sure MyName is a directory.

If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' Display entry only if it (OR Write to a table)
End If ' it represents a directory.
End If
MyName = Dir ' Get next entry.
Loop

Steve Medvid
&quot;IT Consultant & Web Master&quot;
web page under development...
 
Use the common dialog control. Put one of the controls on the form.

below code assumes a button called cmdBrowse and a common dialog control called comdfilepicker. THis one just looks for text files

Private Sub CmdBrowse_Click()
Const cdlOFNFileMustExist = &H1000
Const CdlOFNExtensionDifferent = &H400
'Let the user select a txt file to import
On Error GoTo errhandler

With comdFilePicker
.Filter = &quot;Text Files (*.txt)|*.txt&quot;
.DefaultExt = &quot;.txt&quot;
'.FilterIndex = 1
.Flags = cdlOFNFileMustExist + CdlOFNExtensionDifferent
.ShowOpen
End With
'show the file
msgbox comdFilePicker.FileName

Exit Sub
errhandler:
'cancel pressed
MsgBox Err.Number
MsgBox Err.Description
End Sub
 
common dialog control - best approach to allow user to select a file... I thought u wanted a directory name... Steve Medvid
&quot;IT Consultant & Web Master&quot;
web page under development...
 

Thanks for responses and yep I`m after the directory name and not the actual file itself.

I have the coding that will supply the whole path including the file but not one that will pull back only the directory level.

smedvid,

Am IU correct in thinking that your method will produce a list of all deirectory levels within the targeted drive? I`m on a network and so the directory structures can get very complicated to show in this manner. Is there not a way like the filepicker that works only at the directory level?

Thanks for your time

Taff
 
Actually, the code where I use the Shell Command the issue a DIR is on a network... In that case, I am grabbing all mdb files from every directory on the LAN. If you just to a DIR *. /s that should return just the directory names.

I do not believe the common dialog can be used to select only a directory. I have hear of tree-view controls, but have never used them... they may permit such selection...

htwh Steve Medvid
&quot;IT Consultant & Web Master&quot;
web page under development...
 
I need the code that will pull back the whole file. Any givers.
 
Hi,
I found this tread while I was looking for solution of the same kind. I want to let the user select(brouwse) a path to copy the database tables(back end)to, as a back up.
The suggestion of everytime would be very usefull in my case, but where can I find this common dialog control in Access 2000?
 
Hi

Try the code below, it will allow your user to browse folders and only return the path information without the filename to a string variable for you. Once you pass the information to the variable you can add code to write it to a table.

'***Copy and paste this to a module***

Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib &quot;shell32.dll&quot; Alias _
&quot;SHGetPathFromIDListA&quot; (ByVal pidl As Long, _
ByVal pszPath As String) As Long

Private Declare Function SHBrowseForFolder Lib &quot;shell32.dll&quot; Alias _
&quot;SHBrowseForFolderA&quot; (lpBrowseInfo As BROWSEINFO) _
As Long


Private Const BIF_RETURNONLYFSDIRS = &H1


Public Function BrowseFolder(szDialogTitle As String) As String
Dim X As Long, bi As BROWSEINFO, dwIList As Long
Dim szPath As String, wPos As Integer

With bi
.hOwner = hWndAccessApp
.lpszTitle = szDialogTitle
.ulFlags = BIF_RETURNONLYFSDIRS
End With

dwIList = SHBrowseForFolder(bi)
szPath = Space$(512)
X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)

If X Then
wPos = InStr(szPath, Chr(0))
BrowseFolder = Left$(szPath, wPos - 1)
Else
BrowseFolder = &quot;&quot;
End If
End Function

'***Add this to the click event of your command button***

Dim strFolderName as string

strFolderName = BrowseFolder(&quot;What Folder you want to select?&quot;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top