...

View Full Version : Getting ‘FileSystemObject’ to ignore a specific file



Stanlee
09-21-2005, 11:20 AM
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’ http://www.codingforums.com/showthread.php?t=8736 which was really good – but I noticed that it still lists demo.asp file (that’s the one I’d like to exclude!)


THANKS!

NancyJ
09-21-2005, 12:40 PM
<%
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


<%
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


pos = instr(file.name, ".")
extension = mid(file.name, pos+1, len(file.name)-pos)

then you would replace


if right(file.name, 3) = arrEclude(i) then
exclude = true
end if
with


if extension = arrEclude(i) then
exclude = true
end if


apologies if there are any mistakes in there, typing as I'm tihnking

glenngv
09-21-2005, 12:54 PM
Alternative solution using RegExp (BrainJar's script as base):


<% 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)
%>

Stanlee
09-21-2005, 05:28 PM
Thanks youz! This all looks brilliant! :thumbsup: 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!

NancyJ
09-21-2005, 05:47 PM
a couple of things jump out at me - I've highlighted the changes



<%
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

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)

Stanlee
09-21-2005, 06:13 PM
Wow. They are some really obvious mistakes I've made right there! :o

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

NancyJ
09-21-2005, 06:24 PM
Wow. They are some really obvious mistakes I've made right there! :o

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

glenngv
09-22-2005, 08:09 AM
Microsoft VBScript compilation error '800a0414'

Cannot use parentheses when calling a Sub

/dirtest.asp, line 24

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

Remove the parentheses.

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


<% 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
%>

Stanlee
10-13-2005, 10:44 PM
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!

glenngv
10-14-2005, 10:11 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum