Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
 All Forums
 Community Forums
 Code Support: ASP.NET (Non-Forum Related)
 "Cleaning up" a script

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!
Before posting, make sure you have read this topic!

Screensize:
UserName:
Password:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkInsert EmailInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
   

T O P I C    R E V I E W
Shaggy Posted - 07 May 2010 : 08:16:19
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()
%>
4   L A T E S T    R E P L I E S    (Newest First)
HuwR Posted - 10 May 2010 : 07:08:14
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
HuwR Posted - 10 May 2010 : 05:31:01
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
Shaggy Posted - 10 May 2010 : 05:18:25
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.

HuwR Posted - 07 May 2010 : 14:39:28
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

Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000 Version 3.4.07