Question

I'm creating a system that need to populate subfolder name, which this will be part of reference URL. These subfolders will be manually uploaded through FTP software under a provided folder (eg. newsletter). Whenever subfolder in the server was created, name of that subfolder will appear on a page so that user can select. After that an URL that point to that subfolder will be created for further usage.

Right now my current code (below) work well in populating subfolder name. However before this step, I want to check if there's any subfolder exist first. If not, ask the user to upload it first before it take to further step.

I've tried with the code below and it not work. So could you please help me. Thank you.

<%
Dim objFSO
dim fo,x
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
set fo=objFSO.GetFolder(Server.MapPath("../newsletterDB"))

    for each x in fo.SubFolders
        if fo.SubFolders <> null then
      Response.write("<input type = ""radio"" name=""group1"" value=" & x.Name & ">" & x.Name & "<br>")
        else
         response.write " Please upload any e-Mag first !"
        end if
    next

set fo=nothing
set objFSO=nothing
%> 
Was it helpful?

Solution

Your code fails because you iterate the sub folders, and only in the loop perform the check.

Here is a custom function I have to return plain array of sub folder names of a given root folder:

'GetSubFolders: gets absolute path of a folder and returns array of its sub folders.
'In case given path is not a valid existing folder path, empty array is returned. (no error check)
Function GetSubFolders(strAbsoluteFolderPath)
    Dim objFSO, objFolder, arrSubFolders()
    Dim oCurrentSubFolder
    ReDim arrSubFolders(-1)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists(strAbsoluteFolderPath) Then
        Set objFolder = objFSO.GetFolder(strAbsoluteFolderPath)
        For Each oCurrentSubFolder In objFolder.SubFolders
            ReDim Preserve arrSubFolders(UBound(arrSubFolders) + 1)
            arrSubFolders(UBound(arrSubFolders)) = oCurrentSubFolder.Name
        Next
        Set objFolder = Nothing
    End If
    Set objFSO = Nothing
    GetSubFolders = arrSubFolders
End Function

Usage in your case is as simple as:

Dim arrSubFolders
arrSubFolders = GetSubFolders(Server.MapPath("../newsletterDB"))
If UBound(arrSubFolders)>=0 Then
    Response.write("<input type = ""radio"" name=""group1"" value=""" & arrSubFolders(0) & """>" & arrSubFolders(0) & "<br>")
Else  
    response.write " Please upload any e-Mag first !"
End If
Erase arrSubFolders
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top