Forum Moderators: open

Message Too Old, No Replies

File System Object

File System Object Read Folder within folder...please help!

         

Shuvi

2:28 pm on Jan 8, 2004 (gmt 0)

10+ Year Member



I'm trying to use File System Object to get all the pages and their path in my wwwroot folder. Here's what I have so far:
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

'Get the file information
Set objFolder = objFSO.GetFolder("C:\Inetpub\wwwroot")
F = objFolder.Name
Set Folderwithin = objFSO.GetFolder(F)
Response.Write(Folderwithin.Name)

any suggestions?

korkus2000

2:34 pm on Jan 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What error are you getting or what happens?

Shuvi

3:17 pm on Jan 8, 2004 (gmt 0)

10+ Year Member



Well I changed my code a little and now I'm close then I was but not quite there yet!
Here's what I have:
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set f = objFSO.GetFolder("C:\Inetpub\wwwroot")
Set fc = f.Files
Set ff = f.SubFolders
'--- This lists the sub folders
For Each f1 in ff
Response.Write f1.name & "<br>"
Next
'--- This lists the files in the current folder
For Each f1 in fc
fname_split = split(f1.name,".")
if (fname_split(0) <> "dir") then
Response.Write sUP & f1.name & "," & fname_split(0) & "<br>"
end if
Next

Now it's printing everything in my wwwroot folder including other folders. Now I need it to read all the files within those folder and if there are folders read file in those as well.

SO I know it's just a matter of recursice loop somewhere but I have no idea how.
?

Dreamquick

4:47 pm on Jan 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What about something like this... recursive directory listing and traversal, should work pretty much anywhere as long as you don't need it to go too deep and trip out the stack.

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")  
Set objRootFolder = objFSO.GetFolder("C:\Inetpub\wwwroot")

'Start the process on the root folder 
ListFolder objRootFolder

Sub ListFolder( ByRef objFolder )

'Display the folder name 
Response.Write "Folder: " & objFolder.Name & "<br>"

'Generate a list of the files in the current folder 
Response.Write "--File list--<br>"
For Each objFile in objFolder.Files
fname_split = split(objFile.name,".")
if (fname_split(0) <> "dir") then
Response.Write sUP & objFile.name & "," & fname_split(0) & "<br>"
end if
Next
Response.Write "--File list--<br>"

'Loop through each of the sub-folders 
For Each objSubFolder in objFolder.SubFolders
ListFolder objSubFolder
Next

End Sub

aspdaddy

7:39 pm on Jan 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<snipped>
doh......diddnt see the last post

Shuvi

3:13 pm on Jan 9, 2004 (gmt 0)

10+ Year Member



Okay I tried it and it works but now I need to store it in the database:
Here's what I've got!
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<% Call PrintFolderStructure("C:\Inetpub\wwwroot") %>
</body>
</html>
<%
Sub PrintFolderStructure(path)
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(path)

Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = Application("Test")
objConn.Open
'Response.Write "<B>" & path & "</b><br>"

For Each File in objFolder.Files
'if the file extension is asp add to database
extsplit = split(File.name,".")
'This code runs for each file in a folder
displaysplit = split(path,"\")
For iLoop = LBound(displaysplit) to UBound(displaysplit)
if extsplit(1) = "asp" and (displaysplit(UBound(displaysplit))) <> "_notes" then
if iLoop = 3 then
Response.Write("<b>Display Name</b> - ")
For i = iLoop to UBound(displaysplit)
Response.Write " " & displaysplit(i)
PageName = displaysplit(i)
PageDescription = displaysplit(i)
Next
Response.Write " " & extsplit(0)

Response.Write (" <b>File Name</b> - ") & (File.Name) & "<BR>"
'strQuery = "INSERT INTO WebPages (PageName, Description, URL, FromDate) VALUES "
'strQuery = strQuery & "(""" & PageName & """, """ & PageDescription & """, """ & URL & """, #" & now() & "#);"
'on error resume next
'objConn.Execute strQuery
'if err<>0 then
'response.Write(err.description)
'else
'response.Write("Record was inserted!")
'end if
end if
end if
Next
Next

For Each Folder in objFolder.SubFolders
'This code runs for each subfolder
'Sub calls itself passing the path of the subfolder
Response.Write "<br>"
If left(Folder.Name,1) <> "_" Then
PrintFolderStructure(Folder.Path)
End If

Next

Set objFSO = Nothing
Set objConn = Nothing
End Sub
%>

I'm having trouble capturing the DisplayName and URL to put it in the database. Please help...
Shuvi