Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

objNetwork query 1

Status
Not open for further replies.

captedgar

Programmer
Dec 4, 2009
20
0
0
GB
Hi there

I have one mapped network drive to my PC at home. i call it the V Drive
i came across a Jscript which had the following line when
EnumNetworkDrives Method was initiated


var x = objNetwork.EnumNetworkDrives();
for(i = 1; i < x.length; i += 2)
{
if(strRoot.toUpperCase()==x.Item(i).toUpperCase())
{
return true;
}
}

please note strRoot = is a folder location on my PC
can u explain how this code works and what is passed to strRoot finally. i know each drive occupies 2 items in the collection of objNetwork. Each mapped Drive takes up 2 items in the collection, one for drive letter and one for UNC Path i.e. \\server1\share1.
 
Nothing is passed to strRoot. Your code merely checks to see if strRoot is equal to one of the Items in the array obtained from the objNetwork.Enum call.

If it finds it, it returns true to whatever called it.

Otherwise it returns nothing.

What is it you are attempting to do?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for the reply.

The following is the function i have and the following are the parameters passed to it
strRoot = \\server1\\share1
bUserName = name of the user(passed as a string value) ex: captedgar
There is only 1 mapped drive to the PC which is V Drive.
Can u now explain the logic behind this function and what would be the output please?

function AssignDrives(strRoot,bUserName)
{
var x = objNetwork.EnumNetworkDrives();
for(i = 1; i < x.length; i += 2)
{
if(strRoot.toUpperCase()==x.Item(i).toUpperCase())
{
return true;
}
}

var output;
if(bUserName)
{
output=WshShell.Run('cmd /c net use /USER:'+ WScript.Arguments.Named.Item('List') + ' '+strRoot,2,true);
}
else
{
output=WshShell.Run('cmd /c net use '+strRoot,2,true);
}
if(0==output)
{
return strRoot;
}
return false;
}


 
I know most of the logic my main questions are as follows

1. for(i = 1; i < x.length; i += 2) = how this will evaluate when u only have 1 mapped drive called V Drive. Will it read V Drive or will it read strRoot = \\server1\\share1

2. if(strRoot.toUpperCase()==x.Item(i).toUpperCase())
How will this evaluate based on question 1

 
Basically your code is checking to see whether the path has already being mapped to a drive letter, if it hasn't it moves forward and maps the drive.

1. for(i = 1; i < x.length; i += 2) = how this will evaluate when u only have 1 mapped drive called V Drive. Will it read V Drive or will it read strRoot = \\server1\\share1

This is just a loop, it doesn't evaluate anything. It runs a loop a specific number of times (x.length is that number). x.length being the number of entries in the array returned by the EnumNetworkDrives.

If there's only one mapped drive then it will only run it 2 times since each drive has 2 entries.

2. if(strRoot.toUpperCase()==x.Item(i).toUpperCase())
How will this evaluate based on question 1
With that it performs the check, to find if the path provided by strRoot already exists as a mapped drive or not.

If it finds it, then it issues the return true; command which automatically exits the function.

If it doesn't find it, it moves on and executes the rest of the code.

Where it checks to see if bUserName is set, if so it executes the net use command to map the drive using the specified user's credentials.

If no bUserName is specified it just attempts to map the drive with no credentials. Using 'net use' again.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
You need to be able to read documentation before you be somebody.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top