請教各位前輩:
小弟因對程式不是很懂,但因公司有網路磁碟連接後的更名需求,因此在網路上搜尋了一個VBS程式後,透過撰寫改版,而產生了一個VBS檔,目前使用者套用該VBS檔案後,會產生網路磁碟機(已更名),但如果重覆執行時,便會出現錯誤訊息,不知應如何才能重覆建立,可以類似net use z: /delete的方式重覆執行,卻不會有錯誤訊息產生。
以下是我寫的指令,還請各位前輩指導應如何修改至可重覆執行,謝謝
' NameDrive.vbs
' VBScript to map a network drive.
' Authors Guy Thomas and Barry Maybury
' Version 1.4 - April 2010
' ----------------------------------------'
'
Option Explicit
Dim objNetwork0, strDrive0, objShell0, objUNC0
Dim objNetwork1, strDrive1, objShell1, objUNC1
strDriveLetter0 = "H:"
strRemotePath0 = "\\192.168.123.123\123"
strNewName0 = "123"
' Section to map the network drive
Set objNetwork0 = CreateObject("WScript.Network")
objNetwork0.MapNetworkDrive strDriveLetter0, strRemotePath0
' Section which actually (re)names the Mapped Drive
Set objShell0 = CreateObject("Shell.Application")
objShell0.NameSpace(strDriveLetter0).Self.Name = strNewName0
'
strDriveLetter1 = "J:"
strRemotePath1 = "\\192.168.123.123\456"
strNewName1 = "456"
' Section to map the network drive
Set objNetwork1 = CreateObject("WScript.Network")
objNetwork1.MapNetworkDrive strDriveLetter1, strRemotePath1
' Section which actually (re)names the Mapped Drive
Set objShell1 = CreateObject("Shell.Application")
objShell1.NameSpace(strDriveLetter1).Self.Name = strNewName1
' End of Example VBScript.
'
VBScript 刪除網路磁碟機
<pre class="c" name="code">
Option Explicit
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
objNetwork..RemoveNetworkDrive "H:"
程式碼copy & paste之後,請稍微做點功課,研究一下。
這位前輩所提供的方式,其實小弟我也有想過,但全部移除再全部建立,並非是我最想要的結果,比較是希望由判斷的方式去確認磁碟存在與否,不存在就自動建立,有存在的就略過。
用on error resume next吧....遇到錯誤就略過
如果要判斷是否為網路磁碟機,這個可能要動用到WMI了
簡單一點的方式
<pre class="c" name="code">
Option Explicit
Dim objFSO
Dim objNetwork0, strDrive0, objShell0, objUNC0
Dim objNetwork1, strDrive1, objShell1, objUNC1
Set objFSO = CreateObject("Scripting.FileSystemObject")
'判斷磁碟機H是否存在,不存在才連接網路磁碟機
If (objFSO.DriveExists("H:") = False) Then
strDriveLetter0 = "H:"
strRemotePath0 = "\\192.168.123.123\123"
strNewName0 = "123"
Set objNetwork0 = CreateObject("WScript.Network")
objNetwork0.MapNetworkDrive strDriveLetter0, strRemotePath0
End If
Set objShell0 = CreateObject("Shell.Application")
objShell0.NameSpace(strDriveLetter0).Self.Name = strNewName0
If (objFSO.DriveExists("J:") = False) Then
strDriveLetter1 = "J:"
strRemotePath1 = "\\192.168.123.123\456"
strNewName1 = "456"
Set objNetwork1 = CreateObject("WScript.Network")
objNetwork1.MapNetworkDrive strDriveLetter1, strRemotePath1
End If
Set objShell1 = CreateObject("Shell.Application")
objShell1.NameSpace(strDriveLetter1).Self.Name = strNewName1
感謝前輩賜教,真的很感謝你,小弟於今早已編修撰寫完成,明日再將在下所請益後寫出的結果,與前輩共同分享,謝謝。
此為撰寫出的結果,如有寫的不太順的地方或錯誤的地方,再請給予指導,謝謝
http://ithelp.ithome.com.tw/upload/images/20130530/2013053008441751a6a0e13a583_thumb.jpg
Option Explicit
Dim strDriveLetter, strRemotePath, strNewName
Dim objNetwork, objShell
Dim i
For i =0 To 1 -->套用迴圈
Select Case i
Case 0
strDriveLetter = "H:" -->磁碟機代號
strRemotePath = "\\192.168.123.123\123" -->欲連結的路徑
strNewName = "123" -->網路磁碟機所要給的更換名稱
Case 1
strDriveLetter = "K:"
strRemotePath = "\\192.168.123.123\456"
strNewName = "456"
End Select
'*** remove ***
On Error Resume Next
Set objNetwork = CreateObject("WScript.Network")
Call objNetwork.RemoveNetworkDrive(strDriveLetter, True)
objNetwork = nothing
'*** create ***
Set objNetwork = CreateObject("WScript.Network")
Call objNetwork.MapNetworkDrive (strDriveLetter, strRemotePath)
objNetwork = nothing
'*** rename ****
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter).Self.Name = strNewName
objShell = nothing
NEXT
要用功呀
因小弟所走的是系統網管,對程式部份真的比較笨,讓前輩你見笑了。日後一定會努力學習的,謝謝。
網管還是得懂些程式,這樣要做些自動化管理比較方便。
網海無涯,學無止盡啊~但可以多跨足一些領域學習,這確實是很好的事情,小弟目前正在努力學習JAVA Script及PHP,只因公司提出這方面的需求,但小第還是不得不說句內心話啊,現在的MIS真的是萬能雜工啊。
網管學 vbscript,powershell,shell script,perl 會比較有用吧!
你寫網頁程式又不會比 programmer 專業,
看得懂別人在寫什麼就好了.