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("&","&")
	/* // 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
		}
	}
}

Comments

Popular posts from this blog

Link: HTML Agility Pack (.NET)

Basic Excel Spreadsheet Generation (ASP/ASP.NET)

ASP.NET: Binding alphabet to dropdown list