Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple ASP code needed to display & not display 1

Status
Not open for further replies.

JGKWORK

IS-IT--Management
Apr 1, 2003
342
GB
Hi,
I have the following code to list contents of a folder. I need to know the code to do the following:

1/. change the code so that it doesn't show the file "default.asp" or the folder "Images".

2/. Seperate from above I need to know how I WOULD SHOW ONLY the file "index.htm"

could some one help, thanks very much:



<%@LANGUAGE=&quot;VBSCRIPT&quot;%>
<%
Option Explicit
On Error Resume Next

' this section is optional - it just denies anonymous access
If Request.ServerVariables(&quot;LOGON_USER&quot;)=&quot;&quot; Then
Response.Status = &quot;401 Access Denied&quot;
End If

' declare variables
Dim objFSO, objFolder
Dim objCollection, objItem

Dim strPhysicalPath, strTitle, strServerName
Dim strPath, strTemp
Dim strName, strFile, strExt, strAttr
Dim intSizeB, intSizeK, intAttr, dtmDate

' declare constants
Const vbReadOnly = 1
Const vbHidden = 2
Const vbSystem = 4
Const vbVolume = 8
Const vbDirectory = 16
Const vbArchive = 32
Const vbAlias = 64
Const vbCompressed = 128

' don't cache the page
Response.AddHeader &quot;Pragma&quot;, &quot;No-Cache&quot;
Response.CacheControl = &quot;Private&quot;

' get the current folder URL path
strTemp = Mid(Request.ServerVariables(&quot;URL&quot;),2)
strPath = &quot;&quot;

Do While Instr(strTemp,&quot;/&quot;)
strPath = strPath & Left(strTemp,Instr(strTemp,&quot;/&quot;))
strTemp = Mid(strTemp,Instr(strTemp,&quot;/&quot;)+1)
Loop

strPath = &quot;/&quot; & strPath

' build the page title
strServerName = UCase(Request.ServerVariables(&quot;SERVER_NAME&quot;))
strTitle = &quot;Contents of the &quot; & strPath & &quot; folder&quot;

' create the file system objects
strPhysicalPath = Server.MapPath(strPath)
Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set objFolder = objFSO.GetFolder(strPhysicalPath)
%>
<html>
<head>
<title><%=strServerName%> - <%=strTitle%></title>
<meta name=&quot;GENERATOR&quot; content=&quot;The Mighty Hand of Bob&quot;>
</head>
<body bgcolor=&quot;#FFFFFF&quot;>
<table width=&quot;906&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; height=&quot;26&quot;>
<tr>
<td width=&quot;283&quot;><img src=&quot;/images/coatbridge.gif&quot; width=&quot;208&quot; height=&quot;69&quot;></td>
<td width=&quot;378&quot;>
<div align=&quot;center&quot;><img src=&quot;/NLN_Materials/Images/NLNMDIT.gif&quot; width=&quot;120&quot; height=&quot;100&quot;></div>
</td>
<td width=&quot;245&quot;> </td>
</tr>
</table>
<h1 align=&quot;left&quot;> </h1>
<div align=&quot;center&quot;><center>
<table width=&quot;40%&quot; border=&quot;1&quot; cellspacing=&quot;1&quot; cellpadding=&quot;2&quot; bordercolor=&quot;#CCCCCC&quot;>
<tr>
<th align=&quot;left&quot; bgcolor=&quot;#003366&quot;><font color=&quot;#FFFFFF&quot;>Course Material:</font></th>
</tr>
<%
''''''''''''''''''''''''''''''''''''''''
' output the folder list
''''''''''''''''''''''''''''''''''''''''

Set objCollection = objFolder.SubFolders

For Each objItem in objCollection
strName = objItem.Name
strAttr = MakeAttr(objItem.Attributes)
dtmDate = CDate(objItem.DateLastModified)
%>
<tr>
<td align=&quot;left&quot;><b><a href=&quot;<%=strName%>&quot;><%=strName%></a></b></td>
</tr>
<% Next %>
<%
''''''''''''''''''''''''''''''''''''''''
' output the file list
''''''''''''''''''''''''''''''''''''''''

Set objCollection = objFolder.Files

For Each objItem in objCollection
strName = objItem.Name
strFile = Server.HTMLEncode(Lcase(strName))

intSizeB = objItem.Size
intSizeK = Int((intSizeB/1024) + .5)
If intSizeK = 0 Then intSizeK = 1

strAttr = MakeAttr(objItem.Attributes)
strName = Ucase(objItem.ShortName)
If Instr(strName,&quot;.&quot;) Then strExt = Right(strName,Len(strName)-Instr(strName,&quot;.&quot;)) Else strExt = &quot;&quot;
dtmDate = CDate(objItem.DateLastModified)
%>
<tr>
<td align=&quot;left&quot;><a href=&quot;<%=strFile%>&quot;><%=strFile%></a></td>
</tr>
<% Next %>
</table>
</center></div>

</body>
</html>
<%
Set objFSO = Nothing
Set objFolder = Nothing

' this adds the IIf() function to VBScript
Function IIf(i,j,k)
If i Then IIf = j Else IIf = k
End Function

' this function creates a string from the file atttributes
Function MakeAttr(intAttr)
MakeAttr = MakeAttr & IIf(intAttr And vbArchive,&quot;A&quot;,&quot;-&quot;)
MakeAttr = MakeAttr & IIf(intAttr And vbSystem,&quot;S&quot;,&quot;-&quot;)
MakeAttr = MakeAttr & IIf(intAttr And vbHidden,&quot;H&quot;,&quot;-&quot;)
MakeAttr = MakeAttr & IIf(intAttr And vbReadOnly,&quot;R&quot;,&quot;-&quot;)
End Function
%>
 
use the objItem.Name to check what you want or dont want Office notice: The beatings won't stop until morale improves
 
thanks I tried this (If strName <> &quot;Default.asp&quot; Then) and it worked great. But i can't manage to do it to ONLY display one specific file and ignore the rest i.e. the index.htm file. Any ideas? if so could u include an example - I ain't too good with code!

Also, I now realise I need to be able to do some thing else. When the contents are listed I would like to add &quot;index.htm&quot; to the end of the link (so the web page opens imediately instead of listing contents of another page then clicking on index.htm).

Any help very much appreciated.
 
I am not sure what you mean, but I would do something loke this:

<%
if lcase(trim(strName)) = &quot;index.html&quot; then
show the file info here
end if
%>

try to include the index file at the end to show it (haven't tried it, but it should work)

right at the end of your asp page include it
(this include must be OUTSIDE the asp tags <%%> right at the end of your page)

<!-- #include virtual =&quot;index.html&quot; -->


Hope this helps
Office notice: The beatings won't stop until morale improves
 
Try

<%
if lcase(strName) = &quot;index.htm&quot; then

%>

be aware also that strName is converted to uppercase, this will cause problems if your ASP code runs on a *nix box (case sensitive file system)


Chris.
Indifference will be the downfall of mankind, but who cares?
 
Hi, tried this but it is still listing everything that's in the folder - what i need is it to ONLY list index.htm.

thanks...
 
Where in the code are you placing this conditional?


Chris. Indifference will be the downfall of mankind, but who cares?
 
Thanks for the help:


''''''''''''''''''''''''''''''''''''''''
' output the file list
''''''''''''''''''''''''''''''''''''''''

Set objCollection = objFolder.Files

For Each objItem in objCollection
strName = objItem.Name
if lcase(trim(strName)) = &quot;index.htm&quot; then
strFile = Server.HTMLEncode(Lcase(strName))

intSizeB = objItem.Size
intSizeK = Int((intSizeB/1024) + .5)
If intSizeK = 0 Then intSizeK = 1

strAttr = MakeAttr(objItem.Attributes)
strName = Ucase(objItem.ShortName)
If Instr(strName,&quot;.&quot;) Then strExt = Right(strName,Len(strName)-Instr(strName,&quot;.&quot;)) Else strExt = &quot;&quot;
dtmDate = CDate(objItem.DateLastModified)
%>
<tr>
<td align=&quot;left&quot;><a href=&quot;<%=strFile%>&quot;><%=strFile%></a></td>
</tr>
<%
End If
Next
%>
 
sorry tschonken, didn't expliain myself very well about the index.htm :

what i mean is:

this code lists the contents of a folder, each item in the list is a link to the contents of another folder.

What i want to be able to do is modify these links in order to add index.htm to the end of each. This means that when u click on the link it opens the page &quot;index.htm&quot; in the folder below it.

This saves having to go down one more level.

This would remove the need for the previous question, but I wanted to know how to do both.

Thanks for the help.
 
I can't for the life of me see why the conditional is not working. Just tested it and it worked ok on my server (Win2k)

The way to add index.htm to make a full link will be;

dtmDate = CDate(objItem.DateLastModified)
URLName = strName & &quot;/index.htm&quot;

%>
<TR>
<TD align=&quot;left&quot;><B><A href=&quot;<%=URLName%>&quot;><%=strName%></A></B></TD>
</TR>

not forgetting to dim URLName of course


Chris.

Indifference will be the downfall of mankind, but who cares?
 
Thanks alot Chris, I'll try this when i get back to work tommorrow - if the URLName works it will be a much better solution anyway,

thanks again.
 
Hi Chris,

I tried it your code, but unfortunately it is doing exactely the same thing, i.e. listing the folder contents of the next folder (instead of opening the index.htm file). I can't understand why this ain't working!

Here's the code I used - any help, anyone!

thanks.


<%@LANGUAGE=&quot;VBSCRIPT&quot;%>
<%
Option Explicit
On Error Resume Next

' this section is optional - it just denies anonymous access
If Request.ServerVariables(&quot;LOGON_USER&quot;)=&quot;&quot; Then
Response.Status = &quot;401 Access Denied&quot;
End If

' declare variables
Dim objFSO, objFolder
Dim objCollection, objItem

Dim strPhysicalPath, strTitle, strServerName
Dim strPath, strTemp
Dim strName, strFile, strExt, strAttr
Dim intSizeB, intSizeK, intAttr, dtmDate
Dim URLName

' declare constants
Const vbReadOnly = 1
Const vbHidden = 2
Const vbSystem = 4
Const vbVolume = 8
Const vbDirectory = 16
Const vbArchive = 32
Const vbAlias = 64
Const vbCompressed = 128

' don't cache the page
Response.AddHeader &quot;Pragma&quot;, &quot;No-Cache&quot;
Response.CacheControl = &quot;Private&quot;

' get the current folder URL path
strTemp = Mid(Request.ServerVariables(&quot;URL&quot;),2)
strPath = &quot;&quot;

Do While Instr(strTemp,&quot;/&quot;)
strPath = strPath & Left(strTemp,Instr(strTemp,&quot;/&quot;))
strTemp = Mid(strTemp,Instr(strTemp,&quot;/&quot;)+1)
Loop

strPath = &quot;/&quot; & strPath

' build the page title
strServerName = UCase(Request.ServerVariables(&quot;SERVER_NAME&quot;))
strTitle = &quot;Contents of the &quot; & strPath & &quot; folder&quot;

' create the file system objects
strPhysicalPath = Server.MapPath(strPath)
Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set objFolder = objFSO.GetFolder(strPhysicalPath)
%>
<html>
<head>
<title><%=strServerName%> - <%=strTitle%></title>
<meta name=&quot;GENERATOR&quot; content=&quot;The Mighty Hand of Bob&quot;>
</head>
<body bgcolor=&quot;#FFFFFF&quot;>
<table width=&quot;906&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; height=&quot;26&quot;>
<tr>
<td width=&quot;283&quot;><img src=&quot;/images/coatbridge.gif&quot; width=&quot;208&quot; height=&quot;69&quot;></td>
<td width=&quot;378&quot;>
<div align=&quot;center&quot;><img src=&quot;/images/NLNMDIT.gif&quot; width=&quot;120&quot; height=&quot;100&quot;></div>
</td>
<td width=&quot;245&quot;>&nbsp;</td>
</tr>
</table>
<h1 align=&quot;left&quot;>&nbsp;</h1>
<div align=&quot;center&quot;><center>
<table width=&quot;40%&quot; border=&quot;1&quot; cellspacing=&quot;1&quot; cellpadding=&quot;2&quot; bordercolor=&quot;#CCCCCC&quot;>
<tr>
<th align=&quot;left&quot; bgcolor=&quot;#003366&quot;><font color=&quot;#FFFFFF&quot;><%=strServerName%><br>
<%=strTitle%>:</font></th>
</tr>
<%
''''''''''''''''''''''''''''''''''''''''
' output the folder list
''''''''''''''''''''''''''''''''''''''''

Set objCollection = objFolder.SubFolders

For Each objItem in objCollection
strName = objItem.Name
strAttr = MakeAttr(objItem.Attributes)
dtmDate = CDate(objItem.DateLastModified)
%>
<tr>
<td align=&quot;left&quot;><b><a href=&quot;<%=strName%>&quot;><%=strName%></a></b></td>
</tr>
<% Next %>
<%
''''''''''''''''''''''''''''''''''''''''
' output the file list
''''''''''''''''''''''''''''''''''''''''

Set objCollection = objFolder.Files

For Each objItem in objCollection
strName = objItem.Name
strFile = Server.HTMLEncode(Lcase(strName))

intSizeB = objItem.Size
intSizeK = Int((intSizeB/1024) + .5)
If intSizeK = 0 Then intSizeK = 1

strAttr = MakeAttr(objItem.Attributes)
strName = Ucase(objItem.ShortName)
If Instr(strName,&quot;.&quot;) Then strExt = Right(strName,Len(strName)-Instr(strName,&quot;.&quot;)) Else strExt = &quot;&quot;
dtmDate = CDate(objItem.DateLastModified)
URLName = strName & &quot;/index.htm&quot;

%>
<tr>
<td align=&quot;left&quot;><a href=&quot;<%=URLName%>&quot;><%=strName%></a></td>
</tr>
<%
Next
%>
</table>
</center></div>

</body>
</html>
<%
Set objFSO = Nothing
Set objFolder = Nothing

' this adds the IIf() function to VBScript
Function IIf(i,j,k)
If i Then IIf = j Else IIf = k
End Function

' this function creates a string from the file atttributes
Function MakeAttr(intAttr)
MakeAttr = MakeAttr & IIf(intAttr And vbArchive,&quot;A&quot;,&quot;-&quot;)
MakeAttr = MakeAttr & IIf(intAttr And vbSystem,&quot;S&quot;,&quot;-&quot;)
MakeAttr = MakeAttr & IIf(intAttr And vbHidden,&quot;H&quot;,&quot;-&quot;)
MakeAttr = MakeAttr & IIf(intAttr And vbReadOnly,&quot;R&quot;,&quot;-&quot;)
End Function
%>
 
Something else a bit wierd:

when I remove the following:

<td align=&quot;left&quot;><a href=&quot;<%=URLName%>&quot;><%=strName%></a></td>

Nothing changes, i.e. i still get a listing of the contents of the folder - I thought this bit of code was the output?

cheers...
 
Observation and a couple of quick questions because this is looking like a server issue

I am guessing the reason for this code is to avoid having to create virtual folders on the server,

1/ do you have access to the server
2/ is it running MS IIS 5.0 (Win2k)


Chris.
Indifference will be the downfall of mankind, but who cares?
 
Can anyone advise as to what these server issues might be?

many thanks.
 
Please help me - I really need to get this working! can anyone help?

Many thanks.
 
Sorry, got dragged kicking and screaming into the real world, Taken me hours to recover! [shocked]


I take it this is what you are trying to get to


Chris.
Indifference will be the downfall of mankind, but who cares?
 
No problem, thanks for getting back.

ok, your just showing off now! hehe, yeh that's exactely what I need.

I'm off to bed - can't stay awake any longer - back tomorrow at 9am. (UK).

thank you so much for the help..
 
Wait a minute, i've just noticed i can downoad the code! does that mean its workin?! i.e. it wasn't the server? OR am i just getting exited?

I'll try it tommorrow at work, thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top