Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > File System Object Help

Reply
Thread Tools

File System Object Help

 
 
boyd.roberts@heathwallace.com
Guest
Posts: n/a
 
      06-04-2008
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
 
Reply With Quote
 
 
 
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      06-04-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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 & "<BR>"

> 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

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
 
 
 
Old Pedant
Guest
Posts: n/a
 
      06-04-2008
> 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.


 
Reply With Quote
 
Anthony Jones
Guest
Posts: n/a
 
      06-05-2008

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


--
Anthony Jones - MVP ASP/ASP.NET


 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      06-05-2008
Old Pedant wrote:
>> 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.


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.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      06-05-2008
Old Pedant wrote:
>> 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.



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

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


 
Reply With Quote
 
Old Pedant
Guest
Posts: n/a
 
      06-05-2008
> 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.


 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      06-06-2008
Old Pedant wrote:
>> 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.

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

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File System Object - not deleting file Lise ASP General 3 06-08-2006 03:14 PM
Help: Unable to cast object of type 'System.Int32' to type 'System.String'. keithb ASP .Net 3 05-07-2006 08:06 AM
Object reference not set to an instance of an object. System.Web.UI.WebControls.ListItemCollection.SaveViewState() System.Web.UI.WebControls.ListControl.SaveViewState() Brano ASP .Net 3 11-08-2005 12:29 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Why are sender and object used as in sender As System.Object, ByVal e As System.EventArgs? Showjumper ASP .Net 2 08-02-2003 10:53 PM



Advertisments