10. VbScript | Script Directory

Try the codes for yourself to memorize the material. Ask if you have any questions.

So today we are going to talk about the Script Directory. Say we have a file that is located on the desktop, but we don’t know exactly what the directory (address in other words) is. So we are going use a wscript function. We’ve worked with wscript before, examples including wscript.sleep() which tells your VbScript to pause and wscript.quit which quits or exits the script altogether, wscript.echo “” which is the equivalent to a message box.

But for finding the Script Directory, we use wscript.scriptname to tell us the name of the script file and script.scriptfullname gives us the complete path of the script file.

Output:

Capture
Run the Script
2
This is message box for a
3
This is message box for b

But if you are looking for only the directory of the script file instead of the full path with the filename, you need to subtract the script-full-name from the script-name using Len, Left, Right functions.

First step is to figure out the number of characters from the script-full-name and from the script-name. Then you subtract them from each other to leave the directory part:

Output:

1
Message box a shows the file name is 12 characters long.
2
Message box b shows the file path is 34 characters long.

This is how you subtract out the number of characters in the file name from the full path:

Output:

3
22 character difference

Last step:

Output:

4
The directory without the file name

What's Your Opinion?