Hej Det var en del kod, jag skummar igenom snabbt och ger några tips bara.. Hejfso - optimering
Nån som känner för att ge lite tips på hur man kan optimera denna kod?
<%
Dim strPath
Dim objFSO
Dim objFolder
Dim objItem
strPath = "images/galleri/"
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath(strPath))
Set rstFolders = Server.CreateObject("ADODB.Recordset")
rstFolders.Fields.Append "name", adVarChar, 255
rstFolders.Fields.Append "size", adInteger
rstFolders.Fields.Append "date", adDate
rstFolders.Open
Count = 0
nFirst = CInt(Request.QueryString("page"))
nLast = nFirst + 5
For Each objItem In objFolder.SubFolders
if(Count >= nFirst) Then
rstFolders.AddNew
rstFolders.Fields("name").Value = objItem.Name
rstFolders.Fields("size").Value = objItem.Size
rstFolders.Fields("date").Value = objItem.DateCreated
End If
Count = Count + 1
If (Count >= nLast) Then Exit For
Next
Set objItem = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
IF rstFolders.EOF OR rstFolders.BOF THEN
goStart = "Yes"
ELSE
rstFolders.Sort = "date DESC"
rstFolders.MoveFirst
Do While Not rstFolders.EOF
fSize = rstFolders.Fields("size").Value
fSize = Round(fSize/1024/1024,2)
fDate = FormatDateTime(rstFolders.Fields("date").Value,2) & " kl " & FormatDateTime(rstFolders.Fields("date").Value,4)
%>
"><%=rstFolders.Fields("name").Value%><br>
<%
Response.Write "Skapad: " & fDate & "<br>"
Response.Write "Storlek: " & fSize & " mb<br>"
Dim FS, myFolder, myFiles
Set FS = CreateObject("Scripting.FileSystemObject")
Set myFolder = FS.GetFolder(Server.MapPath("images/galleri/" & rstFolders.Fields("name").Value))
Set myFiles = myFolder.Files
antalBilder = 0
For Each fileFound In myFiles
antalBilder = antalBilder + 1
Next
Response.Write "Antal bilder: " & antalBilder & " st<br><br>"
Set FS = Nothing
Set myFolder = Nothing
Set myFiles = Nothing
rstFolders.MoveNext
Loop
END IF
Response.Write "<div align=right>"
If(nFirst>=5) Then
IF goStart = "Yes" THEN
'Visa inget
ELSE
Response.Write "Tidigare "
END IF
End If
IF goStart = "Yes" THEN
Response.Write "<div align=left>"
Response.Write "<b>Ojdå!</b><br>Nu tog det slut på bilder.<br><br>"
Response.Write "</div>"
Response.Write "Tillbaka till start "
ELSE
Response.Write "Nästa "
END IF
Response.Write "</div>"
rstFolders.Close
Set rstFolders = Nothing
%>
Sv: fso - antal bilder i undermappar
<code>
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
' Du har här ett objFSO. Använd det i resten av koden. Skapa det inte igen!
' längre ner skapar du det för varje varv i en snurra, inte så bra.
' sätt den till Nothing allra sist.
For Each objItem In objFolder.SubFolders
if(Count >= nFirst) Then
' detta är väl onödigt. For-snurran stannar när det är slut..
Next
' vad gör FormatDateTime ?
' ett snabbare sätt att slå ihop strängar på:
Dim fDate
ReDim fDate(2) ' detta görs EN gång. Bara EN gång. 3 element. Upperbound=2.
fDate(0) = FormatDateTime(rstFolders.Fields("date").Value,2)
fDate(1) = " kl "
fDate(2) FormatDateTime(rstFolders.Fields("date").Value,4)
sResult = Join(fDate)
Response.Write "Skapad: " & fDate & "<br>"
'concat med en array som ovan eller
'ett annat sätt som oxå är mer optimalt.
Response.Write "Skapad: "
Response.Write fDate
Response.Write "<br>"
Dim FS, myFolder, myFiles
Set FS = CreateObject("Scripting.FileSystemObject")
'NEJ!! inte en gång till .. :-)
For Each fileFound In myFiles
antalBilder = antalBilder + 1
Next
' finns inte myFolder.Files.Count ?
</code>Sv: fso - antal bilder i undermappar
Tack för hjälpen :)
Det blev så här till slut.
( går att beskåda på http://www.weine.se/ .... galleri i menyn )
<%
Dim strPath
Dim objFSO
Dim objFolder
Dim objItem
Dim myFolder
Dim myFiles
strPath = "images/galleri/"
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath(strPath))
Set rstFolders = Server.CreateObject("ADODB.Recordset")
rstFolders.Fields.Append "name", adVarChar, 255
rstFolders.Fields.Append "size", adInteger
rstFolders.Fields.Append "date", adDate
rstFolders.Open
pageCount = 0
nFirst = CInt(Request.QueryString("page"))
nLast = nFirst + 5
For Each objItem In objFolder.SubFolders
rstFolders.AddNew
rstFolders.Fields("name").Value = objItem.Name
rstFolders.Fields("size").Value = objItem.Size
rstFolders.Fields("date").Value = objItem.DateCreated
pageCount = pageCount + 1
If (pageCount >= nLast) Then Exit For
Next
IF rstFolders.EOF OR rstFolders.BOF THEN
goStart = "Yes"
ELSE
rstFolders.Sort = "date DESC"
rstFolders.MoveFirst
Do While Not rstFolders.EOF
fSize = rstFolders.Fields("size").Value
fSize = Round(fSize/1024/1024,2)
Dim fDate
ReDim fDate(2)
fDate(0) = FormatDateTime(rstFolders.Fields("date").Value,2)
fDate(1) = " kl "
fDate(2) = FormatDateTime(rstFolders.Fields("date").Value,4)
fDateResult = Join(fDate)
%>
"><%=rstFolders.Fields("name").Value%><br>
<%
Response.Write "Skapad: "
Response.Write fDateResult
Response.Write "<br>"
Set myFolder = objFSO.GetFolder(Server.MapPath("images/galleri/" & rstFolders.Fields("name").Value))
Set myFiles = myFolder.Files
Response.Write "Antal bilder: "
Response.Write myFolder.Files.Count
Response.Write " st<br>"
Response.Write "Storlek: "
Response.Write fSize
Response.Write " mb<br><br>"
rstFolders.MoveNext
Loop
END IF
Response.Write "<div align=right>"
If(nFirst>=5) Then
IF goStart = "Yes" THEN
'Visa inget
ELSE
Response.Write "Tidigare "
END IF
End If
IF goStart = "Yes" THEN
Response.Write "<div align=left>"
Response.Write "<b>Ojdå!</b><br>Nu tog det slut på bilder.<br><br>"
Response.Write "</div>"
Response.Write "Tillbaka till start "
ELSE
Response.Write "Nästa "
END IF
Response.Write "</div>"
rstFolders.Close
Set rstFolders = Nothing
Set myFolder = Nothing
Set myFiles = Nothing
Set objItem = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
%>