File System Object Help

B

boyd.roberts

Hello All,

I am trying to generate a list of files in a directory on a website,
which only includes a defined list of file extentions.

Below is my code but i have an issue that this only returns the first
file in my includelist.


<%
Dim objFSO, objFile, objFileitem, objFolder, objFolderContents
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath("."))
Set objFolderContents = objFolder.Files

for each objFileItem in objFolderContents

includelist = "html, asp, php, htm"
arrInclude = split(includelist, ",")

pos = instr(objFileItem.Name, ".")
extension = mid(objFileItem.Name, pos+1, len(objFileItem.Name)-pos)

strinclude = false

if extension = arrInclude(i) then
strinclude = true
end if

if strinclude = true then
%>
<a href="<%=objFileitem.Name%>"><%=objFileitem.Name%></a><br>
<%
end if
next
%>

Hope someone can show me the errors of my ways.

Many Thanks
 
B

Bob Barrows [MVP]

Hello All,

I am trying to generate a list of files in a directory on a website,
which only includes a defined list of file extentions.

Below is my code but i have an issue that this only returns the first
file in my includelist.


<%
Dim objFSO, objFile, objFileitem, objFolder, objFolderContents
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath("."))
Set objFolderContents = objFolder.Files

for each objFileItem in objFolderContents

includelist = "html, asp, php, htm"
arrInclude = split(includelist, ",")

pos = instr(objFileItem.Name, ".")
extension = mid(objFileItem.Name, pos+1, len(objFileItem.Name)-pos)

strinclude = false

if extension = arrInclude(i) then

You tell me: what is the value of i here?
What do you think you would see if you did
Response.Write i & said:
strinclude = true
end if

if strinclude = true then
%>
<a href="<%=objFileitem.Name%>"><%=objFileitem.Name%></a><br>
<%
end if
next
%>

Hope someone can show me the errors of my ways.
Try this:

includelist = ",html,asp,php,htm,"
if instr(includelist, "," & extension & ",") > 0 then
strinclude = true
else
strinclude = false
end if
 
O

Old Pedant

includelist = ",html,asp,php,htm,"
if instr(includelist, "," & extension & ",") > 0 then
strinclude = true
else
strinclude = false
end if

Well, since my screen name is indeed old PEDANT...

Why not simply

<%
....
strInclude = InStr( ",html,asp,php,htm," , "," & extension & "," ) > 0
....
%>

Don't need to use an IF test to set a boolean value based on something that
is already a boolean value.
 
A

Anthony Jones

Hello All,

I am trying to generate a list of files in a directory on a website,
which only includes a defined list of file extentions.

Below is my code but i have an issue that this only returns the first
file in my includelist.


<%
Dim objFSO, objFile, objFileitem, objFolder, objFolderContents
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath("."))
Set objFolderContents = objFolder.Files

for each objFileItem in objFolderContents

includelist = "html, asp, php, htm"
arrInclude = split(includelist, ",")

pos = instr(objFileItem.Name, ".")
extension = mid(objFileItem.Name, pos+1, len(objFileItem.Name)-pos)

strinclude = false

if extension = arrInclude(i) then
strinclude = true
end if

if strinclude = true then
%>
<a href="<%=objFileitem.Name%>"><%=objFileitem.Name%></a><br>
<%
end if
next
%>

Hope someone can show me the errors of my ways.

In addition to the help the others have given you should bear in mind that
the following is a legal file name:-

This.is.a.file.htm

Use-

sIncludeList = ",html,asp,php,htm,"

....

sExtension = objFSO.GetExtenstionName(objFileItem.Name)

bInclude = Instr(sIncludeList, "," & sExtension & ",") > 0
 
B

Bob Barrows [MVP]

Old said:
Well, since my screen name is indeed old PEDANT...

Why not simply

<%
...
strInclude = InStr( ",html,asp,php,htm," , "," & extension & "," ) > 0
...
%>

Don't need to use an IF test to set a boolean value based on
something that is already a boolean value.

That's how I would do it, but my goal was to make sure the OP understood
what was going on, not minimizing the number of lines of code. :)
 
B

Bob Barrows [MVP]

Old said:
Well, since my screen name is indeed old PEDANT...

Why not simply

<%
...
strInclude = InStr( ",html,asp,php,htm," , "," & extension & "," ) > 0
...
%>

Don't need to use an IF test to set a boolean value based on
something that is already a boolean value.

:)
Actually, since we are being pedantic, InStr() does not return a boolean
value: it returns a number signifying the position of the search string
in the to-be-searched string. Your shortcut is relying on the
coincidence that zero corresponds to the value of the boolean false in
vbscript, as well as relying on an implicit conversion of an integer to
a boolean.

It's fortunate that vbscript is no longer being developed so there is no
chance that Instr() will ever be revised to return a zero-based index
for the character position ...
 
O

Old Pedant

Actually, since we are being pedantic, InStr() does not return a boolean
value: it returns a number signifying the position of the search string
in the to-be-searched string. Your shortcut is relying on the
coincidence that zero corresponds to the value of the boolean false in
vbscript, as well as relying on an implicit conversion of an integer to
a boolean.

Ummm...not, it's not. Read it again:

strInclude = InStr( ",html,asp,php,htm," , "," & extension & "," ) > 0

Look at the "> 0" there at the end.

That's why I noted "Don't need to use an IF test to set a boolean value
based on something that is already a boolean value." That is, the "> 0"
comparison is already producing the boolean value.

I suppose it would have been clearer to have written it as

strInclude = ( InStr( ",html,asp,php,htm," , "," & extension & "," ) >
0 )

But since extra parentheses in VBScript cause an extra dummy Variant to be
pushed onto the operator stack, I avoided that. Yeah, I know. Optimizing
the wrong thing.

In reality, of course, there's no reason in his code to even *HAVE* that
strInclude variable. Easier, faster, simpler to just do the IF test and
driectly affect the HTML:

<%
....
for each objFileItem in objFolderContents
fName = objFileItem.Name
extension = Mid(fName, InStrRev(fName,".")+1)
If InStr( ",html,asp,php,htm," , "," & extension & "," ) > 0 Then
%>
<a href="<%=fName%>"><%=fName%></a><br>
<%
end if
next
%>
....

But never mind. Getting far off track now.
 
B

Bob Barrows [MVP]

Old said:
Ummm...not, it's not. Read it again:

strInclude = InStr( ",html,asp,php,htm," , "," & extension & ","
) > 0

Look at the "> 0" there at the end.
Oops, I did miss that, A lot of people think leaving that ">0" bit off is
very smart.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top