WSH の JScript を書いていて、ふと起動パスをえる方法を調べることに。
function getAppPath() { // 起動パスを得る(末尾にパス記号付) var full = WScript.ScriptFullName; var name = WScript.ScriptName; var appPath = full.substr(0, full.length - name.length); return appPath; } WScript.Echo(getAppPath); WScript.Quit();
よく使うので、一行でコピペできるように整形したら以下の感じ
var appPath = WScript.ScriptFullName.substr(0, WScript.ScriptFullName.length - WScript.ScriptName.length);
あるいは、以下のようにする
var fso = WScript.CreateObject("Scripting.FileSystemObject"); var file = fso.GetFile(WScript.ScriptFullName); var appPath = file.ParentFolder.Path; WScript.Echo(appPath);
よく使うので、一行でコピペできるようにしたら以下のようになりました。
var fso = WScript.CreateObject("Scripting.FileSystemObject"); var appPath = fso.GetFile(WScript.ScriptFullName).ParentFolder.Path;
FileSystemObjectを使うなら、上記の方がスマートかな?
function getAppPath() { var fso = new ActiveXObject("Scripting.FileSystemObject"); return fso.GetFile(WScript.ScriptFullName).ParentFolder.Path; }
もし、HTAであれば、location.href を使うと起動パスを得られます。
alert(location.href); // file:///xxx.hta というURLが得られる // ... var fso = new ActiveXObject("Scripting.FileSystemObject"); var appUri = fso.GetParentFolderName(location.href); var appPath = appUri.substr("file:///".length).split("/").join("\\");