"Cleaning up" a script - Posted (3841 Views)
Support Moderator
Shaggy
Posts: 6780
6780
I've written the following script which loops through a directory of images, and outputs an XML file with various attributes of those images. It works just fine but, given that I'm clueless about .NET, is there anything you guys can see that I should be doing differently to make it more efficient? In particular, I'm trying to figure out the .NET equivalent of &_ so I can wrap a string onto a new line - all those response.writes just look wrong to me! Also, is there any way of inserting a variable into a string without having to enclose it in parentheses (It's not really a big deal but, again, it just looks wrong!)?
Code:
<%@page language="VB" debug=true%>
<%@import namespace="system.drawing"%>
<%@import namespace="system.io"%>
<%
response.contenttype="text/xml"
response.write("<?xml version=""1.0"" encoding=""utf-8""?>")
response.write("<images>")
dim objFso=server.createobject("Scripting.FileSystemObject")
dim objFolder=objFso.getfolder(server.mappath("/path/to/directory/"))
dim x,intHeight,intWidth
for each x in objFolder.files
intHeight=0:intWidth=0
using fsFile as new filestream(x.path,filemode.open)
dim bmpImg as new bitmap(bmpImg.fromstream(fsFile))
intWidth=bmpImg.width
intHeight=bmpImg.height
bmpImg.dispose():bmpImg=nothing
end using
response.write("<image>")
response.write("<date>"&(x.datelastmodified)&"</date>")
response.write("<height>"&(intHeight)&"</height>")
response.write("<name>"&(x.name)&"</name>")
response.write("<size>"&(x.size)&"</size>")
response.write("<width>"&(intWidth)&"</width>")
response.write("</image>")
next
objFolder=nothing:objFso=nothing
response.write("</images>")
response.end()
%>
Search is your friend “I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”
 Sort direction, for dates DESC means newest first  
 Page size 
Posted
Forum Admin
HuwR
Posts: 20611
20611
mmm, well I wouldn't write it like that at all, you have written it like classic ASP not .Net, you don't use things like FSO in .net

this is how you would do it in a .net way
Code:

<%@ Page Language="VB" Debug="True" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>

<script language="VB" runat="server">
Sub Page_Load(Sender as Object, E as EventArgs)

Dim bmp As Bitmap
Dim builder As New StringBuilder

Dim dirInfo As New DirectoryInfo(MapPath("/path/to/directory/"))
Dim files as FileInfo() = dirInfo.GetFiles("*")

builder.Append("<?xml version=\"1.0"" encoding=\"utf-8\"?>").AppendLine()
builder.Append("<images>").AppendLine()
For Each File In files
bmp = New Bitmap(File.Name)
builder.Append("<image>").AppendLine()
builder.AppendFormat("<date>{0}</date>,File.LastWriteTime.ToShortDateString()").AppendLine()
builder.AppendFormat("<height>{0}</height>",bmp.Height).AppendLine()
builder.AppendFormat("<name>{0}</name>",Path.GetFileName(File)).AppendLine()
builder.AppendFormat("<size>{0}</size>",bmp.Size).AppendLine()
builder.AppendFormat("<width>{0}</width>",bmp.Width).AppendLine()
builder.Append("</image>").AppendLine()

Next
builder.Append("</images>")
Dim s As String = builder.ToString
Response.Write(s)

End Sub
</script>

I don't normally write VB, much prefer C# so not tested and my not be 100% correct smile
Posted
Support Moderator
Shaggy
Posts: 6780
6780
Woah! shock That's very different - looks like I have a looooong way to go with .NET.
Thanks for the rewrite, Huw; will give it s apin later on and let you know how I get on. smile
Search is your friend “I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”
Posted
Forum Admin
HuwR
Posts: 20611
20611
can't guarantee it will work, it was a rough guess based on how I would write it in c#, I guess I could have done that and then run it through one of the code convertors smile
Posted
Forum Admin
HuwR
Posts: 20611
20611
there are also a lod of XML functions which may be better to use than the stringbuilder, but without knowing what version of .net you are targeting it is difficult to know which ones you can use
 
You Must enter a message