编程技术、软件应用与系统模拟

(Programming, Applicaiton and Simulation)



本站目录

 

首页
ASP/Access/IIS
DELPHI/PASCAL
PASCAL高级编程
C语言编程实例
WORD
Excel
MATLAB
MINITAB讲座
Windows
DOS
SAS
生物系统模拟
土壤水分剖析器
其他



镜像站点

 

主站
北美镜象站
欧洲镜象站(1)
欧洲镜象站(2)

本站 Google

[搜索]  [站内导航]
座右铭:
只做有益人类的事
不做有害人类的事


Mapping network drives in LAN (Windows Script Host)
Zhanshan Dong

In Windows, there several varieties of script files with different file extensions as the
following.

.bat   MS-DOS batch file MS-DOS operating system batch file
.asp   ASP page Active Server Page file
.html  HTML file Web page
.js    JScript file Windows script
.vbs   VBScript file Windows script
.wsf   Windows Script Host file Container or project file for a Windows script; supported
       by WSH 2.0 and later.
.wsh   Windows Script Host files Property file for a script file; supported by WSH 1.0 and
       later.

Each script type is suited to different application needs, and each has strengths and
weaknesses. If you are familiar with DOS, I sure you know BATCH files. If you wrote
webpages, you might be familiar with HTML, ASP, and/or JS/VBS. If you work with Windows
itself, you should be famaliar with WSH or WSF. WSF file is more powerful script file which
can include several scripts to achieve different, but related functions. WSF file must run
under WSH 2.0 and later. If your OS is Windows 2000/Me or later, you already have it.
Otherwise, you need go to Microsoft.COM to download a latest version of Windows Script
Host.

In my lab, there is a cluster of 11 computers. I used to setup the network drives for each
machine individually. But sometimes I mixed the drive names up. I get sick of it. I turned
to Widnows Script Host to write a script project (see following source code) an distributed
it to all machines. This script project includes three functional scripts: (1) list all
current network drive mapping; (2) remove all current network drive mapping; (3) map all
available network drive with fixed drive name.

Although this script is useful, you need run it every time after you reboot your computer.
That is not good, especially when you have a cluster of computers. I put a register key to
Windows registery. The register key is as the below.

REGEDIT4
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run]
"MapNetDrives"="c:\\windows\\wscript //job:map c:\\windows\\network.wsf"

To be noticed, the script file (network.wsf) needed to put under Windows folder to make the
registery key work. After this is done, the computer will map all available network drive
everytime after computers are rebooted.

If you'd like put a shortcut on your desktop to run the mapping function, you need create a
shortcut for the script file and put it on your desktop first. Then you should modify the
shortcut property. Change the Target textbox to the following string.

C:\WINDOWS\wscript.exe //job:map C:\WINDOWS\network.wsf

IF you'd like put a shortcut link on your desktop to remove network drives, you should
change the Target textbox to the following string.

C:\WINDOWS\wscript.exe //job:remove C:\WINDOWS\network.wsf

Ok, enjoying yourself.

Source Code
===========
<package>
   <job id="list">
      <script language="VBScript">
         CRLF = chr(13) & chr(10)
         Set oNet = WScript.CreateObject("WScript.Network")
         Set oDrives = oNet.EnumNetworkDrives
         sString = "Network drive mappings:" + CRLF
         if oDrives.Count = 0 then
            sString = sString + "No network drives."
         else
            For i = 0 to oDrives.Count - 1 Step 2
               sString = sString + "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1) + CRLF
            Next
         end if
         Wscript.Echo sString
      </script>
   </job>
   <job id="remove">
      <script language="VBScript">
 	 Dim oNet
	 Set oNet = WScript.CreateObject("WScript.Network")
         Set oDrives = oNet.EnumNetworkDrives
         For i = 0 to oDrives.Count - 1 Step 2
            oNet.RemoveNetworkDrive oDrives.Item(i)
         Next
      </script>
   </job>
   <job id="map">
      <script language="VBScript">
        on error resume next
        dim allmap(12,2)
        allmap(0,1) = "D:"
        allmap(0,2) = "\\lamco_7\maincdrom"
        allmap(1,1) = "E:"
        allmap(1,2) = "\\lamco_7\zip250"
        allmap(2,1) = "G:"
        allmap(2,2) = "\\lamco_7\main-c"
        allmap(3,1) = "M:"
        allmap(3,2) = "\\lamco_1\c"
        allmap(4,1) = "N:"
        allmap(4,2) = "\\lamco_2\c"
        allmap(5,1) = "O:"
        allmap(5,2) = "\\lamco_3\c"
        allmap(6,1) = "P:"
        allmap(6,2) = "\\lamco_4\c"
        allmap(7,1) = "Q:"
        allmap(7,2) = "\\lamco_5\c"
        allmap(8,1) = "R:"
        allmap(8,2) = "\\lamco_6\c"
        allmap(9,1) = "S:"
        allmap(9,2) = "\\lamco_8\c"
        allmap(10,1) = "T:"
        allmap(10,2) = "\\lamco_9\c"
        allmap(11,1) = "U:"
        allmap(11,2) = "\\lamco_10\c"
        allmap(12,1) = "V:"
        allmap(12,2) = "\\lamco_11\c"

        Set oNet = WScript.CreateObject("WScript.Network")
        Set oDrives = oNet.EnumNetworkDrives
        For i = 0 to oDrives.Count - 1 Step 2
            oNet.RemoveNetworkDrive oDrives.Item(i)
        Next
	Set oShell = CreateObject("WScript.Shell")
	ComputerName = UCase(oNet.ComputerName)
	for i = 0 to 12
	   mapStr = UCase(allmap(i,2))
	   pos = inStr(mapStr,ComputerName)
	   if pos>0 then
	   	oShell.Exec("%comspec% /c subst " & allmap(i,1) & " c:\")
	   else
	   	oNet.MapNetworkDrive allmap(i,1), allmap(i,2),"false","","1d2s3z4$"
	   end if
	next
      </script>
   </job>
</package>
     Download source code(Please right click your mouse and choose "save target as")

© 1998-, 董占山, 版权所有。
转载文章请注明出处(www.sunfinedata.com/articles)。