jmsdhn 發表於 2022-10-14 18:08

【VBS】獲取VBS本身所在資料夾後的處置方法

本帖最後由 jmsdhn 於 2022-10-16 16:28 編輯

打擾了!

假定如下內容放在R:\AAA\TEST.VBS
scriptdir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
MsgBox "所在位置:" & scriptdir

將顯示【所在位置:R:\AAA】

想請教如何在TEST.VBS內
繼續使用【R:\AAA】這個可變的字串值
譬如
ws.run "R:\AAA\測試.bat",0


感謝指教!

maodou 發表於 2022-10-16 14:50

scriptdir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Set shell = CreateObject("WScript.Shell")
shell.CurrentDirectory = scriptdir
shell.Run "測試.bat", 0, True

jmsdhn 發表於 2022-10-16 16:27

本帖最後由 jmsdhn 於 2022-10-17 11:08 編輯

感謝不吝指教!

看來第三行是關鍵
感覺等號兩邊是同等級(同樣是物件)
才能造成相等

剛去搜尋shell.CurrentDirectory或CurrentDirectory看能不能知道這是怎麼用的
不過好像沒有官方說明
感覺CurrentDirectory很像是CMD指令後的參數
且又歸於物件等級
然後SHELL得到CurrentDirectory回傳的值後
又切到RUN參數來執行CurrentDirectory指向的執行程式或批次吧

在測試.bat中做了簡單的dir/b>%TEMP%\AAA.log
果然成功產生AAA.log和VBS所在的檔案列表了

感謝教學!

maodou 發表於 2022-10-16 17:02

jmsdhn 發表於 2022-10-16 16:27
剛去搜尋shell.CurrentDirectory或CurrentDirectory看能不能知道這是怎麼用的
不過好像沒有官方說明
How Can I Change the Working Folder of a Script?
頁: [1]
查看完整版本: 【VBS】獲取VBS本身所在資料夾後的處置方法