Solved

VBScript get self and parent folder name

Posted on 2009-04-01
1
10,452 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Batch/VBScript : Disable Windows tasks 7 40
powershell script 9 67
Restart service script 5 14
Index Match Formula VBA 6 23
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

821 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