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?
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
You can use WScript.ScriptFullName to access the full path of the running script at run time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With