VBScript Basics, Part 10 | Script Directory


VBScripting (.vbs) Basic tutorial on how to find out your script directory. This includes the name of your vbscript file you are running, as well as the full path, and current directory it is located in at all times.

Code Example: (Using Left & Len).
______________________________________________________

msgbox Left(wscript.scriptfullname, (Len(wscript.scriptfullname) – Len(wscript.scriptname)))
______________________________________________________

Code Example: (Running a script/file without a username).
______________________________________________________

CreateObject(“WScript.Shell”).Run “C:Users%username%Desktoptest.txt”
______________________________________________________

Code Example: (Four other options to get the Current Directory).
______________________________________________________

Option Explicit
Dim cd1, cd2, cd3, cd4

cd1 = CreateObject(“WScript.Shell”).CurrentDirectory
cd2 = CreateObject(“Scripting.FileSystemObject”).GetAbsolutePathName(“.”)
cd3 = CreateObject(“Scripting.FileSystemObject”).GetParentFolderName(Wscript.ScriptFullName)
cd4 = Replace(WScript.ScriptFullName,WScript.ScriptName, “”)

wscript.echo cd1
wscript.echo cd2
wscript.echo cd3
wscript.echo cd4
______________________________________________________

Know the Basics:
——————————————————————————————
wscript.scriptname = displays the name of your vbscript file.
wscript.scriptfullname = displays the full path and name of your vbscript file.
——————————————————————————————

Leave a Comment