VBScript to get Windows Directory?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: VBScript to get Windows Directory?

  1. #1
    Join Date
    Aug 2005
    Posts
    218

    VBScript to get Windows Directory?

    How do I get the window's directory using VBScript? Here's how I get the Program Files Directory:

    Code:
    Function GetProgramsFolder()
    	Dim wshShell
    	Set wshShell = WScript.CreateObject("WScript.Shell")
    	GetProgramsFolder = wshShell.SpecialFolders("AllUsersPrograms")
    End Function

  2. #2

    Re: VBScript to get Windows Directory?

    Try this:

    Code:
    set shell = WScript.CreateObject("WScript.Shell")
    
    windowsdir = shell.ExpandEnvironmentStrings("%windir%")
    
    MsgBox(windowsdir)

  3. #3
    Join Date
    Aug 2005
    Posts
    218

    Re: VBScript to get Windows Directory?

    How about the system windows directory?

    for example: when you're in a machine with Terminal Services, I want the system windows directory rather than C:\Documents and Settings\Adminstrator\WINDIR

    thanks!
    Last edited by koden; October 31st, 2005 at 05:46 PM.

  4. #4
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,869

    Re: VBScript to get Windows Directory?

    %windir% is a constant. How did it load the profile WINDOWS folder?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Aug 2005
    Posts
    218

    Re: VBScript to get Windows Directory?

    anyone know how to get the SYSTEM windows directory? I need this in the case where there are multiple users in a terminal services enabled machine.

  6. #6

    Re: VBScript to get Windows Directory?

    Open a command prompt and type "set" and press enter. This will show you all the current environment variables. Find the one you need and use it accordingly.

  7. #7
    Join Date
    Aug 2005
    Posts
    218

    Re: VBScript to get Windows Directory?

    Quote Originally Posted by mmetzger
    Open a command prompt and type "set" and press enter. This will show you all the current environment variables. Find the one you need and use it accordingly.
    I need to program this dynamically, so that anyone running my vbscript will have their system's windows directory path available.

    please help!

  8. #8

    Re: VBScript to get Windows Directory?

    Quote Originally Posted by koden
    I need to program this dynamically, so that anyone running my vbscript will have their system's windows directory path available.

    please help!
    I don't understand what you're asking. Every user has environment variables configured. If the %windir% one does not work for what you need, login as one of said users and then type "set". This will show you all available variables. Find the one that works and use that in your vbscript.

  9. #9
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,869

    Re: VBScript to get Windows Directory?

    Okay, taking mmetzger's original thought. Change "%windir" to "%WindDir%"

    Code:
    set shell = CreateObject("WScript.Shell")
    WScript.Echo shell.ExpandEnvironmentStrings("%WinDir%")
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  10. #10
    Join Date
    Aug 2005
    Posts
    218

    Re: VBScript to get Windows Directory?

    Quote Originally Posted by mmetzger
    I don't understand what you're asking. Every user has environment variables configured. If the %windir% one does not work for what you need, login as one of said users and then type "set". This will show you all available variables. Find the one that works and use that in your vbscript.

    I'm writing a program using vbscript. It will be distributed to thousands of different machines. It needs to find the System's windows directory (C:\WINDOWS or C:\WINNT), not a users' directory (ie. C:\Documents and Settings\Administrator\Windows). Currently %windir% finds the user's windows directory.

    I cannot simply goto every computer and login different and type set. This needs to be dynamically discovered.

    Does %WinDir% get the system windows directory?

    This is only an issue on Terminal Services machines, though about 900 of the thousands will be terminal service enabled machines.

  11. #11

    Re: VBScript to get Windows Directory?

    Sigh...

    I'm not suggesting you to try it on every machine, but try it on one of the ones you expect to have issues.

    That being said, try %SystemRoot%.

  12. #12
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,869

    Re: VBScript to get Windows Directory?

    Quote Originally Posted by koden
    Does %WinDir% get the system windows directory?
    You keep saying "system windows" directory.

    There is only one windows directory (c:\windows\).
    Code:
    %windir%
    There is only one system directory (c:\windows\system32).
    Code:
    %systemdir%
    There is only one windows profile per user (c:\Documents and Settings\User\windows).
    There is only one system profile per user (c:\Documents and Settings\User\windows\system32).

    Both %windir% and %systemdir% are variables usually, but not always, set by Windows. If they are set, you can call them. If they are not, you cannot. It is rare that they will not work.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  13. #13
    Join Date
    Aug 2005
    Posts
    218

    Re: VBScript to get Windows Directory?

    I don't think anyone understands.

    This is not a problem on any specific machine. This is a product that is going out to thousands of customers with different machines. My vbscript needs to goto the C:\WINNT or C:\Windows or equivalent on their machines, even in terminal service enabled machines.

    %windir% goes to C:\WINNT or C:\WINDOWS etc. if it's not terminal service enabled. If it IS terminal service enambed, it will goto the .../User/Windows directory. I need to find a specific file that is NEVER in the /user/windows directory, but ALWAYS in the C:\WINNT or C:\WINDOWS type folder.

    In C++ this can be found using GetSystemWindowsDirectory(). GetWindowsDirectory will get the /user/windows if under terminal services, much like %windir%. So what is the equivalent of GetSystemWIndowsDirectory() in c++?

    mmetzger: you're totally missing the point. I've tried it on different machines, and I know for a fact that it is not getting the correct directory in terminal service enabled machines. This is a product that will be distributed to thousands of people, VERY dynamic and unpredictable. I NEED to be sure it will not fail under any circumstance. This is not some small little script I'm running on a little website or something.

  14. #14

    Re: VBScript to get Windows Directory?

    I understand the fact this is not on a single machine. What you don't seem to understand is that there are multiple variables that point to the same directory. I do not have access to a terminal server system to validate this, but on XP, 2000 and 2003 SystemRoot points to the directory you're looking for. What I want to know however is if there is a different variable on a terminal server login that points to it. Hence the reason I was telling you to run "set" and determine that. If that is the case, you simply check for the existence of it first. If you find it then use it, otherwise check windir, systemroot, etc.

  15. #15
    Join Date
    Aug 2005
    Posts
    218

    Re: VBScript to get Windows Directory?

    Quote Originally Posted by mmetzger
    I understand the fact this is not on a single machine. What you don't seem to understand is that there are multiple variables that point to the same directory. I do not have access to a terminal server system to validate this, but on XP, 2000 and 2003 SystemRoot points to the directory you're looking for. What I want to know however is if there is a different variable on a terminal server login that points to it. Hence the reason I was telling you to run "set" and determine that. If that is the case, you simply check for the existence of it first. If you find it then use it, otherwise check windir, systemroot, etc.
    ah i see what you mean. totally didn't understand the first couple times =P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center