Need to create table of all folders and filenames within master fo

R

Ron Carr

I have 100 GB of music on my hard drive. Would like to back up to DVD but
some filenames are too long.
Therefore I would like to get all filenames within my "Music" folder into a
table so I can calculate lengths and trim where I have to.
Any tips or references to such code?
Many thanks!
Ron
 
T

Tom van Stiphout

On Mon, 26 Oct 2009 06:31:01 -0700, Ron Carr

Google for "Dir Recursive VBA" and you should have no problem finding
references.

I don't think filenames on DVD must be shorter than filenames on HD.

-Tom.
Microsoft Access MVP
 
D

Daniel Pineault

Here a modified it for you:

Function GetDirListing(strPath As String, Optional strFilter As String)
' Procedure : GetDirListing
' Author : CARDA Consultants Inc.
' Website : http:\\www.cardaconsultants.com
' Purpose : List all the files within a directory
' Copyright : The following may be altered and reused as you wish so long as
the
' copyright notice is left unchanged (including Author, Website
and
' Copyright). It may not be sold/resold or reposted on other
sites (links
' back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' strPath = full path include trailing \ ie:"c:\windows\"
' strFilter = extension of files ie:"pdf". if you want to return
' a complete listing of all the files enter a value of
' "*" as the strFilter
'
Dim MyFile As String
Dim MyFolder As String

Dim Db As Database
Dim rs As Recordset
Dim tblName As String
Dim tblFilenameField As String

'On Error GoTo Error_Handler

tblName = "MyMusicFiles" 'Table name to add files listing to
tblFilenameField = "FileName" 'Field in the table for the filename
Set Db = CurrentDb
Set rs = Db.OpenRecordset(tblName)

MyFolder = strPath

'Add the trailing \ if it was omitted
If Right(MyFolder, 1) <> "\" Then MyFolder = MyFolder & "\"
'Modify the strFilter to include all files if omitted in the function
'call
If strFilter = "" Then strFilter = "*"

'Loop through all the files in the directory by using Dir$ function
MyFile = Dir$(MyFolder & "*." & strFilter, vbNormal + vbHidden + vbSystem)
Do While MyFile <> ""
'FileCopy SourceFile, DestinationFile
rs.AddNew
rs(tblFilenameField) = MyFile
rs.Update
MyFile = Dir$
Loop

rs.Close
Set rs = Nothing
Set Db = Nothing

Exit Function

Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & _
Err.Number & vbCrLf & "Error Source: GetDirListing" & vbCrLf & "Error
Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Exit Function

End Function

you'd run it by using a command like

GetDirListing("C:\Documents and Settings\All Users\Documents\My Music\Sample
Music")

Don't forget to switch the

tblName
tblFilenameField

variables to suit your table name and structure!
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.
 

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