List All Files and SubFolders Inside Folder (Any depth level

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
10 posts Page 1 of 1
Contributors
User avatar
smit
New Member
New Member
Posts: 12
Joined: Sat Mar 24, 2012 4:25 am

I have visual basic 2010 express edition

I google the code for this condition

& I found following code
Code: Select all
Dim fso
Dim ObjOutFile
 
'Creating File System Object
Set fso = CreateObject("Scripting.FileSystemObject")
 
'Create an output file
Set ObjOutFile = fso.CreateTextFile("OutputFiles.csv")
 
'Writing CSV headers
ObjOutFile.WriteLine("Type,File Name,File Path")
 
'Call the GetFile function to get all files
GetFiles("C:\Windows\Help")
 
'Close the output file
ObjOutFile.Close
 
WScript.Echo("Completed")
 
Function GetFiles(FolderName)
    On Error Resume Next
     
    Dim ObjFolder
    Dim ObjSubFolders
    Dim ObjSubFolder
    Dim ObjFiles
    Dim ObjFile
 
    Set ObjFolder = fso.GetFolder(FolderName)
    Set ObjFiles = ObjFolder.Files
     
    'Write all files to output files
    For Each ObjFile In ObjFiles
        ObjOutFile.WriteLine("File," & ObjFile.Name & "," & ObjFile.Path)
    Next
     
    'Getting all subfolders
    Set ObjSubFolders = ObjFolder.SubFolders
     
    For Each ObjFolder In ObjSubFolders
        'Writing SubFolder Name and Path
        ObjOutFile.WriteLine("Folder," & ObjFolder.Name & "," & ObjFolder.Path)
         
        'Getting all Files from subfolder
        GetFiles(ObjFolder.Path)
    Next
     
End Function
But it is not working for me

Any 1 can solve this ??
hack.forummotions.com
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Code: Select all
Public Sub ListFolder(ByVal Parent As DirectoryInfo)
   For Each SubFolder As DirectoryInfo In Parent.GetFolders()
      Console.WriteLine(SubFolder.FullName) 'Write the full path of the folder to the console'
      ListFolder(SubFolder)  'Loop through each folder in a recursive way'
   Next

   For Each File As FileInfo In Parent.GetFiles()
       Console.WriteLine(File.FullName) 'Write the full path of the file to the console'
   Next
End Sub
You must import System.IO
To start the function, you can for example call
Code: Select all
ListFolders(New DirectoryInfo("C:\Users"))
to list all the contents of the user (and all subfolders) folder.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
smit
New Member
New Member
Posts: 12
Joined: Sat Mar 24, 2012 4:25 am

Thanx for reply

but I m still confused

I created the Public Sub as u told me

But How to run that ??
hack.forummotions.com
User avatar
smit
New Member
New Member
Posts: 12
Joined: Sat Mar 24, 2012 4:25 am

I actually want something like follow

When button get click it should go to the path which is present in textbox1.text

grab all folders , files , subfolder , subfiles and store them in 1 text file (c:/temp/test1.txt)
hack.forummotions.com
User avatar
smit
New Member
New Member
Posts: 12
Joined: Sat Mar 24, 2012 4:25 am

Code: Select all
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim directory = "E:\Torrent"
        Dim files() As System.IO.FileInfo
        Dim dirinfo As New System.IO.DirectoryInfo(directory)
        files = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
        For Each File In files

            ListBox1.Items.Add(File)
        Next
    End Sub
Please tell me code in place of ListBox1.Items.Add(File)

to add that file name in c:/test1.txt file
hack.forummotions.com
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

smit, please try avoiding multiple posts as it spams the topic.
Image
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Okay, try this:
Code: Select all
    Public Sub ListFolder(ByVal Parent As DirectoryInfo, outputFile As StreamWriter)
        For Each SubFolder As DirectoryInfo In Parent.GetDirectories()
            outputFile.WriteLine(String.Format("Folder,{0},{1}", SubFolder.Name, SubFolder.Parent.FullName))
            ListFolder(SubFolder, outputFile)  'Loop through each folder in a recursive way'
        Next

        For Each File As FileInfo In Parent.GetFiles()
            outputFile.WriteLine(String.Format("File,{0},{1}", File.Name, File.Directory.FullName))
        Next
    End Sub
To run it, use this:
Code: Select all
        Dim writer As New StreamWriter("C:\file.csv", False) 'File for writing'
        writer.WriteLine("Type,File Name,File Path") 'Write CSV headers'
        ListFolder(New DirectoryInfo("C:\Program Files"), writer) 'Write the contents of Program Files to C:\file.csv'
        writer.Close()
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
smit
New Member
New Member
Posts: 12
Joined: Sat Mar 24, 2012 4:25 am

Thanx Working great
hack.forummotions.com
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

You've posted this in the wrong section but if you don't need help anymore be sure to lock the topic :)
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
smit
New Member
New Member
Posts: 12
Joined: Sat Mar 24, 2012 4:25 am

smashapps wrote:
You've posted this in the wrong section but if you don't need help anymore be sure to lock the topic :)
How to lock this topic (The lock topic button is not appearing)
hack.forummotions.com
10 posts Page 1 of 1
Return to “Tutorials”