Friday, August 29, 2003

.NET Weblogs @ ASP.NET

.NET Weblogs @ ASP.NET - good weblog site with many bloggers.
Of interest is Datagrid Girl - using the datagrid in ASP.NET

Friday, August 22, 2003

Google Toolbar 2.0

Google Toolbar 2.0 is no longer beta: http://toolbar.google.com/ Features:
  • Search the web with Google from any site
  • New! Eliminate pop-up ads
  • New! Fill in forms with one click
  • New! Link a weblog to the page you're visiting
  • New! Restrict your search to pages located in a specific country
  • Search just within the pages of a site
  • Highlight search terms on a page

VBScript - Set Homepage (MSIE) / Add Notepad to Send To

The following will set up your homepage (in Internet Explorer) via a VBScript file. Create a blank text file called 'sethome.vbs'. Running it will change your default home page. Useful for setting the home page via a login script (or after some spyware may have changed your homepage).
Set WSHShell = WScript.CreateObject("WScript.Shell")

' home page URL
StartPage = "http://www.google.co.uk"
WSHShell.RegWrite "HKLM\Software\Microsoft\Internet Explorer\Main\Start Page", StartPage
WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\Start Page", StartPage
This code will add notepad to the Send To menu (when you right click a file). Create a new file called 'sendtonotepad.vbs', containing the following code.
Set WSHShell = WScript.CreateObject("WScript.Shell")

Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")

sWinDir = oFSO.GetSpecialFolder(WindowsFolder)

sSendTo = WSHShell.SpecialFolders("SendTo")

Set oShortCut = WSHShell.CreateShortcut(sSendTo & "\Notepad.lnk")

With oShortCut
 .WindowStyle = 3
 .IconLocation = "notepad.exe, 0"
 .TargetPath = sWinDir&"\notepad.exe"
 .Save
End With
Should work with all versions of windows after 95 (95/98/ME/2k/XP) with Windows Scripting installed. The code will not run in a web page. To allow IE site visitors to set their homepage to the one they are on, simple paste the following into your page:
<!--[if IE]>
<a href="#" onClick="this.style.behavior='url(#default#homepage)';this.setHomePage(location.href);">Set this page as your default homepage</a>
<![endif]-->
The latest version of Windows Scripting (required) can be found here

Monday, August 11, 2003

Overflow PRE tag (HTML & CSS)

Text doesn't wrap with the <pre> tag, so you need to scroll horizontally if there is a lot of text on one line:
Nulla neque diam, accumsan nec, egestas non, facilisis sed, wisi.    Donec lacinia vestibulum arcu. Suspendisse ligula. Donec tortor. Maecenas pharetra eros at odio.
	Morbi feugiat tempus orci. Cras vitae tellus. Sed congue nonummy magna. In sed neque id nisl tempus congue.
Duis varius egestas tellus. Suspendisse potenti. Nunc rhoncus, eros eget malesuada rhoncus, mi purus pharetra eros, sit amet porta lectus tortor ac erat.
In hac habitasse platea dictumst.
However, if you wrap it as follows <pre style="overflow: auto; width: 100%">Text</pre> it adds scroll bars when needed:
Nulla neque diam, accumsan nec, egestas non, facilisis sed, wisi.    Donec lacinia vestibulum arcu. Suspendisse ligula. Donec tortor. Maecenas pharetra eros at odio.
	Morbi feugiat tempus orci. Cras vitae tellus. Sed congue nonummy magna. In sed neque id nisl tempus congue.
Duis varius egestas tellus. Suspendisse potenti. Nunc rhoncus, eros eget malesuada rhoncus, mi purus pharetra eros, sit amet porta lectus tortor ac erat.
In hac habitasse platea dictumst.
width and overflow has to be defined, else it will not work. It also does not work particulary well when there are not many lines of text (scroll bars get in the way [at least 3 line breaks recommended]):
Nulla neque diam, accumsan nec, egestas non, facilisis sed, wisi.    Donec lacinia vestibulum arcu. Suspendisse ligula. Donec tortor. Maecenas pharetra eros at odio.

Monday, August 04, 2003

File Functions (JScript):

Various JScript functions for manipulating files: They should all work whichever format you use for the folder (c:\webroot\folder\subfolder\ or /folder/subfolder/)

Get files (returned as enumerator):
/*
sFolder = which folder (e.g. '/folder/subfolder/', 'c:\webroot\folder\subfolder\')
return enumerator of files
*/
function getFiles(sFolder){ 
	// create filesystemobject
	var oFSO = Server.CreateObject("Scripting.FileSystemObject")
	// get folder object
	try{
		var oFolder = oFSO.getFolder((sFolder.indexOf("/")==-1)?sFolder:Server.MapPath(sFolder))
	}catch(e){
		// if error occurs return blank enumerator
		return new Enumerator()
	}
	// create enumerator for files
	var eFiles = new Enumerator(oFolder.Files)
	// return files list
	return eFiles
}
Get file name:
/*
sPTF = path to file, bExt = include extension, default yes
returns filename as string
*/
function getFileName(sPTF,bExt){ 
	// new method using filesystemobject
	if(bExt!=false) bExt = true // if bExt is not supplied, the default is to return the file with the extension
	// create filesystemobject
	var oFSO = Server.CreateObject("Scripting.FileSystemObject")
	/*  
	get file object
	---------------
	there is a method to get the file name by using oFSO.GetFileName(sPTF)
	but it does not check to see if the file exists
	this method returns blank string if the file does not exist
	*/
	try{
		var oFile = oFSO.GetFile((sPTF.indexOf("/")==-1)?sPTF:Server.MapPath(sPTF))
		
	}catch(e){
		// if error occurs return blank string
		return ""
	}
	// get file name
	sFileName = oFile.Name
	// return dot?
	sFileName = (bExt)?sFileName:sFileName.substr(0,sFileName.lastIndexOf('.'))
	return sFileName.replace("&","&amp;")
	/* // old method using string manipulation
	iFNB = sPTF.lastIndexOf("\\")+1
	// file name begins here (the last \ in the file path), 1 is added to the result because javascript is zero based
	// if iFNB returns 0 then the supplied path is an absolute one, it is therefor compensated for
	iFNB = (iFNB==0)?sPTF.lastIndexOf("/")+1:iFNB
	if(bExt){ // if we want the extension
		// substring begins at iFNB and ends at the end of the string
		return sPTF.substr(iFNB) // return file (with extension)
	}
	else{ // if we don't want the extension
		// substring begins at iFNB and ends at the position of the last dot
		return sPTF.substr(iFNB,sPTF.lastIndexOf('.')-iFNB) // remove extension - including '.'
	} // end if(bExt)
	*/
}
Get file extension:
/*
sPTF = path to file, bDot = include dot, default false
returns file extension as string
*/
function getFileExt(sPTF,bDot){
	if(bDot!=true) bDot = false // if bDot is not supplied, the default is to return the extension with no dot
	// return substring beginning before or after the final dot according to bDot
	return sPTF.substr(sPTF.lastIndexOf('.')+((!bDot)?1:0))
}
Remove file extension ('file.ext' becomes 'file'):
/*
sFN = filename
return filename without extension
*/
function rmFileExt(sFN){
	return sFN.substr(0, sFN.lastIndexOf("."))
}
Get file size: consists of two functions - one for getting the file size, and one for converting it (if you already know the size)
/*
sPTF = path to file
sFmt = size format (b, kb, mb) - automatic if undefined
nDP = number of decimal places - default 2 if undefined
returns file size as string
*/
function getFileSize(sPTF, sFmt, nDP){
	// create filesystemobject
	var oFSO = Server.CreateObject("Scripting.FileSystemObject")
	// get file object
	try{
		var oFile = oFSO.GetFile((sPTF.indexOf("/")==-1)?sPTF:Server.MapPath(sPTF))
	}catch(e){
		// if error occurs return blank string
		return ""
	}
	// get file size
	var nFileSize = parseInt(oFile.size)
	sFmt = String(sFmt).toLowerCase()
	return fmtFileSize(nFileSize, sFmt, nDP)
}

function fmtFileSize(nFileSize, sFmt, nDP){
	if(isNaN(nDP)) nDP = 2
	var sAppend = ""
	if(sFmt!="b"||sFmt!="kb"||sFmt!="mb"){
		if(nFileSize<1000){
			var sFmt = "b"; var sAppend = " bytes";
		}
		else if(nFileSize>999 && nFileSize<1000000){
			var sFmt = "kb"; var sAppend = " kb";
		}
		else{
			var sFmt = "mb"; var sAppend = " mb";
		}
	}else{var sAppend = " "+sFmt}
	if(sFmt=="b") return nFileSize + sAppend
	if(sFmt=="kb"){
		// divide by 1024 to get the size in kb
		nFileSize = nFileSize / 1024;
		// multiply by 10 to the power of nDP
		nFileSize = nFileSize * Math.pow(10,nDP);
		// round up/down the new file size, and divide to remove the power and return the correct value
		nFileSize = Math.round(nFileSize) / Math.pow(10,nDP);
		return nFileSize + sAppend;
	}
	if(sFmt=="mb"){
		// divide by 1024 twice to get the size in mb
		nFileSize = nFileSize / 1024 / 1024;
		// multiply by 10 to the power of nDP
		nFileSize = nFileSize * Math.pow(10,nDP);
		// round up/down the new file size, and divide to remove the power and return the correct value
		nFileSize = Math.round(nFileSize) / Math.pow(10,nDP);
		return nFileSize + sAppend;
	}
}
File last modified date:
/*
sPTF = path to file
returns last modified date of file as date string
*/
function getLastModified(sPTF){
	// create filesystemobject
	var oFSO = Server.CreateObject("Scripting.FileSystemObject")
	// get file object
	try{
		var oFile = oFSO.GetFile((sPTF.indexOf("/")==-1)?sPTF:Server.MapPath(sPTF))
	}catch(e){
		// if error occurs return blank string
		return ""
	}
	// get file last modified
	dtLastModified = new Date(oFile.DateLastModified)
	return dtLastModified.toLocaleString()
}
Get file type:
/*
sPTF = path to file
returns file type as string
*/
function getFileType(sPTF){
	// create filesystemobject
	var oFSO = Server.CreateObject("Scripting.FileSystemObject")
	// get file object
	try{
		var oFile = oFSO.GetFile((sPTF.indexOf("/")==-1)?sPTF:Server.MapPath(sPTF))
	}catch(e){
		// if error occurs return blank string
		return ""
	}
	// get file type
	return oFile.Type
}
Get title, get comment: This gets summary information (for windows 2000). [Right click file, properties, select summary tab]
function getComment(sPTF){
	sPTF = (sPTF.indexOf("/")==-1)?sPTF:Server.MapPath(sPTF)
	var objShell = Server.CreateObject("Shell.Application");
	objFolder = objShell.NameSpace(sPTF.substr(0,sPTF.lastIndexOf("\\")+1));
	if (objFolder != null){
		objFolderItem = objFolder.ParseName(getFileName(sPTF,true));
		if (objFolderItem != null){
			return objFolder.GetDetailsOf(objFolderItem, 5); // 14 for XP and .NET Server
		}
	}
}
function getTitle(sPTF){
	sPTF = (sPTF.indexOf("/")==-1)?sPTF:Server.MapPath(sPTF)
	var objShell = Server.CreateObject("Shell.Application");
	objFolder = objShell.NameSpace(sPTF.substr(0,sPTF.lastIndexOf("\\")+1));
	if (objFolder != null){
		objFolderItem = objFolder.ParseName(getFileName(sPTF,true));
		if (objFolderItem != null){
			return objFolder.GetDetailsOf(objFolderItem, 11); // 10 for XP and .NET Server
		}
	}
}