Results 1 to 4 of 4

Thread: [RESOLVED] %userprofile% & %systemdrive% options?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2008
    Posts
    159

    Resolved [RESOLVED] %userprofile% & %systemdrive% options?

    Hi All,

    I'm trying to figure out if there are any system options in VBscript that I could use similar to what are available through a CMD prompt or through a Batch file.

    The ones that I am most commonly looking to use are:

    %userprofile%
    %systemdrive%
    %systemroot%

    If the options are not available, how can I create something in VBscript that will automatically locate these areas?

    For example I want to find a certain folder in Application Data for the logged in user. In a Batch file I would do something like this.

    %userprofile%\Application Data\Folder

    How can that be done in VBS?

    Thanks in advance!

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: %userprofile% & %systemdrive% options?

    ExpandEnvironmentString

    vb Code:
    1. 'VBScript Example
    2. set WshShell = CreateObject("WScript.Shell")
    3. WScript.Echo WshShell.ExpandEnvironmentStrings("%SystemRoot%")
    4. WScript.Echo WshShell.ExpandEnvironmentStrings("%WinDir%")

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2008
    Posts
    159

    Re: %userprofile% & %systemdrive% options?

    Ok, so I tried what you show above (modified a little for my code for testing) and I get an error shown below.

    Script: File Path
    Line: 12
    Char: 1
    Error: Object Doesn't Supprt this property or method: 'WshShell.ExpandEnvironmentalStrings'
    Code: 800A01B6
    Source: Microsoft VBScript runtime error

    Here is my code. If I remove line 12 it shows the same error but on line 13.

    Code:
    Dim FilSys, Uname
    Dim FDel, UserName
    Dim strComputer
    Dim UserPath, WinDir, SystemRoot
    
    Set WshShell = Wscript.CreateObject("WScript.Shell")
    Set FilSys = CreateObject ("Scripting.FileSystemObject")
    Set Uname = CreateObject ("wscript.network")
    
    strComputer = "."
    UserName = Uname.UserName
    UserPath = WshShell.ExpandEnvironmentalStrings("%UserProfile%")
    WinDir = WshShell.ExpandEnvironmentalStrings("%WinDir%")
    SystemRoot = WshShell.ExpandEnvironmentalStrings("%SystemRoot%")
    FDel = (UserPath & "\Application Data\TEST")
    
    FilSys.DeleteFolder FDel

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2008
    Posts
    159

    Arrow Re: %userprofile% & %systemdrive% options?

    Ok... Well I got it to work now... Check out the code below. Not sure why the other way (that was previously provided) didn't work...

    Code:
    Dim FilSys, Envi
    Dim strComputer
    Dim UserPath, WinDir, SystemRoot
    
    Set WshShell = Wscript.CreateObject("WScript.Shell")
    Set FilSys = CreateObject ("Scripting.FileSystemObject")
    Set Envi = WshShell.Environment ("Process")
    
    strComputer = "."
    UserPath = Envi("UserProfile")
    'WinDir = WshShell.ExpandEnvironmentalStrings("%WinDir%")
    'SystemRoot = WshShell.ExpandEnvironmentalStrings("%SystemRoot%")
    WinDir = "\Application Data\Test"
    
    'MsgBox (UserPath & WinDir)
    FilSys.DeleteFolder (UserPath & WinDir)

Posting Permissions

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



Click Here to Expand Forum to Full Width