各位前輩大家好,小弟想跟各位請教一下vbs的寫法,我在192.168.1.10的電腦有個data的資料夾;在裡面有各USER的名稱資料夾,但是我需要直接mount到files,路徑應該為\\192.168.1.10\data\%username%\files,我vbs的%username%改成Andy的話可以直接mount但改為%username%這個變數就無法辨識路徑,想請教一下這應該要怎麼改
我電腦有join domain,所以希望可以自動帶電腦使用者名稱,就可以使用
Set FSO = CreateObject("Scripting.FileSystemObject")
Set NetWorkObj = CreateObject("WScript.Network")
UserName = NetWorkObj.UserName
FolderPath = "\\192.168.1.10\data\"&UserName\files"
if not (FSO.FolderExists(FolderPath)) Then
FSO.CreateFolder(FolderPath)
End IF
Dim strFolder,objFSO
strFolder = "T:\"
set objFSO = createobject("Scripting.FileSystemObject")
if not objFSO.FolderExists(strFolder) then
Dim objNetwork
Dim strDriveLetter, strRemotePath, strNewName
strDriveLetter = "T:"
strRemotePath = "\\192.168.1.10\data\"&UserName\files"
strNewName = UserName & "的vbox"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
' Section which actually (re)names the Mapped Drive
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter).Self.Name = strNewName
end if
Dim SH, txtFolderToOpen
Set SH = WScript.CreateObject("Shell.Application")
txtFolderToOpen = "T:\"
SH.Explore txtFolderToOpen
Set SH = Nothing
WScript.Quit
因為有AD...所以....
UserName = NetWorkObj.UserName
這一行改成
<pre class="c" name="code">
Set objSysInfo = Createobject("ADSystemInfo")
UserName = objSysInfo.UserName
Set objSysInfo = Createobject("ADSystemInfo")
UserName = objSysInfo.UserName
感謝wiselou大大的回答,不過我將這行code帶入還是有問題;
我想要的是讀取某個User資料夾內的files
路徑\\192.168.1.10\data\"&UserName\files"
不過他讀到&UserName就無法自動改為目前使用者帳號,
我將最後的files刪除之後,就可以自動辨識;
所以一定要加files這資料夾的話,@UserName要如何才能自動辨識目前使用者帳號。
<pre class="c" name="code">strRemotePath = "\\192.168.1.10\data\"&UserName\files"
改為
<pre class="c" name="code">strRemotePath = "\\192.168.1.10\data\" &UserName &"\files"
試試看~