Return all files in folder and its' sub folders

B

Brad

Thanks for taking the time to read my question.

At one point I had some code that looked in a specified folder and its' sub
folders and returned all the file names and file sizes, but I can no longer
find it. Does anyone know if someone has written something like this that I
can use?

I want to list out all the file names with folder path and file size of all
the files in a directory. For example, in My Documents.

I would also like all files in the folders within My Documents with the same
info.

Thanks so much for your help.

Brad
 
A

Allen Browne

See:
List files recursively - List files in a folder and subfolders
at:
http://allenbrowne.com/ser-59.html

Use the FileDateTime() function to get the date and time, or FileLen() to
get the size.

Careful with the size if you have enormous files (>2gb), as FileLen()
returns a signed long.
 
B

Brad

Hi Allen,

I'm having a bit of trouble with the FileLen().... well actually, I'm not
sure what to do with it.

How do I use it to get my file size?

Thanks,

Brad
 
D

DontKnow

Hi Allen ,

If I wanted to display the selected files from the list box to a text box,
what modifcations would I need to do to capture the selected directories. I
am currently able to display the number of the file using the "varitem"
function but am unable to display the actual name of the file within the
directory into a text box??

Please can you help me please?

cheers
 
B

Brad

You can use the With statement like this to write each file name to a table,
then make the control source of the dropdown list the field in the table you
just wrote to.

This is untested, but should work. Others can comment.

Sub GetFileNames()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim FileScript, FolderPath, FileSet, FileSelection

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("YourTableNameHere")
Set FileScript = CreateObject("Scripting.FileSystemObject")
Set FolderPath = FileScript.GetFolder("D:\Pictures\Florence Birmingham\")
'Path here
Set FileSet = FolderPath.Files




For Each FileSelection In FileSet

With rst
.AddNew
!FieldNameThatHoldsFileName = FolderPath & & FileSelection.Name
.Update
End With



Next


rst.close
set dbs = nothing

End Sub
 
D

DontKnow

Thanks Brad,

I was using Allens' code to produce what is in a directory and what I wanted
was to modify his code to produce was when I selected an file in the list box
that file would be displayed in the text box, I don't think that I need to
save the data to a table as your code does?

Thanks in advance for your help!!

Cheers
 
P

pietlinden

Thanks Brad,

I was using Allens' code to produce what is in a directory and what I wanted
was to modify his code to produce was when I selected an file in the listbox
that file would be displayed in the text box, I don't think that I need to
save the data to a table as your code does?

Thanks in advance for your help!!

Cheers

If all you want to do is show the value of the selected item in the
listbox in a textbox, then this is easy. One thing will screw you up,
though. If the MultiSelect property of the listbox is something other
than Simple, the ItemsSelected is a collection and not a single
value. In that case, you have to loop through the items in the
collection.

dim varItem as variant
for each varItem in me.lbxMulti.ItemsSelected
strItems=strItems & "," & lbxMulti.ItemData(varItem)
next varItem
stritems=right$(strItems,len(strItems)-1)
me.txtShowItems=strItems

Otherwise, you can refer to the control directly and do something...

me.txtShowItems=me.lbxSingle.Value
 
D

DontKnow

Hi [email protected],

Thats excellent, to enable me to just have the file name without the
fullpath info, how would I emmend your current code??

Many thanks for your input , much appreciated!!

I am learning!!

Cheers
 
Top