Get Folder File List (1 Viewer)

ivandgreat

Registered User.
Local time
Today, 15:52
Joined
Sep 18, 2012
Messages
30
Hi,

I wanted to get the File List in a specified folder and put it in a listbox but i dont know how to do it, does anyone have the code?

br,

:)
 

pr2-eugin

Super Moderator
Local time
Today, 23:52
Joined
Nov 30, 2011
Messages
8,494
Have you tried Googling it? I just did and found this.. Hope this helps.. I have also modified to set it inside the Listbox (highlighted in Blue).. Just change the name..
Code:
Public Function AllFiles(ByVal FullPath As String) As String
[COLOR=SeaGreen]'***************************************************
'PURPOSE: Returns all files in a folder using
'the FileSystemObject

'PARAMETER: FullPath = FullPath to folder for
'which you want all files

'RETURN VALUE: Places content of file into the Listbox called 'List2'

'REQUIRES: Reference to Micrsoft Scripting
'          Runtime

'EXAMPLE:

[B][COLOR=RoyalBlue]'Dim sFiles() as string
'dim lCtr as long
'sFiles = AllFiles("C:\Windows\System")
'For lCtr = 0 to Ubound(sFiles)
'  Me.List2.AddItem (sfiles(lctr))
'Next[/COLOR][/B]
'************************************************[/COLOR]

Dim oFs As New FileSystemObject
Dim sAns() As String
Dim oFolder As Folder
Dim oFile As File
Dim lElement As Long

ReDim sAns(0) As String
If oFs.FolderExists(FullPath) Then
    Set oFolder = oFs.GetFolder(FullPath)
 
    For Each oFile In oFolder.Files
      lElement = IIf(sAns(0) = "", 0, lElement + 1)
      ReDim Preserve sAns(lElement) As String
      sAns(lElement) = oFile.Name
    Next
End If

AllFiles = sAns
ErrHandler:
    Set oFs = Nothing
    Set oFolder = Nothing
    Set oFile = Nothing
End Function
I did not do the coding.. Found the code at : http://www.freevbcode.com/ShowCode.asp?ID=1487
 

ivandgreat

Registered User.
Local time
Today, 15:52
Joined
Sep 18, 2012
Messages
30
Thanks, but how should i use this in access form?
 

pr2-eugin

Super Moderator
Local time
Today, 23:52
Joined
Nov 30, 2011
Messages
8,494
Okay.. I do not know where do you want to call this function, or where you would like to gather the data as none of this is mentioned in your initial query, but MY GUESS is that you have an unbound ListBox (List2) that holds the information, so based on the assumption here it goes..
STEP 1: Open a Module.
STEP 2: Copy and paste it inside the new module
STEP 3: Give the module a name and save it.
STEP 4: In the Form_Current method, put the example code I have placed.. something like..
Code:
Private Sub Form_Current()
    Dim sFiles() as string
    Dim lCtr as long
    sFiles = AllFiles("C:\Windows\System")    [COLOR=SeaGreen]'Whatever path you have in mind to open[/COLOR]
    For lCtr = 0 to Ubound(sFiles)
      Me.List2.AddItem (sfiles(lctr))
    Next
End Sub

Hope this helps, If you need further assistance. Please explain more on what you want to do.
 

ivandgreat

Registered User.
Local time
Today, 15:52
Joined
Sep 18, 2012
Messages
30
Okay.. I do not know where do you want to call this function, or where you would like to gather the data as none of this is mentioned in your initial query, but MY GUESS is that you have an unbound ListBox (List2) that holds the information, so based on the assumption here it goes..
STEP 1: Open a Module.
STEP 2: Copy and paste it inside the new module
STEP 3: Give the module a name and save it.
STEP 4: In the Form_Current method, put the example code I have placed.. something like..
Code:
Private Sub Form_Current()
    Dim sFiles() as string
    Dim lCtr as long
    sFiles = AllFiles("C:\Windows\System")    [COLOR=SeaGreen]'Whatever path you have in mind to open[/COLOR]
    For lCtr = 0 to Ubound(sFiles)
      Me.List2.AddItem (sfiles(lctr))
    Next
End Sub

Hope this helps, If you need further assistance. Please explain more on what you want to do.

Thanks for the code, i will try it.

Actually i wanted to list down all files located in my folder, then try to open it when doubled click.

What will i add if i wanted to open when double click it?

br,
 

pr2-eugin

Super Moderator
Local time
Today, 23:52
Joined
Nov 30, 2011
Messages
8,494
Thanks for the code, i will try it.
You will try it after 6 months :eek:
Actually i wanted to list down all files located in my folder,
This is what the code would have done, and I hope so far you have been able to get this..
then try to open it when doubled click.
in the double click event, add the code to do that, I think a simple
Code:
[URL="http://msdn.microsoft.com/en-us/library/office/aa221236%28v=office.11%29.aspx"]Application.FollowHyperlink[/URL](Me.ListBoxName)
Should do the trick.. However I have not tested this.. Let me know how it goes.. Good luck !!
 

MyTech

Access VBA
Local time
Today, 18:52
Joined
Jun 10, 2010
Messages
108
You will try it after 6 months :eek:
This is what the code would have done, and I hope so far you have been able to get this.. in the double click event, add the code to do that, I think a simple
Code:
[URL="http://msdn.microsoft.com/en-us/library/office/aa221236%28v=office.11%29.aspx"]Application.FollowHyperlink[/URL](Me.ListBoxName)
Should do the trick.. However I have not tested this.. Let me know how it goes.. Good luck !!

He actually wanted to open in Access (inside a form) an actual Windows Explorer window where he can just double click any type of file and Windows Explorer will open it with the relevant program.
(Of course he will try it not before 6 months. Because he saw that you don't understand his needs and just slapped in his face.. :eek:)
 

pr2-eugin

Super Moderator
Local time
Today, 23:52
Joined
Nov 30, 2011
Messages
8,494
Not sure what your problem is MyTech.. As you revived a 4 month old thread that had nothing to do with helping out the OP nor having follow up questions..

As you say,
He actually wanted to open in Access (inside a form) an actual Windows Explorer window where he can just double click any type of file and Windows Explorer will open it with the relevant program.
This was not an established requirement until the third post..

So not sure what you mean by..
Because he saw that you don't understand his needs and just slapped in his face.. :eek:
The initial post was..
I wanted to get the File List in a specified folder and put it in a listbox but i dont know how to do it, does anyone have the code?
No information on where the OP wanted it, what he/she has done so far.. With limited information, we can only take wild guesses..

Even after providing a way of opening it with double click the OP has not responded if it worked or if he/she is stuck.. So please before responding to a thread that is totally not related to you.. I would advice you to please understand what's been done/provided.. :rolleyes: Good day..
 

boblarson

Smeghead
Local time
Today, 15:52
Joined
Jan 12, 2001
Messages
32,059
He actually wanted to open in Access (inside a form) an actual Windows Explorer window where he can just double click any type of file and Windows Explorer will open it with the relevant program.
(Of course he will try it not before 6 months. Because he saw that you don't understand his needs and just slapped in his face.. :eek:)
1. Your post does nothing but attack someone.

2. If you don't have anything to add, please stay out of it.

3. If you have an issue with what someone posted, send them a PM or contact a moderator (if you feel it is serious).

There was nothing wrong with Paul questioning why it took 3 months to respond and he did not "slap in his face."
 

MyTech

Access VBA
Local time
Today, 18:52
Joined
Jun 10, 2010
Messages
108
First of all, I'm completely sorry.
"pr2-eugin", I ask your forgiveness, and I hope to watch more my words in the future.

I see that MY words, sounded way more as a slap in the face, than YOUR words which I interpreted as a slap, in my eyes.


But let me explain:

It was very obvious that "ivandgreat" was a very new user, and, true, he was not descriptive enough.
When I saw the first part of your respond "You will try it after 6 months :eek:", I felt that this newbie got insulted by it.
I went over to his profile, and saw that he was very active here for a few weeks until that message, and he became quiet. A few days later he tried to hit another question, and as soon he got there another comment (from somebody else) saying that he is not descriptive enough, he left this site for good.

"I" think, that these comments were for him like slams which hit him out of the society on this site. but this is sure, that you definitely didn't mean it so. you were just joking to him. But powerful people must be very sensitive to others, and have to keep in mind that this newbie might feel like a fool by getting such a comment from the giant. and that is why I commented to you that he got a slap from you.

Well, I was approved guilty. My own words turn back to myself and tell me: Hey, you have to watch what you are saying or writing. You have to think how others "might" accept it.

Let's all learn together to do what's right.




I was instructed in a warning message that if I have an issue with someone, I should send them a PM. But I really believe that you all agree that I should post this apology message over here, because I think that: "If you made the mess in public, then clean it up in public".
 

Users who are viewing this thread

Top Bottom