Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP.NET (Non-Forum Related)
 "Cleaning up" a script
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 07 May 2010 :  08:16:19  Show Profile  Reply with Quote
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!)?
<%@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.”

HuwR
Forum Admin

United Kingdom
20579 Posts

Posted - 07 May 2010 :  14:39:28  Show Profile  Visit HuwR's Homepage  Reply with Quote
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

<%@ 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

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 10 May 2010 :  05:18:25  Show Profile  Reply with Quote
Woah! 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.


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.”
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20579 Posts

Posted - 10 May 2010 :  05:31:01  Show Profile  Visit HuwR's Homepage  Reply with Quote
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

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20579 Posts

Posted - 10 May 2010 :  07:08:14  Show Profile  Visit HuwR's Homepage  Reply with Quote
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

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000 Version 3.4.07