Solved

VBScript get self and parent folder name

Posted on 2009-04-01
1
9,752 Views
Last Modified: 2012-05-06
I'm overlooking somethign here. This script is saved as "location.vbs" and returns the name of the folder that location.vbs is in when run, and the parent folder. It works correctly when placed in any folder at least 3 directories down from the root, but fails on anything one or two directories from the root.

I'm having a brain-fade as to why. I need this to give me the name of the folder (note, NOT the path, just the folder name) that the script is sitting in when run, regardless of where that is. (The script will be moved, so it can't be hard-coded) and the name of the parent folder (if any, or null if the script is on root.)

Dim filesys
Set filesys = CreateObject("Scripting.FileSystemObject")
varPathCurrent = filesys.GetParentFolderName(WScript.ScriptFullName)
varPathParent = filesys.GetParentFolderName(varPathCurrent)
varPathGrandParent = filesys.GetParentFolderName(varPathParent)

varNameFolderCurrent = mid(varPathCurrent, len(varPathParent) + 2 , len(varPathCurrent) - len(varPathParent) - 1)
varNameFolderParent = mid(varPathCurrent, len(varPathGrandParent) + 2 , len(varPathCurrent) - len(varPathGrandParent) - len(varNameFolderCurrent) - 2)

msgbox varNameFolderCurrent & vbCrLf & varNameFolderParent
0
Comment
Question by:mcj
1 Comment
 
LVL 65

Accepted Solution

by:
RobSampson earned 125 total points
ID: 24044771
Hi there.

See if this works for you.

Regards,

Rob.
Dim filesys

Set filesys = CreateObject("Scripting.FileSystemObject")

varPathCurrent = filesys.GetParentFolderName(WScript.ScriptFullName)

varPathParent = filesys.GetParentFolderName(varPathCurrent)

varPathGrandParent = filesys.GetParentFolderName(varPathParent)
 

If filesys.FolderExists(varPathCurrent) Then

	If InStrRev(varPathCurrent, "\") < Len(varPathCurrent) Then

		varPathCurrent = Mid(varPathCurrent, InStrRev(varPathCurrent, "\") + 1)

	Else

		varPathCurrent = Left(varPathCurrent, Len(varPathCurrent) - 1)

	End If

Else

	varPathCurrent = ""

End If
 

If filesys.FolderExists(varPathParent) Then

	If InStrRev(varPathParent, "\") < Len(varPathParent) Then

		varPathParent = Mid(varPathParent, InStrRev(varPathParent, "\") + 1)

	Else

		varPathParent = Left(varPathParent, Len(varPathParent) - 1)

	End If

Else

	varPathParent = ""

End If
 

msgbox varPathCurrent & vbCrLf & varPathParent

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now