JSI Tip 10292. How can a Vbscript set the current folder to a files parent folder?


If you wanted to set the current directory to the parent folder of a fully qualified file name:

Dim fso, oArgs
Set oArgs = WScript.Arguments
FQFN = oArgs(0)
Set fso = CreateObject("Scripting.FileSystemObject")
GetTheParent = fso.GetParentFolderName(FQFN)
oShell.currentdirectory = GetTheParent

Sample

cscript //nologo c:\util\test2.vbs %Systemroot%\SYSTEM32\DRIVERS\tcpip.sys

returns:

C:\WINDOWS\SYSTEM32\DRIVERS
C:C:\WINDOWS\SYSTEM32\DRIVERS

where test2.vbs contains:

Dim fso, oArgs
Set oArgs = WScript.Arguments
FQFN = oArgs(0)
Set fso = CreateObject("Scripting.FileSystemObject")
GetTheParent = fso.GetParentFolderName(FQFN)
Wscript.Echo GetTheParent
set oShell = createobject("wscript.shell")
wscript.echo oShell.currentdirectory
oShell.currentdirectory = GetTheParent
wscript.echo oShell.currentdirectory



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish