Tuesday, July 10, 2012

How to map network drive using VBScript


Some time it is required to map a network drive using vbscript.
Today I will demonstate how to connet and then disconnect from a network drive.

In order to connect to a network drive, we need following data/information:

1. HomeServer Location: Actual path of the drive. In our example it is
"\\125.99.218.158\Data"

2. Name of the drive you wish to give: Let us say Z:

3. UserId/Password using which we will connect the map drive. If userid is same as logged in user then it is not required.

We will create an object of WScript.Network and call its method MapNetworkDrive

Here is the code:

Option Explicit
Dim strUser, strPassword, strDriveLetter, strHomeServer, strProfile
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")

strDriveLetter = "Z:"
strHomeServer = "\\125.99.218.158\Data"
strProfile = "False" ' Mapping (not) stored in user Profile
strUser = "USERID"
strPassword = "PASSWORD"

objNetwork.MapNetworkDrive strDriveLetter,strHomeServer,strProfile,strUser,strPassword

WScript.Quit

So the above code is creating an object of Wscript.Network and calling method MapNetworkDrive by passing parameters like drive letter, hom server, userid and password.
There is one more parameter that is strProfile. If set true, it will store the drive info in user profile and it will connect automatically next time when user logs in.



RemoveNetworkDrive

Remove a shared network drive mapping.
Syntax
      objNetwork.RemoveNetworkDrive(strName, [bForce], [bUpdateProfile])
Key
   objNetwork : A WScript.Network object
   strName    : The mapped drive you want to remove.
 
   bForce     : Force the removal of the mapped drive (TRUE/FALSE).
 
   bUpdateProfile : Remove the mapping from the user's profile (TRUE/FALSE).
Example
   Dim objNetwork
   Set objNetwork = CreateObject("WScript.Network")
   objNetwork.MapNetworkDrive "I:", "\\print_server\hp_01","True","jdoe","jdoepassword"

   objNetwork.RemoveNetworkDrive "I:"





Sunday, July 1, 2012

document.cookie


One property alone carries the burden of realizing session-only cookies in JavaScript:
document.cookie Read/write property that stores and persists data in a semicolon (;) delimited format
Think of document.cookie as the familiar variable, but with a twisted, persistent personality: Below we store a string into it, and examine what comes out:
document.cookie="JavaScript Kit"
alert(document.cookie) 
You should see 1 of 3 things get alerted- 1) "JavaScript Kit", 2) A string containing multiple values, one of which is "JavaScript Kit", or 3) A blank string. Note that if you see nothing (blank string), your browser does not have cookies enabled (hint: change that!).
document.cookie is a "public facility", with its usage shared by all scripts on all sites that access it. Due to this, the property often contains more values than you had stored into it, the other values being from other sites. When this is the case, each value is separated by a semicolon. All of these values are persisted until the browser is closed.

 Retrieving data from the cookie

Ok, so everyone can take a bite out of document.cookie; how do you locate your piece and grab it? Herein lies the bulk of your work when working with session only cookies- retrieving what was put into it.
The following 2 steps are involved in most techniques used to get your data out of a session cookie:
1) Store your data using a name/value pair format
2) Use string methods such as string.indexOf() and string.split() to scan for/retrieve this data
For example:
//store data using name/value format
document.cookie="sitename=JavaScript Kit" 

//retrieve value from cookie
var beginindex, endindex, result
//set begin index to 1st letter of value ("W")
beginindex=document.cookie.indexOf("sitename")+9
endindex=beginindex
//while we haven't hit ";" and it's not end of cookie
while (document.cookie.charAt(endindex)!=";"&&
endindex<=document.cookie.length)
endindex++
//result contains "JavaScript Kit"
var result=document.cookie.substring(beginindex,endindex) 
Quite a mess, all the reason to read on...
-Get cookie routine
If there's a time to forgo your ego and use premade JavaScript libraries, it's when working with cookies. Read up on the subject in most JavaScript books, and you'll find the same recommendation.
With respect to the topic at hand- retrieving data from a cookie- an excellent prebuilt routine to use is the following:

Shelley Power's get cookie routine:


//Get cookie routine by Shelley Powers 
function get_cookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) { 
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
   return returnvalue;
}
With it in place, to extract your data from document.cookie, simply input the data's name as parameter:
var result=get_cookie("sitename") 
Should the corresponding value not exist, the routine returns an empty string.
p.s: See here for a complete JavaScript cookie library.
We now know how to both store and retrieve data from a session-only cookie. But what good is that?

test