Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Getting ‘FileSystemObject’ to ignore a specific file

    OK, here’s one: Is there a nice easy way of getting the FileSystemObject to ‘ignore’ a certain file or file-type? (or even better a set of file-types! A comma delineated ‘exclusion list’ would be great!)

    I read the sticky 'listing all files in a directory’ listing all files in a directory which was really good – but I noticed that it still lists demo.asp file (that’s the one I’d like to exclude!)


    THANKS!
    Last edited by Stanlee; 09-21-2005 at 10:23 AM.

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    Code:
    <%
    filepath = "/"
    exludelist = "demo.asp, ecludeme.asp"
    
    arrExclude = split(excludelist, ", ")
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    Set Folder = fso.GetFolder(Server.MapPath(filepath))
    
    for each file in Folder.Files
       for i = lbound(arrExclude) to ubound(arrExclude) 
           exclude = false
           if file.name = arrEclude(i) then
               exclude = true
           end if
       next
       if exclude = false then
          'display file stuff here
       end if
    next
    
    Set fso = nothing
    %>
    or to exclude certain extensions

    Code:
    <%
    filepath = "/"
    exludelist = "asp, php"
    
    arrExclude = split(excludelist, ", ")
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    Set Folder = fso.GetFolder(Server.MapPath(filepath))
    
    for each file in Folder.Files
       for i = lbound(arrExclude) to ubound(arrExclude) 
           exclude = false
           if right(file.name, 3) = arrEclude(i) then
               exclude = true
           end if
       next
       if exclude = false then
          'display file stuff here
       end if
    next
    
    Set fso = nothing
    %>
    a more advanced version might get the position of the . in the file name then take that position to the end, rather than assuming that the file extension is only 3 letters...
    something like
    Code:
    pos = instr(file.name, ".") 
    extension = mid(file.name, pos+1, len(file.name)-pos)
    then you would replace
    Code:
    if right(file.name, 3) = arrEclude(i) then
               exclude = true
           end if
    with
    Code:
    if extension = arrEclude(i) then
               exclude = true
           end if
    apologies if there are any mistakes in there, typing as I'm tihnking
    Last edited by NancyJ; 09-21-2005 at 11:46 AM.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Alternative solution using RegExp (BrainJar's script as base):
    Code:
    <% sub ListFolderContents(path, exclusionPattern) 'exclusionPattern = array of file extensions to be excluded
    
         dim fs, folder, file, item, url, regex, strPattern, pattern
    
         set fs = CreateObject("Scripting.FileSystemObject")
         set folder = fs.GetFolder(path)
    
        'Display the target folder and info.
    
         Response.Write("<li><b>" & folder.Name & "</b> - " _
           & folder.Files.Count & " files, ")
         if folder.SubFolders.Count > 0 then
           Response.Write(folder.SubFolders.Count & " directories, ")
         end if
         Response.Write(Round(folder.Size / 1024) & " KB total." _
           & "</li>" & vbCrLf)
    
         Response.Write("<ul>" & vbCrLf)
    
         'Display a list of sub folders.
    
         for each item in folder.SubFolders
           ListFolderContents(item.Path, exclusionPattern)
         next
    
        'set regular expression for excluding files
        Set regex = New RegExp 
        if isarray(exclusionPattern) then
           for each pattern in exclusionPattern
              if strPattern <> "" Then
                strPattern = strPattern & "|\"& pattern
             else
                strPattern = "(\" & pattern
             end if
           next
           if pat <> "" then
              strPattern = strPattern & ")$"
           end if 
           regex.Pattern = strPattern
           regex.IgnoreCase = True
        end if
    
         'Display a list of files.
         For Each item In folder.Files
           If Not regex.Test(item.path) Then
             url = MapURL(item.path)
             Response.Write("<li><a href=""" & url & """>" _
               & item.Name & "</a> - " _
               & item.Size & " bytes, " _
               & "last modified on " & item.DateLastModified & "." _
               & "</li>" & vbCrLf)
           End If
         Next
    
         Response.Write("</ul>" & vbCrLf)
    
       end sub 
    
    'sample usage
    ListFolderContents "somepath", Array(".asp", ".inc", ".js") 'excludes .asp, .inc and .js files
    ListFolderContents "someotherpath", null 'no exclusion (all files)
    %>

  • #4
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks youz! This all looks brilliant! But I’m having a few problems… I’ve tried both solutions and hit a couple of snags


    Nancy:
    First off, I decided to make it an ‘Includelist’ rather than an ‘Exclude list’ (Not that this makes any difference! )
    The variable ‘strinclude’ seems to be empty at any point in the script though, the ‘for-each’ loops are working fine it’s just that the lbound /ubound created ‘strinclude’ variable isn’t doing it’s thing…

    <%
    Dim objFSO, objFile, objFileitem, objFolder, objFolderContents
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.getFile(strPhisicalPath)
    Set objFolder = objFile.ParentFolder
    Set objFolderContents = objFolder.Files
    objFolder = Cstr(objFolder & "\" & strSUBFOLDER & "\")



    includelist = "jpg, gif"
    arrInclude = split(arrInclude, ", ")

    'Set Folder = fso.GetFolder(Server.MapPath(filepath))

    for each objFileItem in objFolderContents
    for i = lbound(arrInclude) to ubound(arrInclude)
    strinclude = "false"
    if right(objFileitem.name, 3) = arrEclude(i) then
    strinclude = true
    end if
    next
    if strinclude = false then
    'display file stuff here
    response.write strinclude &"," ‘ (This just returns a list of commas at the moment)
    end if
    next

    Set fso = nothing

    %>




    glenngv

    I’m getting a error as I’m calling the sub here Glen, I’ve been looking around for the solution from various sources (Removing the parentheses, explicitly using the ‘Call’ command) but have had no luck so far…


    Microsoft VBScript compilation error '800a0414'

    Cannot use parentheses when calling a Sub

    /dirtest.asp, line 24

    ListFolderContents(item.Path, exclusionPattern)
    -----------------------------------------------^


    I can see what it's doing though and it's a step up from where I was! Ta!
    Last edited by Stanlee; 09-21-2005 at 04:30 PM.

  • #5
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    a couple of things jump out at me - I've highlighted the changes

    Code:
    <%
    Dim objFSO, objFile, objFileitem, objFolder, objFolderContents
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.getFile(strPhisicalPath)
    Set objFolder = objFile.ParentFolder
    Set objFolderContents = objFolder.Files
    objFolder = Cstr(objFolder & "\" & strSUBFOLDER & "\")
    
    
    
    includelist = "jpg, gif"
    arrInclude = split(includelist, ", ")
    
    'Set Folder = fso.GetFolder(Server.MapPath(filepath))
    
    for each objFileItem in objFolderContents
    for i = lbound(arrInclude) to ubound(arrInclude)
    strinclude = false
    if right(objFileitem.name, 3) = arrInclude(i) then
    strinclude = true
    end if
    next
    if strinclude = true then
    'display file stuff here
    response.write objFileItem.name&"," ‘ (This just returns a list of commas at the moment)
    end if
    next
    
    Set fso = nothing
    
    %>
    as for
    Code:
    Microsoft VBScript compilation error '800a0414'
    
    Cannot use parentheses when calling a Sub
    
    /dirtest.asp, line 24
    
    ListFolderContents(item.Path, exclusionPattern)
    try ListFolderContents item.Path, exclusionPattern
    or call ListFolderContents(item.Path, exclusionPattern)

  • #6
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Apolgies and Thanks!

    Wow. They are some really obvious mistakes I've made right there!

    I tried the amended code and what-do-y'now, it worked! So both thanks and apologies!!

  • #7
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    Quote Originally Posted by Stanlee
    Wow. They are some really obvious mistakes I've made right there!

    I tried the amended code and what-do-y'now, it worked! So both thanks and apologies!!
    Happens to the best of us

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Quote Originally Posted by Stanlee
    Microsoft VBScript compilation error '800a0414'

    Cannot use parentheses when calling a Sub

    /dirtest.asp, line 24

    ListFolderContents(item.Path, exclusionPattern)
    -----------------------------------------------^
    Remove the parentheses.
    Code:
    ListFolderContents item.Path, exclusionPattern
    To make it an inclusion list instead of exclusion list only needs little changes. You only need to rename the parameter to inclusionPattern and remove the Not operator to reverse the condition If Not regex.Test(item.path) Then

    Code:
    <% sub ListFolderContents(path, inclusionPattern) 'inclusionPattern = array of file extensions to be included in the list
    
         dim fs, folder, file, item, url, regex, strPattern, pattern
    
         set fs = CreateObject("Scripting.FileSystemObject")
         set folder = fs.GetFolder(path)
    
        'Display the target folder and info.
    
         Response.Write("<li><b>" & folder.Name & "</b> - " _
           & folder.Files.Count & " files, ")
         if folder.SubFolders.Count > 0 then
           Response.Write(folder.SubFolders.Count & " directories, ")
         end if
         Response.Write(Round(folder.Size / 1024) & " KB total." _
           & "</li>" & vbCrLf)
    
         Response.Write("<ul>" & vbCrLf)
    
         'Display a list of sub folders.
    
         for each item in folder.SubFolders
           ListFolderContents item.Path, inclusionPattern
         next
    
        'set regular expression for excluding files
        Set regex = New RegExp 
        if isarray(inclusionPattern) then
           for each pattern in inclusionPattern
              if strPattern <> "" Then
                strPattern = strPattern & "|\"& pattern
             else
                strPattern = "(\" & pattern
             end if
           next
           if pat <> "" then
              strPattern = strPattern & ")$"
           end if 
           regex.Pattern = strPattern
           regex.IgnoreCase = True
        end if
    
         'Display a list of files.
         For Each item In folder.Files
           If regex.Test(item.path) Then
             url = MapURL(item.path)
             Response.Write("<li><a href=""" & url & """>" _
               & item.Name & "</a> - " _
               & item.Size & " bytes, " _
               & "last modified on " & item.DateLastModified & "." _
               & "</li>" & vbCrLf)
           End If
         Next
    
         Response.Write("</ul>" & vbCrLf)
    
       end sub 
    
    'sample usage
    ListFolderContents "somepath", Array(".asp", ".inc", ".js")  'includes .asp, .inc and .js files
    ListFolderContents "someotherpath", null  'includes all files
    %>

  • #9
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Glen! (Sorry about the lateness of the reply)

    I got Nancy's solution working and haven't had time to have a go at your Reg_expr solution - sure it works, and I will have a go with it!

    Ta!

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    No problem. I just contributed an alternative solution. It's still up to you which solution you want. Just want to mention that RegExp is much faster than manual string manipulation. You may not notice the difference if the number of files to be listed is not that big or the number of folder levels are not that deep or few users are connecting to the page at the same time.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •