|
Path ValidatorClass for validating file or folder path patterns
File Name : PathValidatorCLS.js Requirement : none Author : Branimir Petrovic Submitted : 02/12/2001 Updated : 19/09/2002 Category : Class To use a class, follow this syntax : var MyObjClass = new MyClass; WScript.Echo(MyObjClass.MyMethod()); // FileName: PathValidatorCLS.js
//////////////////////////////////////////////////////////////////////
if (WScript.ScriptName == "PathValidatorCLS.js") { demo_PathValidatorCLS(); }
// ======================== CLASS ====================================
function PathValidatorCLS() {
// Author: Branimir Petrovic
// Date: 1 January 2002
// Version: 1.2.0
//
// Revision History:
// 2 January 2002 V 1.1.0 Added lines with: // <<<
// 13 January 2002 V 1.2.0 Added getVersion() method
//
// Methods:
// - getClassName() // Returns "PathValidatorCLS"
// - getVersion() // Returns version string "1.2.0"
// - isValidPath(sFQP)
// - isLocalPath(sFQP)
// - isUNCPath(sFQP)
// - getComputerName(sFQP) // For valid local paths - returns local computer name
// For valid UNC paths - returns remote computer name
// Note:
// isLocalPath and isUNCPath will return meaningful values only if passed path is valid,
// otherwise returned value will be "undefined" (which would be evaluated as false)
// --- Private Data Members ---
var SELF = "PathValidatorCLS";
var VERSION = "1.2.0";
var sComputerName;
var fLocalPath;
var fUNCPath;
//////////////////////////////////////////////////////////////////
this.getClassName = function () { return SELF; }
this.getVersion = function () { return VERSION; }
this.isValidPath = isValidPath;
this.isLocalPath = isLocalPath;
this.isUNCPath = isUNCPath;
this.getComputerName = getComputerName;
// ---------------- Constructor's work ends here ----------------
function isValidPath(sFQP) {
// Based on V 1.0.1 from 26 Nov 2001
// Added functionality for "catching" computer name and local path flag
sComputerName = void 0; // <<< Added 2 Jan 2002
fLocalPath = void 0; // <<< Added 2 Jan 2002
fUNCPath = void 0; // <<< Added 2 Jan 2002
// rule #0 -> Path must not be longer than 255 characters
if (typeof(sFQP)!="string") return false; // >>>
if (sFQP.length>255) return false; // >>>
// rule #1 -> Drive letter & colon pattern:
var regDrv = /^[A-Za-z]:$/;
// rule #2 -> One char name (not allowed chars: .\/*?<>| :")
var regOneCharName = /^[^.\*/\?<>\|:\" ]$/;
// rule #3 -> Two char name: -must not start or end with '.' or ' '
// -not allowed chars: \/*?<>|"
var regTwoCharNameA = /^[^.\*/\?<>\| :\"][^\*/\?<>\| :\"]$/;
var regTwoCharNameB = /^[^\*/\?<>\| :\"][^.\*/\?<>\| :\"]$/;
// rule #4 -> Three or more chars (rule #3 apply as well)
var regThrPlusCharName = /^[^.\*/\?<>\| :\"][^\*/\?<>\|:\"]+[^.\*/\?<>\| :\"]$/;
var sAry = sFQP.split("\");
var nUBound = sAry.length;
if (nUBound<2) return false; // >>>
if ( (sAry[0]=="")&&(sAry[1]=="")) {
// Looks like it is UNC format, but test
// individual path slices for compliance:
if (sAry.length<4) {
// spliced UNC path has at least 4 item long array
return false; // >>>
}
for (var i=2; i<nUBound; i++) {
if (i==(nUBound-1)) {
// test the very last 'slice'
if (sAry[i]=="") {
fUNCPath = true; // <<< Added 2 Jan 2002
fLocalPath = false; // <<< Added 2 Jan 2002
return true; // >>> empty is OK
} else {
fUNCPath = true; // <<< Added 2 Jan 2002
fLocalPath = false; // <<< Added 2 Jan 2002
return isNameOK(sAry[i]); // >>> could be OK
}
}
// test every other 'slice' (but the last)
if (!isNameOK(sAry[i])) return false; // >>> name is NOT OK
if (i==2) sComputerName = sAry[i] // <<< Added 2 Jan 2002
}
} else if (!regDrv.test(sAry[0]) ) return false;// >>> drive letter is NOT OK
// test local path slices for naming rules compliance
sComputerName = computerName(); // <<< Added 2 Jan 2002
for (var i=1; i<nUBound; i++) {
if (i==(nUBound-1)) {
// the very last 'slice'
if (sAry[i]=="") {
fLocalPath = true; // <<< Added 2 Jan 2002
fUNCPath = false; // <<< Added 2 Jan 2002
return true; // >>> empty is OK
}
else {
fLocalPath = true; // <<< Added 2 Jan 2002
fUNCPath = false; // <<< Added 2 Jan 2002
return isNameOK(sAry[i]); // >>> name is OK
}
}
if (!isNameOK(sAry[i])) return false; // >>> name is NOT OK
}
// --- Inner (helper) function
function isNameOK(sName) {
if (!sName) return false;
var nL = sName.length
switch (nL) {
case 1 :
return regOneCharName.test(sName);
case 2 :
return regTwoCharNameA.test(sName)&®TwoCharNameB.test(sName);
default :
return regThrPlusCharName.test(sName);
}
}
}
// --------------------------------------------------------------
function isLocalPath(sFQP) {
return isValidPath(sFQP)&&fLocalPath;
}
// --------------------------------------------------------------
function isUNCPath(sFQP) {
return isValidPath(sFQP)&&fUNCPath;
}
// --------------------------------------------------------------
function getComputerName(sFQP) {
if (isValidPath(sFQP)) return sComputerName;
else return "";
}
// ---------------- Helper Functions ----------------------------
function computerName() { return WScript.CreateObject("WScript.Network").ComputerName; }
} // ======================== END OF CLASS =========================
// ==================================================================
function demo_PathValidatorCLS() {
var oVal = new PathValidatorCLS();
var sPath1 = "\\Computer9\_`~!@#$%^&()[{]};'.,-_+=\MyFldr2\myfile.ext\";
var sPath2 = "C:\Documents and Settings\TRFFC02.ICO";
var sMsg = "Test with UNC path: \n";
sMsg += "oVal.isValidPath(\"" + sPath1 + "\") == ";
sMsg += (oVal.isValidPath(sPath1)? "true": "false") + "\n";
sMsg += "oVal.isLocalPath(\"" + sPath1 + "\") == " + oVal.isLocalPath(sPath1) + "\n";
sMsg += "oVal.isUNCPath(\"" + sPath1 + "\") == " + oVal.isUNCPath(sPath1) + "\n";
sMsg += "oVal.getComputerName(\"" + sPath1 + "\") == " + oVal.getComputerName(sPath1) + "\n\n";
sMsg += "Test with local path: \n";
sMsg += "oVal.isValidPath(\"" + sPath2 + "\") == ";
sMsg += (oVal.isValidPath(sPath2)? "true": "false") + "\n";
sMsg += "oVal.isLocalPath(\"" + sPath2 + "\") == " + oVal.isLocalPath(sPath2) + "\n";
sMsg += "oVal.isUNCPath(\"" + sPath2 + "\") == " + oVal.isUNCPath(sPath2) + "\n";
sMsg += "oVal.getComputerName(\"" + sPath2 + "\") == " + oVal.getComputerName(sPath2) + "\n\n";
WScript.Echo(sMsg);
}
// ==================================================================
|
|||||
|
|