Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBScript -- Refer To Self In Program

Tags:

vbscript

Is there a way to refer to the current file running in VBScript? I could just use the name of the file, but it needs to be operable despite directory changes and renames. The purpose of this is to use the file in a file I/O operation. If not possible, are there any potential alternatives, such as making a file non-re-namable, or non-movable?

like image 439
James Parsons Avatar asked Jun 25 '26 05:06

James Parsons


2 Answers

WScript.ScriptFullName gives you the full path to your running script. You can use the FileSystemObject to parse this path further, if you'd like. For example:

' Assuming the script is at c:\scripts\test.vbs
strFile = WScript.ScriptFullName

With CreateObject("Scripting.FileSystemObject")
    MsgBox .GetDriveName(strFile)           ' => c:
    MsgBox .GetParentFolderName(strFile)    ' => c:\scripts
    MsgBox .GetFileName(strFile)            ' => test.vbs
    MsgBox .GetBaseName(strFile)            ' => test
    MsgBox .GetExtensionName(strFile)       ' => vbs    
End With
like image 186
Bond Avatar answered Jun 30 '26 05:06

Bond


You can use WScript.ScriptFullName to access the full path of the running script at run time.

like image 32
rory.ap Avatar answered Jun 30 '26 04:06

rory.ap