1. 程式人生 > 程式設計 >VBS開啟當前指令碼所在資料夾

VBS開啟當前指令碼所在資料夾

方法一:Wscript.ScriptFullName

'建立一個 Wscript.Shell 物件的例項,稍後會使用這個物件啟動 Windows 資源管理器
Set objShell = CreateObject("Wscript.Shell")
'獲取指令碼的路徑
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
'獲取腳本當前所在資料夾的路徑
strFolder = objFSO.GetParentFolderName(objFile) 
strPath = "explorer.exe /e," & strFolder
'啟動 Windows 資源管理器,開啟指令碼所在的資料夾
objShell.Run strPath

方法二:objShell.CurrentDirectory

這種方法程式碼少了一些

set objShell = CreateObject("Wscript.Shell")
'指令碼的當前目錄
strPath = objShell.CurrentDirectory
strPath = "explorer.exe /e," & strPath
objShell.Run strPath

下面是我們小編的補充

如果是指令碼中需要呼叫下面很簡單的一句話就可以獲取當前目錄

currentpath = createobject("Scripting.FileSystemObject").GetFolder(".").Path

currentpath = createobject("Scripting.FileSystemObject").GetFile(Wscript.ScriptFullName).ParentFolder.Path

是不是更簡單呢,這篇文章就分享到這了,希望大家以後多多支援我們。