File List with VBA

File List with VBA

Anonymous
Not applicable
797 Views
11 Replies
Message 1 of 12

File List with VBA

Anonymous
Not applicable
Hi Everyone,

I have a requirement to obtain all of the .dwg files in the folder
containing the active drawing and all sub directories thereunder. I have am
currently using "sendcommand", shelling to Dos, creating a temp text file in
the temp directory, and then reading the file back in with VBA. Although it
is working well, its cumbersome and hacked to say the least. I have several
other apps in full VB that use the file controls as demonstrated in the
"winseek" app. The file controls don't seem to be available in VBA. Does
anyone have or know of a more elegant method of obtaining a file list from a
given dir and all its sub dirs?

Thanks in Advance,

Gary
0 Likes
798 Views
11 Replies
Replies (11)
Message 2 of 12

Anonymous
Not applicable
Are you trying to browse or are you needing the names of the files and
directories for some other process?

If you want to browse your system, you can use the common dialog box
(comdlg32.ocx) This will give you Microsoft's explorer type browsing dialog
box.

If you need the names of all the files and directories for some other
processing, you can use the dir function.

Hope this gets you going in the right direction.

--
Mark Smith
Air Gage Co.
masmith@airgage.com
0 Likes
Message 3 of 12

Anonymous
Not applicable
Mark,

Thanks for your interest. I am trying to get all of the files in the
directory that contains the active drawing and/or any and all sub
directories under that directory with no user intervention. I am already
using the dir function to get all the files in the directory containing the
active drawing (one of the options in the program). I started looking into
recursive calls with dir but it started getting very involved. A similar
post to this suggested using dos. At the time I didn't want to re-invent the
wheel so I went with the dos stuff. I would like to replace it something
thats works entirely within VBA.

Gary
0 Likes
Message 4 of 12

Anonymous
Not applicable
VBDesign has some code up at
http://vbdesign.hypermart.net/cadpages/get_file_names.htm that may get you
closer.
-- Walter -- http://www.ActiveDwg.com --

Gary McMaster wrote in message
news:ef29a66.1@WebX.SaUCah8kaAW...
> Mark,
>
> Thanks for your interest. I am trying to get all of the files in the
> directory that contains the active drawing and/or any and all sub
> directories under that directory with no user intervention. I am already
> using the dir function to get all the files in the directory containing
the
> active drawing (one of the options in the program). I started looking into
> recursive calls with dir but it started getting very involved. A similar
> post to this suggested using dos. At the time I didn't want to re-invent
the
> wheel so I went with the dos stuff. I would like to replace it something
> thats works entirely within VBA.
>
> Gary
>
0 Likes
Message 5 of 12

Anonymous
Not applicable
Gary,

Here is some code I use to accomplish this task. It you pass the function a
string containing the path you want to start with it will put all *.dwg
files below that path in a listbox. Maybe you can modify it to meet your
needs.

John

Public MyPath as String

Public Sub GetSubDirs(MyPathInput As String)
Dim NumOfDwgFiles As Integer
Dim NumOfDirectories As Integer
Dim MyName As String
Dim i As Integer, n As Integer
Dim StartDirectory As String

'Assign the MyPath variable. If we are in the base directory (eg, C:\) then
dont append '\'
If Right(MyPathInput, 1) <> "\" Then MyPath = MyPathInput & "\" Else MyPath
= MyPathInput
StartDirectory = MyPath

'Find the number of directories in MyPath
i = 0
MyName = Dir(MyPath, vbDirectory)
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
i = i + 1
End If
End If
MyName = Dir
Loop
NumOfDirectories = i

If NumOfDirectories > 0 Then
ReDim dirs(0 To NumOfDirectories - 1) As String
i = 0
'Populate the dirs array with the names of directories in MyPath
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
If MyName <> "." And MyName <> ".." Then
'Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
dirs(i) = MyPath & MyName ' Store entry only if it
represents a directory.
i = i + 1
End If
End If
MyName = Dir ' Get next entry.
Loop
End If

'Find the number of DWG files in MyPath
NumOfDwgFiles = 0
i = 0
MyName = Dir(MyPath + "*.dwg", vbNormal) 'Tell the Dir command what to
look for
Do While MyName <> "" 'Counts the number of files in this directory that
fit above description
MyName = Dir
i = i + 1
Loop
NumOfDwgFiles = i

If NumOfDwgFiles > 0 Then
ReDim files(0 To NumOfDwgFiles - 1) As String 'An array to store the
names of the files
i = 0
' Populate the file array with the names of files in MyPath
MyName = Dir(MyPath + "*.dwg", vbNormal) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
files(i) = MyPath & MyName
MyName = Dir ' Get next entry.
i = i + 1
Loop
'We are adding all DWG files to ListBox2
For i = 0 To (NumOfDwgFiles - 1)
ListBox2.AddItem files(i)
Next i
End If

'Call the GetSubDirs routine (recursivly) for each subdirectory in MyPath.
The base case (when
'the function returns to the routine that called it) is when the For loop is
finished
For n = 0 To NumOfDirectories - 1
GetSubDirs (dirs(n))
Next n

MyPath = StartDirectory
Exit Sub

End Sub
"Gary McMaster" wrote in message
news:ef29a66.1@WebX.SaUCah8kaAW...
> Mark,
>
> Thanks for your interest. I am trying to get all of the files in the
> directory that contains the active drawing and/or any and all sub
> directories under that directory with no user intervention. I am already
> using the dir function to get all the files in the directory containing
the
> active drawing (one of the options in the program). I started looking into
> recursive calls with dir but it started getting very involved. A similar
> post to this suggested using dos. At the time I didn't want to re-invent
the
> wheel so I went with the dos stuff. I would like to replace it something
> thats works entirely within VBA.
>
> Gary
>
0 Likes
Message 6 of 12

Anonymous
Not applicable
Gary,

Here is what I use in a project that allows me to browse for a file:



Private Sub cmdBrowse_Click()

'This sub will open the common dialog box and enable the user to browse for
a file
'It will then set the browse file flag to true so the cmdInsert sub will
know that
'it will need to process the file name differently (It will not be able to
access the
'record set for the file name.)

With frmCatalog.comDlg
.DefaultExt = "dwg"
If myRs.Fields("Item Type").Value = "TABLE" Then
.InitDir = CatalogPath
Else
.InitDir = CatalogPath & myRs.Fields("directory").Value
End If

.ShowOpen
BrowseFileName = .Filename
End With

If BrowseFileName = "*.dwg" Then 'if there is not a valid file name then
the cancel button was pressed
BrowseFile = False
Exit Sub 'no further action required
Else
DwgVer = ChkVer(BrowseFileName) 'check the drawing version

With frmCatalog.ocxVView
If DwgVer <> "R12" Then
.src = BrowseFileName
.Visible = True
frmCatalog.imgAirGageLogo.Visible = False
End If

End With
frmCatalog.cmdInsert.Enabled = True 'enable the insert button
BrowseFile = True
lstPartNum.Clear
End If

End Sub



You will have to make a reference to the comdlg32.ocx in your project and
then place the control on your form. Make the visible property false so
that the user will not see it.

Set the initial directory property prior to showing the dialog so that it
comes up in the same directory as your active drawing

ComDlg.InitialDir = Thisdrawing.Path 'ComDlg is what I named the
control for this project.
'
Make sure you Name it what you want it to be.

From here you can browse all over your system. After you hit the ok button,
you can retrieve the file name(s) from the FileName property. If you have
the multilpeSelect property set to true, you may have to parse thru the
string to seperate the file names.

I hope this is of some help.

--
Mark Smith
Air Gage Co.
masmith@airgage.com
0 Likes
Message 7 of 12

Anonymous
Not applicable
Gary have you looked at the dir function? I built a win16 type of file list
using it (no common dialog). Works pretty good.

phix

Mark Smith wrote in message
news:ef29a66.4@WebX.SaUCah8kaAW...
> Gary,
>
> Here is what I use in a project that allows me to browse for a file:
>
>
>
> Private Sub cmdBrowse_Click()
>
> 'This sub will open the common dialog box and enable the user to browse
for
> a file
> 'It will then set the browse file flag to true so the cmdInsert sub will
> know that
> 'it will need to process the file name differently (It will not be able to
> access the
> 'record set for the file name.)
>
> With frmCatalog.comDlg
> .DefaultExt = "dwg"
> If myRs.Fields("Item Type").Value = "TABLE" Then
> .InitDir = CatalogPath
> Else
> .InitDir = CatalogPath & myRs.Fields("directory").Value
> End If
>
> .ShowOpen
> BrowseFileName = .Filename
> End With
>
> If BrowseFileName = "*.dwg" Then 'if there is not a valid file name
then
> the cancel button was pressed
> BrowseFile = False
> Exit Sub 'no further action required
> Else
> DwgVer = ChkVer(BrowseFileName) 'check the drawing version
>
> With frmCatalog.ocxVView
> If DwgVer <> "R12" Then
> .src = BrowseFileName
> .Visible = True
> frmCatalog.imgAirGageLogo.Visible = False
> End If
>
> End With
> frmCatalog.cmdInsert.Enabled = True 'enable the insert button
> BrowseFile = True
> lstPartNum.Clear
> End If
>
> End Sub
>
>
>
> You will have to make a reference to the comdlg32.ocx in your project and
> then place the control on your form. Make the visible property false so
> that the user will not see it.
>
> Set the initial directory property prior to showing the dialog so that it
> comes up in the same directory as your active drawing
>
> ComDlg.InitialDir = Thisdrawing.Path 'ComDlg is what I named the
> control for this project.
>
'
> Make sure you Name it what you want it to be.
>
> From here you can browse all over your system. After you hit the ok
button,
> you can retrieve the file name(s) from the FileName property. If you have
> the multilpeSelect property set to true, you may have to parse thru the
> string to seperate the file names.
>
> I hope this is of some help.
>
> --
> Mark Smith
> Air Gage Co.
> masmith@airgage.com
>
0 Likes
Message 8 of 12

Anonymous
Not applicable
Thanks Everyone. I can use all of these samples in one place or another.
John, I managed to get the code you posted running and printing to debug.
Looks like just what the doctor ordered. Now I don't have to put a password
on the file so no one can se my feeble and "Micky Mouse" attempt.

Thanks again,

Gary
0 Likes
Message 9 of 12

Anonymous
Not applicable
The Common Dialog Control is not usable in VBA unless the local machine also
has a Microsoft development product like VB installed on it.

--
Attitudes are contagious. Is yours worth catching?
http://www.acadx.com

Mark Smith wrote in message
news:ef29a66.0@WebX.SaUCah8kaAW...
> If you want to browse your system, you can use the common dialog box
> (comdlg32.ocx) This will give you Microsoft's explorer type browsing
dialog
> box.
0 Likes
Message 10 of 12

Anonymous
Not applicable
Thanks Frank. My machine is the only one with VB6 installed. I will have to
be more careful in choosing controls in the future.

Gary
0 Likes
Message 11 of 12

Anonymous
Not applicable
For an excellent replacement that is free, redistributable and comes with
the source code take a look at the CommonDialog Replacement DLL over at
http://vbaccelerator.com

I actually refer it to the CommonDialog Control since it offers support for
dialog positioning, a built-in method for parsing multiple file selections,
the ability to use diaolg templates and much, much more. While you're there,
be sure to look around. The author has some amazing controls and dlls and
they're all free!

--
Attitudes are contagious. Is yours worth catching?
http://www.acadx.com

Gary McMaster wrote in message
news:ef29a66.8@WebX.SaUCah8kaAW...
> Thanks Frank. My machine is the only one with VB6 installed. I will have
to
> be more careful in choosing controls in the future.
>
> Gary
>
0 Likes
Message 12 of 12

Anonymous
Not applicable
Thanks for the tip Frank, I'll check it out.

Gary
0 Likes