jubert roldan
jubert roldan

Reputation: 21

how to make my scripts portable vbs

this would be a noob question..but I want to make my scripts more portable. lets say, I have coded all my .vbs(or any script in any code of nature) in a USB. so I want to run that vbs on that current machine. the USB is assigned to be in F: drive

however when i unplugged that usb and sticked to another machine..it is going to be no longer F:..but it could be E: G: or whatever

I just wanted to know how do i overcome that without changing it directly on the scripts but the script is capable of reading which directory its pointing toward.

Im not sure how that property/functionality is called.

but would appreciate any hints/tips

Upvotes: 1

Views: 166

Answers (1)

BoffinBrain
BoffinBrain

Reputation: 6525

There are two main ways you can make your script more portable.

The first, and probably the most appropriate for your use case, is to check what drive your executable is running on by using WScript.ScriptFullName and getting the first three characters to find the drive letter. You could alternatively chop the script name (WScript.ScriptName) off the end to find the current working directory. Assign this to a variable, and use it everywhere in your code where you specify a path.

Dim fullname : fullname = WScript.ScriptFullName
Dim drive : drive = Mid(fullname, 1, 3)
Dim path : path = Mid(fullname, 1, Len(fullname) - Len(WScript.ScriptName))
WScript.Echo "Drive = " & drive & vbcrlf & "Path = " & path

Another way is to make your script expect run-time arguments from the user, so you can specify exactly where you want to do things.

' Make a shortcut to your script and add a parameter,
' Or run from the command prompt with an additional parameter.
If WScript.Arguments.Length = 0 Then
    WScript.Echo "No arguments supplied!"
    WScript.Quit
End If

WScript.Echo WScript.Arguments(0)

I hope that helps!

Upvotes: 2

Related Questions