|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Loop thru all subfolders and list all files under eachI need a VB routine to loop thru a select top folder to find all subfolders
and list all subfolders/files under each of these subfolders. Any help is greatly appreciated. Bill "Bill Nguyen" <billn_nospam_please@jaco.com> schrieb: <URL:http://dotnet.mvps.org/dotnet/samples/filesystem/FileSystemEnumerator.zip>>I need a VB routine to loop thru a select top folder to find all subfolders >and list all subfolders/files under each of these subfolders. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> I appreciated this but I really don't know where to start. The readme.txt
doesn't tell me a thing about how to get this going. Any thing I have to do in order to get the demo running? Bill Show quoteHide quote "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message news:%232Bbp2mfFHA.484@TK2MSFTNGP14.phx.gbl... > "Bill Nguyen" <billn_nospam_please@jaco.com> schrieb: >>I need a VB routine to loop thru a select top folder to find all >>subfolders and list all subfolders/files under each of these subfolders. > > <URL:http://dotnet.mvps.org/dotnet/samples/filesystem/FileSystemEnumerator.zip> > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://classicvb.org/petition/> "Bill Nguyen" <billn_nospam_please@jaco.com> schrieb: I see, my sample is a bit "oversized". The listing below shows a reduced >I appreciated this but I really don't know where to start. The readme.txt >doesn't tell me a thing about how to get this going. > Any thing I have to do in order to get the demo running? version: \\\ Imports System.IO Friend Module Module1 Friend Sub Main() EnumerateDirectory("C:\WINDOWS") End Sub Private Sub EnumerateDirectory(ByVal RootDirectory As String) For Each s As String In Directory.GetFiles(RootDirectory) Console.WriteLine("File found: " & s) Next s For Each s As String In Directory.GetDirectories(RootDirectory) ' Prevent enumeration of reparse points. ' ' Platform SDK: Storage -- Reparse Points ' <URL:http://msdn.microsoft.com/library/en-us/fileio/base/reparse_points.asp> ' ' You can create an infinitely recursive directory tree ' <URL:http://blogs.msdn.com/oldnewthing/archive/2004/12/27/332704.aspx> If _ Not (File.GetAttributes(s) And FileAttributes.ReparsePoint) = _ FileAttributes.ReparsePoint _ Then Console.WriteLine("Directory found: " & s) EnumerateDirectory(s) End If Next s End Sub End Module /// -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> herfried;
I can't run any of your code in your application. It gave me some kind of error! Below is my newbie version but all it can do is to list all files in a single-level directory. How can I loop thru all directories to list files in lowest level folder and move to the next? Thanks a billion Bill Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim mDir, mFile, rootDir, mrootPath, mFolder, mFileName As String Dim mCount As Integer rootDir = "C:\windows" ' For Each s As String In Directory.GetFiles(rootDir) For Each d As String In Directory.GetDirectories(rootDir) 'Console.WriteLine("File found: " & s) 'mSourceFile = .FileName.Substring(OpenFileDialog1.FileName.LastIndexOf("\") + 1) mFolder = d.Substring(d.LastIndexOf("\") + 1) mrootPath = d.Substring(0, d.LastIndexOf("\")) mDir += mrootPath & "\" & mFolder & vbCrLf For Each f As String In Directory.GetFiles(d) mFileName = f.Substring(f.LastIndexOf("\") + 1) 'mrootPath = f.Substring(1, d.LastIndexOf("\")) mFile += mFileName & vbCrLf Next f MsgBox(mFile) mFile = "" Next d MsgBox(mDir) End Sub Show quoteHide quote "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message news:OctpkmnfFHA.3904@TK2MSFTNGP14.phx.gbl... > "Bill Nguyen" <billn_nospam_please@jaco.com> schrieb: >>I appreciated this but I really don't know where to start. The readme.txt >>doesn't tell me a thing about how to get this going. >> Any thing I have to do in order to get the demo running? > > I see, my sample is a bit "oversized". The listing below shows a reduced > version: > > \\\ > Imports System.IO > > Friend Module Module1 > Friend Sub Main() > EnumerateDirectory("C:\WINDOWS") > End Sub > > Private Sub EnumerateDirectory(ByVal RootDirectory As String) > > For Each s As String In Directory.GetFiles(RootDirectory) > Console.WriteLine("File found: " & s) > Next s > For Each s As String In Directory.GetDirectories(RootDirectory) > > ' Prevent enumeration of reparse points. > ' > ' Platform SDK: Storage -- Reparse Points > ' > <URL:http://msdn.microsoft.com/library/en-us/fileio/base/reparse_points.asp> > ' > ' You can create an infinitely recursive directory tree > ' > <URL:http://blogs.msdn.com/oldnewthing/archive/2004/12/27/332704.aspx> > If _ > Not (File.GetAttributes(s) And FileAttributes.ReparsePoint) > = _ > FileAttributes.ReparsePoint _ > Then > Console.WriteLine("Directory found: " & s) > EnumerateDirectory(s) > End If > Next s > End Sub > End Module > /// > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://classicvb.org/petition/> "Bill Nguyen" <billn_nospam_please@jaco.com> schrieb: Error message?> I can't run any of your code in your application. It gave me some kind of > error! -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> The whole purpose of Herfried's method was that it is a *recursive*
method, i. e. it calls itself. But you have removed the recursion and therefore it only scans the first directory. Here is Herfried's method again (slightly altered): Imports System.IO Friend Module Module1 Friend Sub Main() EnumerateDirectory("C:\WINDOWS") End Sub Private Sub EnumerateDirectory(ByVal RootDirectory As String) For Each s As String In Directory.GetFiles(RootDirectory) '******* Perform task on file s here. Replace with your own file '******* manipulation code here Next For Each s As String In Directory.GetDirectories(RootDirectory) 'Correct me if I'm wrong Herfried but this line prevents recursion into 'empty folders If Not (File.GetAttributes(s) And FileAttributes.ReparsePoint) = _ FileAttributes.ReparsePoint Then '***** Perform work in folder s here. Replace with your '***** folder manipuation code here 'Notice we're calling ourself here to get data on child folders 'This is the recursive part! EnumerateDirectory(s) End If Next End Sub End Module BTW: Why are you using substring parse out the portions of your folders and filename? why not use the methods in the System.IO namespace? For example, instead of: mrootPath = d.Substring(0, d.LastIndexOf("\")) use this instead: mrootPath = Path.GetPathRoot(d) and instead of mFileName = f.Substring(f.LastIndexOf("\") + 1) use this instead: mFileName = Path.GetFileName(f) Chris;
Thanks for the tip. mFileName = Path.GetFileName(f) worked great! mrootPath = Path.GetPathRoot(d) only give me the root path. I need to get the name of the folder at subsequent levels. For example, the full path: C:\Base\Level1\level2\level3\Level4\Filename.TXT C:\Base is the base (root) dir. I need to parse the folder name Level1 thru level4 and assign a corresponding folder level ID 2,3,4) to these folders. Any idea how to do this? Thanks in advance. Bill Show quoteHide quote "Chris Dunaway" <dunaw***@gmail.com> wrote in message news:1120573434.513242.37210@z14g2000cwz.googlegroups.com... > The whole purpose of Herfried's method was that it is a *recursive* > method, i. e. it calls itself. But you have removed the recursion and > therefore it only scans the first directory. > > Here is Herfried's method again (slightly altered): > > Imports System.IO > > Friend Module Module1 > Friend Sub Main() > EnumerateDirectory("C:\WINDOWS") > End Sub > > Private Sub EnumerateDirectory(ByVal RootDirectory As String) > > For Each s As String In Directory.GetFiles(RootDirectory) > '******* Perform task on file s here. Replace with your > own file > '******* manipulation code here > Next > > For Each s As String In Directory.GetDirectories(RootDirectory) > > 'Correct me if I'm wrong Herfried but this line prevents > recursion into > 'empty folders > If Not (File.GetAttributes(s) And > FileAttributes.ReparsePoint) = _ > FileAttributes.ReparsePoint Then > > '***** Perform work in folder s here. Replace with > your > '***** folder manipuation code here > > 'Notice we're calling ourself here to get data on child > folders > 'This is the recursive part! > EnumerateDirectory(s) > End If > Next > End Sub > End Module > > > BTW: Why are you using substring parse out the portions of your > folders and filename? why not use the methods in the System.IO > namespace? > > For example, instead of: > > mrootPath = d.Substring(0, d.LastIndexOf("\")) > > use this instead: > > mrootPath = Path.GetPathRoot(d) > > and instead of > > mFileName = f.Substring(f.LastIndexOf("\") + 1) > > use this instead: > > mFileName = Path.GetFileName(f) > > Do something like this (one I prepared earlier... you don't need to use XML. > C:\Base\Level1\level2\level3\Level4\Filename.TXT > C:\Base is the base (root) dir. > I need to parse the folder name Level1 thru level4 and assign a > corresponding folder level ID 2,3,4) to these folders. > > Any idea how to do this? > Thanks in advance. To add the folder level counter add a line to increment a class-level counter inside the recursion loop and include its value in the output: Private Function GetFiles(ByRef XmlDoc As XmlDocument, ByRef ParentNode As XmlElement, ByVal DirPath As String, Optional ByVal IncludeSubFolders As Boolean = True) As XmlNode Dim objFileInfo As FileInfo Dim objDir As DirectoryInfo = New DirectoryInfo(DirPath) Dim objSubFolder As DirectoryInfo 'add a node for each file For Each objFileInfo In objDir.GetFiles() Dim MyFileNode As XmlElement = XmlDoc.CreateElement("Node") MyFileNode.SetAttribute("type", "file") MyFileNode.SetAttribute("id", intCount.ToString) intCount += 1 MyFileNode.SetAttribute("type", objFileInfo.Extension) MyFileNode.SetAttribute("size", objFileInfo.Length.ToString) MyFileNode.SetAttribute("text", objFileInfo.Name) ParentNode.AppendChild(MyFileNode) Next 'call recursively to do sub folders 'if you don't want this set optional 'parameter to false 'add the folder node If IncludeSubFolders Then For Each objSubFolder In objDir.GetDirectories() Dim DirNode As XmlElement = XmlDoc.CreateElement("Node") DirNode.SetAttribute("type", "folder") DirNode.SetAttribute("id", intCount.ToString) intCount += 1 DirNode.SetAttribute("text", objSubFolder.Name) ParentNode.AppendChild(DirNode) GetFiles(XmlDoc, DirNode, objSubFolder.FullName, True) Next End If Return ParentNode End Function Stuart;
Sorry for my very late response. I'd been away for a long period of time. Thanks for your help! Bill Show quoteHide quote "Stuart Irving" <Stuart Irv***@discussions.microsoft.com> wrote in message news:62787F53-8003-47C9-9378-150FF6907D11@microsoft.com... > >> >> C:\Base\Level1\level2\level3\Level4\Filename.TXT >> C:\Base is the base (root) dir. >> I need to parse the folder name Level1 thru level4 and assign a >> corresponding folder level ID 2,3,4) to these folders. >> >> Any idea how to do this? >> Thanks in advance. > > > Do something like this (one I prepared earlier... you don't need to use > XML. > To add the folder level counter add a line to increment a class-level > counter > inside the recursion loop and include its value in the output: > > Private Function GetFiles(ByRef XmlDoc As XmlDocument, ByRef ParentNode > As XmlElement, ByVal DirPath As String, Optional ByVal IncludeSubFolders > As > Boolean = True) As XmlNode > > Dim objFileInfo As FileInfo > Dim objDir As DirectoryInfo = New DirectoryInfo(DirPath) > Dim objSubFolder As DirectoryInfo > > 'add a node for each file > For Each objFileInfo In objDir.GetFiles() > Dim MyFileNode As XmlElement = XmlDoc.CreateElement("Node") > MyFileNode.SetAttribute("type", "file") > MyFileNode.SetAttribute("id", intCount.ToString) > intCount += 1 > MyFileNode.SetAttribute("type", objFileInfo.Extension) > MyFileNode.SetAttribute("size", objFileInfo.Length.ToString) > MyFileNode.SetAttribute("text", objFileInfo.Name) > ParentNode.AppendChild(MyFileNode) > Next > > 'call recursively to do sub folders > 'if you don't want this set optional > 'parameter to false > > 'add the folder node > If IncludeSubFolders Then > For Each objSubFolder In objDir.GetDirectories() > Dim DirNode As XmlElement = XmlDoc.CreateElement("Node") > DirNode.SetAttribute("type", "folder") > DirNode.SetAttribute("id", intCount.ToString) > intCount += 1 > DirNode.SetAttribute("text", objSubFolder.Name) > ParentNode.AppendChild(DirNode) > GetFiles(XmlDoc, DirNode, objSubFolder.FullName, True) > Next > End If > > Return ParentNode > > End Function |
|||||||||||||||||||||||