Saturday, March 24, 2012

loop thru directory

I'm trying to find out how to loop thru all subdirectories from a root path, as well as all files in each directory.

How can I do this ?? I can't find any code on google

thanks alot for any help

mike123little bit o recursion, not tested a whole bunch so use at your own risk...

JD

<%@. Page Language="C#" %
<script runat="server"
protected void Page_Load(object sender, EventArgs e)
{
listDirs("C:\\dos", " ");
}

public void listDirs(string dirIn, string strt)
{
System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(dirIn);
Response.Write(strt + d.Name + "(DIR)<br>");

string[] subDirs = System.IO.Directory.GetDirectories(dirIn);
string[] files = System.IO.Directory.GetFiles(dirIn);

foreach (string file in files)
{

System.IO.FileInfo f = new System.IO.FileInfo(file);
Response.Write(strt + strt + f.Name + "(FILE)<br>");
}

foreach (string dir in subDirs)
{

listDirs(dir, strt + strt);

}
}

</script
<html>
<body>
<form id="form1" runat="server">
<div
</div>
</form>
</body>
</html


sorry, i should have included this in my post .. do you have it in vb.net ?

I need to then iterate thru each item in that list

hopefully you can help me with this

thanks very much

mike123
Here ya go - we already are iterating over them.

HTH

JD


<%@. Page Language="VB" %>
<script runat="server">
Public Sub listDirs(ByVal dirIn As String, ByVal strt As String)
Dim d As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(dirIn)
Response.Write(strt + d.Name + "(DIR)<br>")
Dim subDirs As String() = System.IO.Directory.GetDirectories(dirIn)
Dim files As String() = System.IO.Directory.GetFiles(dirIn)

Dim file As String
For Each file In files
Dim f As System.IO.FileInfo = New System.IO.FileInfo(file)
Response.Write(strt + strt + f.Name + "(FILE)<br>")
Next

Dim dir As String
For Each dir In subDirs
listDirs(dir, strt + strt)
Next
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
listDirs("C:\dos", " ")
End Sub
</script>
<html>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>

0 comments:

Post a Comment